-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.test.ts
78 lines (68 loc) · 1.4 KB
/
get.test.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
import { test } from 'uvu'
import * as assert from 'uvu/assert'
import { get } from '../src/get'
test('get short path', () => {
assert.equal(get({ a: 1 }, 'a'), 1)
})
test('get long path', () => {
const source = {
a: {
b: {
c: {
d: { e: 1 },
},
},
},
}
assert.equal(get(source, 'a.b.c.d'), { e: 1 })
})
test('array paths', () => {
const source = {
a: {
b: [{ c: 1 }],
},
b: {
c: [0, 1],
},
}
assert.equal(get(source, 'a.b.[0].c'), 1)
assert.equal(get(source, 'b.c.[0]'), 0)
})
test('get key over path', () => {
const source = {
'a.b': 1,
'a': {
b: [{ c: 1 }],
},
}
assert.equal(get(source, 'a.b'), 1)
})
test('access empty keys', () => {
const source = { a: { '': 1 } }
assert.equal(get(source, 'a.[]'), 1)
})
test('handle complex paths', () => {
const source = {
a: {
'1.32': { '["b"]': { c: { "['d']": { '\ne\n': { f: { g: 8 } } } } } },
},
}
assert.equal(get(source, 'a.1..32.["b"].c.[\'d\'].\ne\n.f.g'), 8)
})
test('undefined on nullish object', () => {
const source = null
assert.equal(get(source, 'a'), undefined)
})
test('deep path is nullish', () => {
const source = null
assert.equal(get(source, 'a.b.c'), undefined)
})
test('return null from path', () => {
const source = {
a: {
b: null,
},
}
assert.equal(get(source, 'a.b'), null)
})
test.run()