Passed
Push — main ( 20c980...d65141 )
by Andrii
02:02
created

__sandbox__/ide-renames.test.ts   A

Complexity

Total Complexity 11
Complexity/F 1

Size

Lines of Code 154
Function Count 11

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 106
mnd 0
bc 0
fnc 11
dl 0
loc 154
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 ➔ keyingCtx 0 2 1
A ide-renames.test.ts ➔ keying 0 2 1
A ide-renames.test.ts ➔ keyingCreator 0 4 1
A ide-renames.test.ts ➔ Parent 0 9 1
1
// import { Component } from "react"
2
// import type { ClassHash, ClassNames } from "../src/defs"
3
4
export {}
5
6
type ClassnamesProp<T extends Record<string, unknown>> = {classnames: T}
7
8
type WithClassNamed<K extends string = string> = {classnames: Record<K, unknown>}
9
interface Keying<T extends WithClassNamed> {
10
  (source: Partial<T["classnames"]>) : string
11
}
12
13
it("+ rename with function generic inheritance", () => {
14
  function keying<T extends WithClassNamed>(source: Partial<T["classnames"]>) {
15
    return Object.keys(source).join(" ")
16
  }
17
18
  function keyingCreator<T extends WithClassNamed>()  {
19
    const lambda: Keying<T> = source => Object.keys(source).join(" ")
20
    return lambda
21
  }
22
23
  type ParentProps = ChildProps & ClassnamesProp<{
24
    parent1: string
25
  }>
26
27
  function Parent({classnames, classnames: {
28
    parent1,
29
    renamed: child1
30
  }}: ParentProps) {
31
    return `${keying<ParentProps>({
32
      parent1,
33
      renamed: child1
34
    })} ${Child({classnames})}`
35
  }
36
37
  type ChildProps = ClassnamesProp<{
38
    renamed: string
39
    child2: string
40
  }>
41
42
  function Child({classnames: {
43
    renamed: child1,
44
    child2
45
  }}: ChildProps) {
46
    const k = keyingCreator<ChildProps>()
47
    return k({
48
      renamed: child1,
49
      child2
50
    })
51
  }
52
53
  expect(
54
    Parent({classnames: {}} as ParentProps)
55
  ).toBe(
56
    "parent1 renamed renamed child2"
57
  )
58
})
59
60
it("- rename from bind", () => {
61
  function keyingCtx<K extends string, T extends WithClassNamed<K>>(this: T, source: Partial<T["classnames"]>) {
62
    return Object.keys(source).join(" ")
63
  }
64
65
  type ParentProps = ChildProps & {
66
    classnames: {
67
      parent1: string
68
    }
69
  }
70
71
  function Parent(props: ParentProps) {
72
    const k = keyingCtx.bind(props)
73
    , {
74
      parent1,
75
      renamed_whatever: child1
76
    } = props.classnames
77
78
    return `${k({
79
      parent1,
80
      renamed: child1
81
    })} ${Child(props)}`
82
  }
83
84
  type ChildProps = {
85
    classnames: {
86
      renamed_whatever: string
87
      child2: string
88
    }
89
  }
90
91
  function Child(props: ChildProps) {
92
    const k = keyingCtx.bind(props)
93
    , {
94
      renamed_whatever: child1,
95
      child2
96
    } = props.classnames
97
98
    return k({
99
      renamed: child1,
100
      child2
101
    })
102
  }
103
104
  expect(
105
    Parent({classnames: {}} as ParentProps)
106
  ).toBe(
107
    "parent1 renamed renamed child2"
108
  )
109
})
110
111
it("rename keys as string(-) or same shape object(+)", () => {
112
  function keyer<T extends WithClassNamed>(...keys: (keyof T["classnames"])[]) {
113
    return keys.join(" ")
114
  }
115
116
  function keyShape<T extends WithClassNamed>(toggler: {[K in keyof T["classnames"]]?: boolean}) {
117
    return Object.keys(toggler)
118
    .filter(key => toggler[key])
119
    .join(" ")
120
  }
121
122
123
  type ParentProps = ChildProps & ClassnamesProp<{
124
    parent1: string
125
  }>
126
127
  function Parent(props: ParentProps) {
128
    return `${keyShape<ParentProps>({
129
      parent1: true,
130
      renamed: true,
131
      child2: false
132
    })} ${Child(props)}`
133
  }
134
135
  type ChildProps = ClassnamesProp<{
136
    renamed: string
137
    child2: string
138
  }>
139
140
  function Child(_: ChildProps) {
141
    return keyer<ChildProps>(
142
      //@ts-expect-error
143
      "child1",
144
      "child2"
145
    )
146
  }
147
148
  expect(
149
    Parent({classnames: {}} as ParentProps)
150
  ).toBe(
151
    "parent1 renamed child1 child2"
152
  )
153
})
154
155
// it("rename throw ts expressions", () => {
156
//   type Props = ClassnamesProp<{
157
//     prop1: ClassHash
158
//     prop2: ClassHash
159
//   }>
160
161
//   function Func1(_:ClassnamesProp<{
162
//     fn1: ClassHash
163
//     fn2: ClassHash
164
//   }>) {
165
//     return null
166
//   }
167
168
//   class Comp1 extends Component<ClassnamesProp<{
169
//     class1: ClassHash
170
//     class2: ClassHash
171
//   }>> {}
172
173
//   const flatCollected
174
//   : ClassNames<Props, typeof Func1, typeof Comp1> = {classnames: {
175
//   // : (GetProps<Props> & GetProps<typeof Func1> & GetProps<typeof Comp1>)["classnames"] = {
176
//     class1: undefined,
177
//     class2: undefined,
178
//     fn1: undefined,
179
//     fn2: undefined,
180
//     prop1: undefined,
181
//     prop2: undefined,
182
//   }}
183
184
//   expect(Object.keys(flatCollected).join(" ")).toBe(
185
//     "class1 class2 fn1 fn2 prop1 prop2"
186
//   )
187
// })
188