Passed
Push — main ( 5dc5e5...0c937b )
by Andrii
02:33
created

src/atom.core.ts   A

Complexity

Total Complexity 11
Complexity/F 3.67

Size

Lines of Code 59
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 51
mnd 8
bc 8
fnc 3
dl 0
loc 59
bpm 2.6666
cpm 3.6666
noi 0
c 0
b 0
f 0
rs 10

3 Functions

Rating   Name   Duplication   Size   Complexity  
B atom.core.ts ➔ atom2arr 0 21 5
A atom.core.ts ➔ value2class 0 9 2
A atom.core.ts ➔ object2classes 0 14 4
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
}