Total Complexity | 11 |
Complexity/F | 3.67 |
Lines of Code | 71 |
Function Count | 3 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import type { BemInGeneral } from "./bem.types" |
||
2 | |||
3 | const {isArray: $isArray} = Array |
||
4 | |||
5 | let modDelimiter = "--" |
||
6 | , elementDelimiter = "__" |
||
7 | |||
8 | export type BemOptions = { |
||
9 | elementDelimiter: string |
||
10 | modDelimiter: string |
||
11 | } |
||
12 | |||
13 | export { |
||
14 | bem2arr, |
||
15 | setOptions, |
||
16 | getOptions |
||
17 | } |
||
18 | |||
19 | function bem2arr(query: BemInGeneral) { |
||
20 | const $return: string[] = [] |
||
21 | |||
22 | for (const base in query) { |
||
23 | const baseQ = query[base] |
||
24 | $return.push(base) |
||
25 | |||
26 | if (!baseQ) |
||
27 | continue |
||
28 | if (typeof baseQ !== "object") { |
||
29 | if (typeof baseQ === "string") |
||
30 | $return.push(`${base}${modDelimiter}${baseQ}`) |
||
31 | continue |
||
32 | } |
||
33 | |||
34 | const isArray = $isArray(baseQ) |
||
35 | |||
36 | // TODO check performance of `const in Array` |
||
37 | for (const mod in baseQ) { |
||
38 | //@ts-expect-error //TODO Split Array and Object? |
||
39 | const modValue = baseQ[mod] |
||
40 | if (!modValue) |
||
41 | continue |
||
42 | |||
43 | $return.push(`${base}${ |
||
44 | isArray |
||
45 | ? "" |
||
46 | : `${modDelimiter}${mod}` |
||
47 | }${ |
||
48 | typeof modValue !== "string" |
||
49 | ? "" |
||
50 | : `${modDelimiter}${modValue}` |
||
51 | }`) |
||
52 | } |
||
53 | } |
||
54 | |||
55 | return $return |
||
56 | } |
||
57 | |||
58 | function setOptions({ |
||
59 | elementDelimiter: elD = elementDelimiter, |
||
60 | modDelimiter: modDel = modDelimiter |
||
61 | }: Partial<BemOptions>) { |
||
62 | modDelimiter = modDel |
||
63 | elementDelimiter = elD |
||
64 | } |
||
65 | |||
66 | function getOptions() { |
||
67 | return { |
||
68 | elementDelimiter, |
||
69 | modDelimiter, |
||
70 | } |
||
71 | } |