Total Complexity | 11 |
Complexity/F | 3.67 |
Lines of Code | 59 |
Function Count | 3 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import type {AtomInGeneral} from "./atom.types" |
||
2 | import {EMPTY_ARRAY} from "./consts.json" |
||
3 | |||
4 | const {isArray: $isArray} = Array |
||
5 | |||
6 | const selfKey = "_" |
||
7 | , delimiter = "-" |
||
8 | |||
9 | export { |
||
10 | atom2arr |
||
11 | } |
||
12 | |||
13 | function atom2arr(query: AtomInGeneral) { |
||
14 | const classes: string[] = [] |
||
15 | |||
16 | for (const key in query) { |
||
17 | const value = query[key] |
||
18 | |||
19 | if (typeof value !== "object") { |
||
20 | const className = value2class(key, value) |
||
21 | className && classes.push(className) |
||
22 | continue |
||
23 | } |
||
24 | |||
25 | const subValue = value2class(key, $isArray(value) ? value[0] : false) |
||
26 | , subQuery = object2classes(key, $isArray(value) ? value[1] : value) |
||
27 | |||
28 | subValue && classes.push(subValue) |
||
29 | classes.push(...subQuery) |
||
30 | } |
||
31 | |||
32 | return classes |
||
33 | } |
||
34 | |||
35 | function object2classes(rootKey: string, subQuery: undefined|Record<string, boolean|number|string>) { |
||
36 | if (!subQuery) |
||
37 | return EMPTY_ARRAY |
||
38 | |||
39 | const classes: string[] = [] |
||
40 | for (const key in subQuery) { |
||
41 | const subValue = subQuery[key] |
||
42 | , value = key === selfKey |
||
43 | ? subValue |
||
44 | : value2class(key, subValue) |
||
45 | value && classes.push(`${rootKey}${delimiter}${value}`) |
||
46 | } |
||
47 | return classes |
||
48 | } |
||
49 | |||
50 | function value2class(property: string, value: boolean|number|string) { |
||
51 | switch (value) { |
||
52 | case false: |
||
53 | return |
||
54 | case true: |
||
55 | return property |
||
56 | default: |
||
57 | return `${property}${delimiter}${value}` |
||
58 | } |
||
59 | } |