Skip to content

Commit e85d2dc

Browse files
committed
fix: avoid this
1 parent fc17577 commit e85d2dc

File tree

2 files changed

+65
-59
lines changed

2 files changed

+65
-59
lines changed

packages/common-helpers/src/i18n.ts

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,40 +43,44 @@ interface iUseI18n<L extends Record<string, string | Record<string, string>>> {
4343
export default function useI18n<L extends Record<string, string | Record<string, string>>>(
4444
options: iPluginOptions & { locale?: L } = {}
4545
): iUseI18n<L> {
46-
return {
47-
t<K extends string & keyof L, Ko extends L[K], KA extends string & keyof Ko>(
48-
key: Ko extends string ? K : `${K}.${KA}`,
49-
data: number | { [key: string]: unknown; count?: number } = {},
50-
fallback = `No locale for "${key}" provided`
51-
): string {
52-
// Empty string string if locale doesn't exist
53-
let locale = get(options.locale || {}, key, fallback);
54-
const interpolate = /\{(.+?)\}/g;
55-
const plurals = locale.split("|");
56-
const count = typeof data === "number" ? data : (data?.count ?? -1);
46+
function t<K extends string & keyof L, Ko extends L[K], KA extends string & keyof Ko>(
47+
key: Ko extends string ? K : `${K}.${KA}`,
48+
data: number | { [key: string]: unknown; count?: number } = {},
49+
fallback = `No locale for "${key}" provided`
50+
): string {
51+
// Empty string string if locale doesn't exist
52+
let locale = get(options.locale || {}, key, fallback);
53+
const interpolate = /\{(.+?)\}/g;
54+
const plurals = locale.split("|");
55+
const count = typeof data === "number" ? data : (data?.count ?? -1);
5756

58-
// Pluralization
59-
if (count > -1 && plurals.length > 1) {
60-
if (plurals.length === 2) {
61-
// product, products
62-
locale = plurals[count > 1 ? 1 : 0];
63-
} else if (plurals.length === 3) {
64-
// no products, a product, products
65-
locale = plurals[count ? (count > 1 ? 2 : 1) : 0];
66-
}
57+
// Pluralization
58+
if (count > -1 && plurals.length > 1) {
59+
if (plurals.length === 2) {
60+
// product, products
61+
locale = plurals[count > 1 ? 1 : 0];
62+
} else if (plurals.length === 3) {
63+
// no products, a product, products
64+
locale = plurals[count ? (count > 1 ? 2 : 1) : 0];
6765
}
66+
}
6867

69-
const compile = template(trim(locale), { interpolate });
68+
const compile = template(trim(locale), { interpolate });
7069

71-
return compile(typeof data === "number" ? { count } : data);
72-
},
73-
te<K extends string & keyof L, Ko extends L[K], KA extends string & keyof Ko>(
74-
key: string
75-
): key is Ko extends string ? K : `${K}.${KA}` {
76-
return has(options.locale || {}, key);
77-
},
70+
return compile(typeof data === "number" ? { count } : data);
71+
}
72+
73+
function te<K extends string & keyof L, Ko extends L[K], KA extends string & keyof Ko>(
74+
key: string
75+
): key is Ko extends string ? K : `${K}.${KA}` {
76+
return has(options.locale || {}, key);
77+
}
78+
79+
return {
80+
t,
81+
te,
7882
tet(key: string): string {
79-
return (this.te(key) && this.t(key)) || key;
83+
return (te(key) && t(key)) || key;
8084
},
8185
};
8286
}

packages/common-helpers/src/utils.ts

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -89,40 +89,42 @@ export default function useUtils(options: iPluginOptions = {}): iUseUtils {
8989
console.error(at, "Unknown error", error);
9090
};
9191

92+
function getModifierClasses(values: tPropsModifier, config: igetModifiersArgs = {}): string[] {
93+
if (!values) return [];
94+
95+
const { prefix, modifier, divider } = {
96+
prefix: "",
97+
modifier: "",
98+
divider: "",
99+
// override defaults
100+
...config,
101+
};
102+
const modifierClass = config?.modifierClass || `${prefix}--${modifier}`;
103+
104+
if (Array.isArray(values)) {
105+
return values
106+
.map((value) => {
107+
if (typeof value === "string") return modifierClass + divider + value;
108+
109+
return Object.keys(value).map((key) => {
110+
// validate truthiness
111+
if (!!value[key]) return modifierClass + key;
112+
});
113+
})
114+
.flat(2)
115+
.filter((value): value is string => !!value);
116+
} else if (typeof values === "boolean") {
117+
return [modifierClass];
118+
}
119+
120+
return getModifierClasses([values], { modifierClass });
121+
}
122+
92123
return {
93124
logger: options.logger || logger,
94125
isBrowser,
95126
isTouchDevice,
96-
getModifierClasses(values: tPropsModifier, config: igetModifiersArgs = {}): string[] {
97-
if (!values) return [];
98-
99-
const { prefix, modifier, divider } = {
100-
prefix: "",
101-
modifier: "",
102-
divider: "",
103-
// override defaults
104-
...config,
105-
};
106-
const modifierClass = config?.modifierClass || `${prefix}--${modifier}`;
107-
108-
if (Array.isArray(values)) {
109-
return values
110-
.map((value) => {
111-
if (typeof value === "string") return modifierClass + divider + value;
112-
113-
return Object.keys(value).map((key) => {
114-
// validate truthiness
115-
if (!!value[key]) return modifierClass + key;
116-
});
117-
})
118-
.flat(2)
119-
.filter((value): value is string => !!value);
120-
} else if (typeof values === "boolean") {
121-
return [modifierClass];
122-
}
123-
124-
return this.getModifierClasses([values], { modifierClass });
125-
},
127+
getModifierClasses,
126128
getPropData<T extends string>(prop: tProp<T>, index = 0): T | undefined {
127129
if (typeof prop !== "string") {
128130
const [key, value] = Object.entries(prop)[index];

0 commit comments

Comments
 (0)