forked from eighttrigrams/tsfun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clone.spec.ts
142 lines (88 loc) · 2.57 KB
/
clone.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { clone, isArray, isObject } from '../..'
/**
* tsfun | clone
*/
describe('clone', () => {
it('clone shallow Object', () => {
const k = clone({a: '1', b: 2})
expect(k['a']).toBe('1') // string
expect(k['b']).toBe(2) // number
})
it('clone shallow Array', () => {
const k = clone(['1', 2])
expect(k[0]).toBe('1') // string
expect(k[1]).toBe(2) // number
})
it('clone string', () => {
const k = clone('1')
expect(k).toBe('1')
})
it('clone boolean', () => {
const t = clone(true)
expect(t).toBe(true)
const f = clone(false)
expect(f).toBe(false)
})
it('clone number', () => {
const k = clone(1)
expect(k).toBe(1)
})
it('clone undefined', () => {
const k = clone(undefined)
expect(k).toBe(undefined)
})
it('clone null', () => {
const k = clone(null)
expect(k).toBe(null)
})
it('clone empty Array', () => {
const k = clone([])
expect(k.length).toBe(0)
expect(isArray(k)).toBe(true)
})
it('clone empty Object', () => {
const k = clone({})
expect(Object.keys(k).length).toBe(0)
expect(isObject(k)).toBe(true)
})
it('undefined Array item', () => {
const k = clone([undefined])
expect(k.length).toBe(1)
expect(k[0]).toBe(undefined)
})
it('undefined Object item', () => {
const k = clone({a: undefined})
expect(Object.keys(k).length).toBe(1)
expect(k['a']).toBe(undefined)
})
it('clone nested Object', () => {
const nested = {c: 3}
const k = clone({a: '1', b: nested})
expect(k['b']['c']).toBe(3)
expect(k['b']).not.toBe(nested)
})
it('clone nested Array', () => {
const nested = [3]
const k = clone([nested])
expect(k[0][0]).toBe(3)
expect(k[0]).not.toBe(nested)
})
it('clone mutually nested Array', () => {
const nested = [3]
const k = clone([{a: nested}])
expect(k[0]['a'][0]).toBe(3)
expect(k[0]['a']).not.toBe(nested)
})
it('clone mutually nested Object', () => {
const nested = {c: 3}
const k = clone({a: [nested]})
expect(k['a'][0]['c']).toBe(3)
expect(k['a'][0]).not.toBe(nested)
})
it('clone function', () => {
expect(() => clone((() => {}) as any)).toThrow()
})
it('clone class instance', () => {
expect(() => clone(new Date() as any)).toThrow()
})
})