Files
defgov/packages/css/utils/cpm.ts
2026-05-07 02:15:20 +08:00

43 lines
1.1 KiB
TypeScript

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(" ");
}