Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
176 lines (159 loc) · 6 KB
/
Copy pathindex.js
File metadata and controls
176 lines (159 loc) · 6 KB
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
var test = require('../util/test')(__filename);
var get = require('../../packages/object-safe-get');
var compare = require('../../packages/collection-compare');
test('returns existing properties using dot-notation arg', function(t) {
t.plan(6);
var obj = {a: {aa: {aaa: 2}}, b: 0};
t.ok(compare(get(obj, 'a'), {aa: {aaa: 2}}));
t.ok(compare(get(obj, 'a.aa'), {aaa: 2}));
t.ok(compare(get(obj, 'a.aa.aaa'), 2));
t.ok(compare(get(obj, 'b'), 0));
t.ok(compare(get(obj.a, 'aa'), {aaa: 2}));
t.ok(compare(get(obj.a.aa, 'aaa'), 2));
t.end();
});
test('returns existing properties using array arg', function(t) {
t.plan(8);
var obj = {a: {aa: {aaa: 2}}, b: null};
t.ok(compare(get(obj, ['a']), {aa: {aaa: 2}}));
t.ok(compare(get(obj, ['a', 'aa']), {aaa: 2}));
t.ok(compare(get(obj, ['a', 'aa', 'aaa']), 2));
t.ok(compare(get(obj, ['b']), null));
t.ok(compare(get(obj.a, ['aa']), {aaa: 2}));
t.ok(compare(get(obj.a.aa, ['aaa']), 2));
var arr = ['a', 'aa', 'aaa'];
t.ok(compare(get(obj, arr), 2));
t.ok(compare(arr, ['a', 'aa', 'aaa'])); // array arg preserved
t.end();
});
test('returns undefined for non-existing properties, using dot-notation arg', function(t) {
t.plan(6);
var obj = {a: {aa: {aaa: 2}}, b: 4, c: null, d: 0};
t.ok(compare(get(obj, 'b.bb'), undefined));
t.ok(compare(get(obj, 'a.bb'), undefined));
t.ok(compare(get(obj, 'b.bb.bbb'), undefined));
t.ok(compare(get(obj.b, 'bb.bbb'), undefined));
t.ok(compare(get(obj, 'c.cc'), undefined));
t.ok(compare(get(obj, 'd.dd.ddd'), undefined));
t.end();
});
test('returns 3rd param for non-existing properties, using dot-notation arg', function(t) {
t.plan(6);
var obj = {a: {aa: {aaa: 2}}, b: 4, c: null, d: 0};
t.ok(compare(get(obj, 'b.bb', 888), 888));
t.ok(compare(get(obj, 'a.bb', 888), 888));
t.ok(compare(get(obj, 'b.bb.bbb', 888), 888));
t.ok(compare(get(obj.b, 'bb.bbb', 888), 888));
t.ok(compare(get(obj, 'c.cc', 888), 888));
t.ok(compare(get(obj, 'd.dd.ddd', 888), 888));
t.end();
});
test('returns undefined for non-existing properties, using array arg', function(t) {
t.plan(8);
var obj = {a: {aa: {aaa: 2}}, b: 4, c: null, d: 0};
t.ok(compare(get(obj, ['b', 'bb']), undefined));
t.ok(compare(get(obj, ['a', 'bb']), undefined));
t.ok(compare(get(obj, ['b', 'bb', 'bbb']), undefined));
t.ok(compare(get(obj.b, ['bb', 'bbb']), undefined));
t.ok(compare(get(obj, ['c', 'cc']), undefined));
t.ok(compare(get(obj, ['d', 'dd', 'ddd']), undefined));
var arr = ['b', 'bb', 'bbb'];
t.ok(compare(get(obj, arr), undefined));
t.ok(compare(arr, ['b', 'bb', 'bbb'])); // array arg preserved
t.end();
});
test('returns 3rd param for non-existing properties, using array arg', function(t) {
t.plan(8);
var obj = {a: {aa: {aaa: 2}}, b: 4, c: null, d: 0};
t.ok(compare(get(obj, ['b', 'bb'], 888), 888));
t.ok(compare(get(obj, ['a', 'bb'], 888), 888));
t.ok(compare(get(obj, ['b', 'bb', 'bbb'], 888), 888));
t.ok(compare(get(obj.b, ['bb', 'bbb'], 888), 888));
t.ok(compare(get(obj, ['c', 'cc'], 888), 888));
t.ok(compare(get(obj, ['d', 'dd', 'ddd'], 888), 888));
var arr = ['b', 'bb', 'bbb'];
t.ok(compare(get(obj, arr, 888), 888));
t.ok(compare(arr, ['b', 'bb', 'bbb'])); // array arg preserved
t.end();
});
test('returns undefined for falsey property names using dot notation', function(t) {
t.plan(5);
var obj = {a: {aa: {aaa: 2}}, b: {}};
t.ok(compare(get(obj, 'a.'), undefined));
t.ok(compare(get(obj, 'a.aa.aaa.'), undefined));
t.ok(compare(get(obj, 'b.'), undefined));
t.ok(compare(get(obj, 'b..b'), undefined));
t.ok(compare(get(obj, 'b...b'), undefined));
t.end();
});
test('returns 3rd param for falsey property names using dot notation', function(t) {
t.plan(3);
var obj = {a: {aa: {aaa: 2}}, b: {}};
t.ok(compare(get(obj, 'a.', 888), 888));
t.ok(compare(get(obj, 'a.aa.aaa.', 888), 888));
t.ok(compare(get(obj, 'b.', 888), 888));
t.end();
});
test('returns undefined for falsey property names using array arg', function(t) {
t.plan(5);
var obj = {a: {aa: {aaa: 2}}, b: {'': {'': 3}}};
t.ok(compare(get(obj, ['a', false]), undefined));
t.ok(compare(get(obj, ['a', 'aa', 'aaa', null]), undefined));
t.ok(compare(get(obj, ['b', undefined]), undefined));
var arr = ['a', 'aa', 'aaa', null];
t.ok(compare(get(obj, arr), undefined));
t.ok(compare(arr, ['a', 'aa', 'aaa', null])); // array arg preserved
t.end();
});
test('returns 3rd param for falsey property names using array arg', function(t) {
t.plan(5);
var obj = {a: {aa: {aaa: 2}}, b: {'': {'': 3}}};
t.ok(compare(get(obj, ['a', false], 888), 888));
t.ok(compare(get(obj, ['a', 'aa', 'aaa', null], 888), 888));
t.ok(compare(get(obj, ['b', undefined], 888), 888));
var arr = ['a', 'aa', 'aaa', null];
t.ok(compare(get(obj, arr, 888), 888));
t.ok(compare(arr, ['a', 'aa', 'aaa', null])); // array arg preserved
t.end();
});
test('follows empty keys using array arg', function(t) {
t.plan(2);
var obj = {b: {'': {'': 3}}};
t.ok(compare(get(obj, ['b', '']), {'': 3}));
t.ok(compare(get(obj, ['b', '', '']), 3));
t.end();
});
test('returns undefined if first argument is a falsey value', function(t) {
t.plan(2);
t.ok(compare(get(null, 'a'), undefined));
t.ok(compare(get(undefined, 'a'), undefined));
t.end();
});
test('returns 3rd argument if first argument is a falsey value', function(t) {
t.plan(2);
t.ok(compare(get(null, 'a', 888), 888));
t.ok(compare(get(undefined, 'a', 888), 888));
t.end();
});
test('does not mutate path', function(t) {
t.plan(4);
var obj1 = {a: {ab: {aaa: 2}}, b: null};
var obj2 = {a: {aa: {aaa: 2}}, b: null};
var path = ['a', 'aa', 'aaa'];
t.ok(compare(get(obj1, path), undefined));
t.ok(compare(path, ['a', 'aa', 'aaa']));
t.ok(compare(get(obj2, path), 2));
t.ok(compare(path, ['a', 'aa', 'aaa']));
t.end();
});
/* eslint-disable no-undef*/
if (typeof Symbol === 'function') {
test('works with symbols', function(t) {
t.plan(1);
var obj = {a: {}};
var sym = Symbol();
obj.a[sym] = 4;
t.ok(compare(get(obj.a, sym), 4));
t.end();
});
}