mm
This commit is contained in:
42
packages/css/utils/cpm.ts
Normal file
42
packages/css/utils/cpm.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { defaultPrefixList } from "./prefix-list";
|
||||
|
||||
function prefixParce(cls: string): { prefix: string; rest: string } {
|
||||
const index = cls.indexOf("-");
|
||||
if (index === -1) return { prefix: "", rest: cls };
|
||||
const prefix = cls.slice(0, index);
|
||||
const rest = cls.slice(index + 1);
|
||||
return { prefix, rest };
|
||||
}
|
||||
|
||||
export function cpm(
|
||||
options?: { externalPrefixList?: string[] },
|
||||
...classes: (string | string[])[]
|
||||
): string {
|
||||
const map = new Map<string, string>();
|
||||
const mergedPrefixList: string[] = [
|
||||
...new Set([...(options?.externalPrefixList ?? []), ...defaultPrefixList]),
|
||||
];
|
||||
|
||||
classes.forEach((item) => {
|
||||
if (Array.isArray(item)) {
|
||||
item.forEach((i) => {
|
||||
const { prefix, rest } = prefixParce(i);
|
||||
if (mergedPrefixList.includes(prefix)) {
|
||||
map.set(prefix, rest);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof item === "string") {
|
||||
const { prefix, rest } = prefixParce(item);
|
||||
if (mergedPrefixList.includes(prefix)) {
|
||||
map.set(prefix, rest);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return Array.from(map.entries())
|
||||
.map(([prefix, rest]) => `${prefix}-${rest}`)
|
||||
.join(" ");
|
||||
}
|
||||
Reference in New Issue
Block a user