-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
211 lines (171 loc) · 7.45 KB
/
test.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'use strict'
const assert = require('assert')
const get = require('.')
describe('get()', function () {
it('should fetch Array value using single index', function () {
assert.strictEqual(get(['a', 'b'], 1), 'b')
})
it('should fetch Array value using index chain', function () {
assert.strictEqual(get([['value']], [0, 0]), 'value')
})
it('should fetch Array value using index chain string', function () {
assert.strictEqual(get([['value']], '0.0', {split: true, numerifyIndexes: true}), 'value')
})
it('should fetch Object value using single key', function () {
assert.strictEqual(get({a: 1}, 'a'), 1)
})
it('should fetch Object value using key chain', function () {
assert.strictEqual(get({a: {b: 1}}, ['a', 'b']), 1)
})
it('should fetch Object value using key chain string', function () {
assert.strictEqual(get({a: {b: 1}}, 'a.b', {split: true}), 1)
})
it('should fetch inherited Object value if `inObj` is true', function () {
class Cls {
get key () { return 'value' }
}
assert.strictEqual(typeof get(new Cls(), 'key'), 'undefined')
assert.strictEqual(get(new Cls(), 'key', {inObj: true}), 'value')
})
it('should fetch Map value using single key', function () {
assert.strictEqual(get(new Map([['a', 1]]), 'a'), 1)
})
it('should fetch Map value using key chain', function () {
assert.strictEqual(get(new Map([['one', new Map([['two', 'value']])]]), ['one', 'two']), 'value')
})
it('should fetch Map value using key chain string', function () {
assert.strictEqual(get(new Map([['one', new Map([['two', 'value']])]]), 'one.two', {split: true}), 'value')
})
it('should fetch Set value using single index', function () {
assert.strictEqual(get(new Set(['a', 'b']), 1), 'b')
})
it('should fetch String character using single index', function () {
assert.strictEqual(get('test', 1), 'e')
})
it('should fetch WeakMap value using single key', function () {
const key = {}
assert.strictEqual(get(new WeakMap([[key, 'value']]), key), 'value')
})
it('should support mixed nested collection types', function () {
const map = new Map()
map.set('mapKey', {objKey: 'string'})
assert.strictEqual(get(map, ['mapKey', 'objKey', 5]), 'g')
})
it('should fetch value using equivalent key if `loose` is true', function () {
const map = new Map([[{key: true}, 'value']])
assert.strictEqual(typeof get(map, {key: true}), 'undefined')
assert.strictEqual(get(map, {key: true}, {loose: true}), 'value')
})
it('should support the bind operator', function () {
assert.strictEqual(get.call({a: 1}, 'a'), 1)
})
describe('#in()', function () {
it('should fetch inherited Object value', function () {
class Cls {
get key () { return 'value' }
}
assert.strictEqual(typeof get(new Cls(), 'key'), 'undefined')
assert.strictEqual(get.in(new Cls(), 'key'), 'value')
})
})
describe('#any()', function () {
it('should fetch Object value using any key', function () {
assert.strictEqual(get.any({yes: {sub: 1}}, ['no', ['yes', 'sub']]), 1)
})
describe('#in()', function () {
it('should fetch inherited Object value using any key', function () {
class Cls {
get key () { return 'value' }
}
assert.strictEqual(get.any.in(new Cls(), ['notAKey', 'key']), 'value')
})
})
})
describe('#key()', function () {
it('should return a Map key as-is if it exists', function () {
const key = ['key']
assert.strictEqual(get.key(new Map([[key, 'value']]), key), key)
})
it('should return an Object key as-is if it exists', function () {
assert.strictEqual(get.key({key: 'value'}, 'key'), 'key')
})
it('should return undefined if the Map key does not exist', function () {
assert.strictEqual(typeof get.key(new Map(), {}), 'undefined')
})
it('should return undefined if the Object key does not exist', function () {
assert.strictEqual(typeof get.key({}, 'key'), 'undefined')
})
it('should return an equivalent Map key if `loose` is true', function () {
const a = ['key']
const b = ['key']
assert.strictEqual(get.key(new Map([[a, 1]]), b, {loose: true}), a)
})
it('should return an equivalent Object key if `loose` is true', function () {
const obj = {a: 1}
assert.strictEqual(get.key(obj, 'b', {loose: true, looselyEquals: () => true}), 'a')
})
it('should return the first equivalent Map key if `loose` is true', function () {
const a = ['key']
const b = ['key']
const map = new Map([[a, 'a'], [b, 'b']])
assert.strictEqual(get.key(map, b, {loose: true}), a)
})
it('should return the last equivalent Map key if `loose` and `reverse` are true', function () {
const a = ['key']
const b = ['key']
const map = new Map([[a, 'a'], [b, 'b']])
assert.strictEqual(get.key(map, a, {loose: true, reverse: true}), b)
})
it('should return the first equivalent Object key if `loose` is true', function () {
const obj = {a: 1, b: 2}
assert.strictEqual(get.key(obj, 'b', {loose: true, looselyEquals: () => true}), 'a')
})
it('should return the last equivalent Object key if `loose` and `reverse` are true', function () {
const obj = {a: 1, b: 2}
assert.strictEqual(get.key(obj, 'a', {loose: true, looselyEquals: () => true, reverse: true}), 'b')
})
it('should favor strictly-identical Map keys if `preferStrict` is true', function () {
const a = ['key']
const b = ['key']
const map = new Map([[a, 'a'], [b, 'b']])
assert.strictEqual(get.key(map, b, {loose: true}), a)
assert.strictEqual(get.key(map, b, {loose: true, preferStrict: true}), b)
})
it('should favor strictly-identical Object keys if `preferStrict` is true', function () {
const obj = {a: 1, b: 2}
const looselyEquals = () => true
assert.strictEqual(get.key(obj, 'b', {loose: true, looselyEquals}), 'a')
assert.strictEqual(get.key(obj, 'b', {loose: true, looselyEquals, preferStrict: true}), 'b')
})
it('should support the bind operator', function () {
assert.strictEqual(get.key.call({key: 'value'}, 'key'), 'key')
})
})
describe('#entry()', function () {
it('should return a Map entry if the key exists', function () {
const key = ['key']
assert.deepStrictEqual(get.entry(new Map([[key, 'value']]), key), [key, 'value'])
})
it('should return an Object entry if the key exists', function () {
assert.deepStrictEqual(get.entry({key: 'value'}, 'key'), ['key', 'value'])
})
it('should return undefined if the Map key does not exist', function () {
assert.strictEqual(typeof get.entry(new Map(), {}), 'undefined')
})
it('should return undefined if the Object key does not exist', function () {
assert.strictEqual(typeof get.entry({}, 'key'), 'undefined')
})
it('should return an equivalent Map key if `loose` is true', function () {
const a = ['key']
const b = ['key']
assert.deepStrictEqual(get.entry(new Map([[a, 1]]), b, {loose: true}), [a, 1])
})
it('should return an equivalent Object key if `loose` is true', function () {
const obj = {a: 1}
assert.deepStrictEqual(get.entry(obj, 'b', {loose: true, looselyEquals: () => true}), ['a', 1])
})
it('should support the bind operator', function () {
assert.deepStrictEqual(get.entry.call({key: 'value'}, 'key'), ['key', 'value'])
})
})
})