Passed
Push — main ( 19f8ba...abcd9c )
by Andrii
05:00 queued 02:44
created

src/map.ts   A

Complexity

Total Complexity 5
Complexity/F 2.5

Size

Lines of Code 60
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 44
mnd 3
bc 3
fnc 2
dl 0
loc 60
rs 10
bpm 1.5
cpm 2.5
noi 0
c 0
b 0
f 0
1
import {
2
  CssModule,
3
} from "./definitions.defs";
4
import type {
5
  ClassNamesMapping, ClassNamesMap,
6
} from "./index-types.defs";
7
import {resolver} from "./core"
8
import { AnyObject, OmitIndexed } from "./ts-swiss.defs";
9
import { GetProps } from "./react-swiss.defs";
10
11
const {keys: $keys} = Object
12
13
export {
14
  classNamesMap
15
}
16
17
/**
18
 * Set up mapping classnames function
19
 * @example
20
 * ```typescript
21
 * const mapping = classNamesMap(classnames)
22
 * ```
23
 */
24
function classNamesMap<
25
  Source extends CssModule,
26
>(classnames: Source){
27
  const mapper: ClassNamesMapping<Source> = (target, map) => mapping(classnames, target, map)
28
  return mapper
29
}
30
31
function mapping<
32
  Source extends CssModule,
33
  Target extends AnyObject,
34
  Mapping extends ClassNamesMap<OmitIndexed<GetProps<Target>>, Source>
35
>(
36
  source: Source,
37
  _: Target,
38
  map: Mapping
39
): {[M in keyof Mapping]: string} {
40
  const keys = $keys(map) as (keyof Mapping)[]
41
  , classnames = {} as {[M in keyof Mapping]: string}
42
43
  for (let i = keys.length; i--;) {
44
    const key = keys[i]
45
    , val = map[key]
46
    
47
    if (val === undefined)
48
      continue
49
      
50
    classnames[key] = typeof val === "function"
51
    ? `${val}`
52
    : resolver(source,
53
      //@ts-expect-error #27 TS doesn't understand that ClassNaming is first of all function
54
      val
55
    ).join(" ") 
56
  }
57
58
  return classnames
59
}
60