Passed
Push — main ( 393d41...72f619 )
by Andrii
02:07
created

src/ide-renames.test.ts   A

Complexity

Total Complexity 11
Complexity/F 1

Size

Lines of Code 151
Function Count 11

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 105
mnd 0
bc 0
fnc 11
dl 0
loc 151
rs 10
bpm 0
cpm 1
noi 0
c 0
b 0
f 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
A ide-renames.test.ts ➔ keyer 0 2 1
A ide-renames.test.ts ➔ keyShape 0 5 1
A ide-renames.test.ts ➔ Child 0 9 1
A ide-renames.test.ts ➔ keyingCreator 0 4 1
A ide-renames.test.ts ➔ Parent 0 9 1
A ide-renames.test.ts ➔ keying 0 2 1
A ide-renames.test.ts ➔ keyingCtx 0 2 1
1
export {}
2
3
type ClassnamesProp<T extends Record<string, unknown>> = {classnames: T}
4
5
type WithClassNamed<K extends string = string> = {classnames: Record<K, unknown>}
6
interface Keying<T extends WithClassNamed> {
7
  (source: Partial<T["classnames"]>) : string
8
}
9
10
it("+ rename with function generic inheritance", () => {
11
  function keying<T extends WithClassNamed>(source: Partial<T["classnames"]>) {
12
    return Object.keys(source).join(" ")
13
  }
14
15
  function keyingCreator<T extends WithClassNamed>()  {
16
    const lambda: Keying<T> = source => Object.keys(source).join(" ")
17
    return lambda
18
  }
19
  
20
  type ParentProps = ChildProps & ClassnamesProp<{
21
    parent1: string
22
  }>
23
  
24
  function Parent({classnames, classnames: {
25
    parent1,
26
    renamed: child1
27
  }}: ParentProps) {
28
    return `${keying<ParentProps>({
29
      parent1,
30
      renamed: child1
31
    })} ${Child({classnames})}`
32
  }
33
  
34
  type ChildProps = ClassnamesProp<{
35
    renamed: string
36
    child2: string
37
  }>
38
  
39
  function Child({classnames: {
40
    renamed: child1,
41
    child2
42
  }}: ChildProps) {
43
    const k = keyingCreator<ChildProps>()
44
    return k({
45
      renamed: child1,
46
      child2
47
    })
48
  }
49
50
  expect(
51
    Parent({classnames: {}} as ParentProps)
52
  ).toBe(
53
    "parent1 renamed renamed child2"
54
  )
55
})
56
57
it("- rename from bind", () => {
58
  function keyingCtx<K extends string, T extends WithClassNamed<K>>(this: T, source: Partial<T["classnames"]>) {
59
    return Object.keys(source).join(" ")
60
  }
61
62
  type ParentProps = ChildProps & {
63
    classnames: {
64
      parent1: string
65
    }
66
  }
67
  
68
  function Parent(props: ParentProps) {
69
    const k = keyingCtx.bind(props)
70
    , {
71
      parent1,
72
      renamed_whatever: child1
73
    } = props.classnames
74
    
75
    return `${k({
76
      parent1,
77
      renamed: child1
78
    })} ${Child(props)}`
79
  }
80
  
81
  type ChildProps = {
82
    classnames: {
83
      renamed_whatever: string
84
      child2: string
85
    }
86
  }
87
  
88
  function Child(props: ChildProps) {
89
    const k = keyingCtx.bind(props)
90
    , {
91
      renamed_whatever: child1,
92
      child2  
93
    } = props.classnames
94
       
95
    return k({
96
      renamed: child1,
97
      child2
98
    })
99
  }
100
101
  expect(
102
    Parent({classnames: {}} as ParentProps)
103
  ).toBe(
104
    "parent1 renamed renamed child2"
105
  )
106
})
107
108
it("rename keys as string(-) or same shape object(+)", () => {
109
  function keyer<T extends WithClassNamed>(...keys: (keyof T["classnames"])[]) {
110
    return keys.join(" ")
111
  }
112
  
113
  function keyShape<T extends WithClassNamed>(toggler: {[K in keyof T["classnames"]]?: boolean}) {
114
    return Object.keys(toggler)
115
    .filter(key => toggler[key])
116
    .join(" ")
117
  }
118
  
119
120
  type ParentProps = ChildProps & ClassnamesProp<{
121
    parent1: string
122
  }>
123
  
124
  function Parent(props: ParentProps) {
125
    return `${keyShape<ParentProps>({
126
      parent1: true,
127
      renamed: true,
128
      child2: false
129
    })} ${Child(props)}`
130
  }
131
  
132
  type ChildProps = ClassnamesProp<{
133
    renamed: string
134
    child2: string
135
  }>
136
  
137
  function Child(_: ChildProps) {
138
    return keyer<ChildProps>(
139
      //@ts-expect-error
140
      "child1",
141
      "child2"
142
    )
143
  }
144
145
  expect(
146
    Parent({classnames: {}} as ParentProps)
147
  ).toBe(
148
    "parent1 renamed child1 child2"
149
  )  
150
})
151