Skip to content

Commit aac1b1b

Browse files
committed
refactor(string): getClassNames
1 parent fc2bb86 commit aac1b1b

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/string.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { StrTemplate, TupleM2N, ToCamelCase } from '@tool-pack/types';
2+
import { forEachObj } from './object';
23

34
/**
45
* 数字千位分隔
@@ -488,16 +489,17 @@ export function hideString(
488489
export function getClassNames(
489490
...classes: Array<string | Record<string, boolean | null | undefined | number>>
490491
): string {
491-
const handleObjClasses = (obj: Record<string, boolean | null | undefined | number>): string[] => {
492-
return Object.entries(obj).reduce<string[]>((res, [k, v]) => {
493-
if (v) res.push(k);
494-
return res;
495-
}, []);
492+
const classNames: Record<string, boolean> = {};
493+
494+
const handleObjClasses = (obj: Record<string, boolean | null | undefined | number>): void => {
495+
forEachObj(obj, (v, k): void => {
496+
if (v) classNames[k] = true;
497+
});
496498
};
497499

498-
const list: Array<string | string[]> = classes.map((item) =>
499-
typeof item === 'object' ? handleObjClasses(item as any) : item,
500+
classes.forEach((item) =>
501+
typeof item === 'string' ? (classNames[item] = true) : handleObjClasses(item),
500502
);
501503

502-
return [...new Set(list.flat())].join(' ').trim().replace(/\s+/g, ' ');
504+
return Object.keys(classNames).join(' ').trim().replace(/\s+/g, ' ');
503505
}

0 commit comments

Comments
 (0)