forked from angus-c/just
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
86 lines (78 loc) · 2.78 KB
/
Copy pathindex.js
File metadata and controls
86 lines (78 loc) · 2.78 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
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: 4};
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'), 4));
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(6);
var obj = {a: {aa: {aaa: 2}}, b: 4};
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']), 4));
t.ok(compare(get(obj.a, ['aa']), {aaa: 2}));
t.ok(compare(get(obj.a.aa, ['aaa']), 2));
t.end();
});
test('returns undefined for non-existing properties, using dot-notation arg', function(t) {
t.plan(4);
var obj = {a: {aa: {aaa: 2}}, b: 4};
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.end();
});
test('returns undefined for non-existing properties, using array arg', function(t) {
t.plan(4);
var obj = {a: {aa: {aaa: 2}}, b: 4};
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.end();
});
test('returns undefined for falsey property names using dot notation', function(t) {
t.plan(4);
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, ""'), undefined));
t.end();
});
test('returns undefined for falsey property names using array arg', function(t) {
t.plan(4);
var obj = {a: {aa: {aaa: 2}}, b: {}};
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));
t.ok(compare(get(obj, ['b', '']), undefined));
t.end();
});
test('returns first argument if it is a falsey value', function(t) {
t.plan(2);
t.ok(compare(get(null, 'a'), null));
t.ok(compare(get(undefined, 'a'), undefined));
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();
});
}