|
| 1 | +import type { CSSJSObj, GetParseCaseFunction } from "./type"; |
| 2 | +import { collectionToObj } from "./util"; |
| 3 | + |
| 4 | +const importRe = new RegExp(/^(@import)/); |
| 5 | +const keySeparatorRe = new RegExp(/(?=[\s.:[\]><+,()])/g); |
| 6 | + |
1 | 7 | export const extractClassNameKeys = ( |
2 | | - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
3 | | - obj: Record<string, any> |
| 8 | + obj: CSSJSObj, |
| 9 | + toParseCase: GetParseCaseFunction, |
| 10 | + parentKey?: string |
4 | 11 | ): Map<string, boolean> => { |
5 | 12 | return Object.entries(obj).reduce<Map<string, boolean>>( |
6 | 13 | (curr, [key, value]) => { |
7 | | - const reg = new RegExp(/^(@media)/g); |
8 | | - if (reg.test(key)) return curr; |
9 | | - const splittedKeys = key.split(/(?=[\s.:[\]><+,()])/g); |
10 | | - for (const splittedKey of splittedKeys) { |
11 | | - if (splittedKey.startsWith(".")) { |
12 | | - curr.set(splittedKey.replace(".", "").trim(), true); |
| 14 | + if (importRe.test(key)) return curr; |
| 15 | + const splitKeys = key.split(keySeparatorRe); |
| 16 | + for (const splitKey of splitKeys) { |
| 17 | + if (parentKey === ":export" || splitKey.startsWith(".")) { |
| 18 | + if (toParseCase) { |
| 19 | + curr.set(toParseCase(splitKey.replace(".", "").trim()), true); |
| 20 | + } else { |
| 21 | + curr.set(splitKey.replace(".", "").trim(), true); |
| 22 | + } |
13 | 23 | } |
14 | 24 | } |
15 | 25 |
|
16 | 26 | if (typeof value === "object" && Object.keys(value).length > 0) { |
17 | | - const map = extractClassNameKeys(value); |
| 27 | + const valueToExtract = Array.isArray(value) |
| 28 | + ? collectionToObj(value) |
| 29 | + : value; |
| 30 | + const map = extractClassNameKeys(valueToExtract, toParseCase, key); |
| 31 | + |
18 | 32 | for (const key of map.keys()) { |
19 | | - if (key.startsWith(".")) { |
| 33 | + if (toParseCase) { |
| 34 | + curr.set(toParseCase(key), true); |
| 35 | + } else { |
20 | 36 | curr.set(key, true); |
21 | 37 | } |
22 | 38 | } |
|
0 commit comments