-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
133 lines (107 loc) · 4.48 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
var assert = require('assert');
var DeepObject = require('./index');
var get = DeepObject.get;
var set = DeepObject.set;
var testObject = {
a: {
b: {
c: [{
ae: 1
}, {
ae2: ['demoo']
}]
},
d: [1,2,3]
},
'test-abc': {
a: {
b: [[1, 2, 3], [4, 5, 6]]
}
},
arr: [{
a: {
b: 1
}
}],
'abc"de"': 2,
'[abc.de]': 3,
'a.b': 3
}
console.log('testObject = ' + JSON.stringify(testObject, null, 4));
console.log('Test:', "assert.equal(get(testObject, ''), testObject);");
assert.equal(get(testObject, ''), testObject);
console.log('>> Ok\n');
console.log('Test:', "assert.equal(get(testObject), testObject);");
assert.equal(get(testObject), testObject);
console.log('>> Ok\n');
console.log('Test:', "assert.equal(get(testObject, 'a.b.c'), testObject.a.b.c);");
assert.equal(get(testObject, 'a.b.c'), testObject.a.b.c);
console.log('>> Ok\n');
console.log('Test:', "assert.equal(get(testObject, '[\"a.b\"]'), testObject['a.b']);");
assert.equal(get(testObject, '["a.b"]'), testObject['a.b']);
console.log('>> Ok\n');
console.log('Test:', "get(testObject, '[\'abc\"de\"\']'), testObject['abc\"de\"'];");
assert.equal(get(testObject, '[\'abc"de"\']'), testObject['abc"de"']);
console.log('>> Ok\n');
console.log('Test:', "assert.equal(get(testObject, '[\'[abc\"de\"]\']'), testObject['[abc\"de\"]']);");
assert.equal(get(testObject, '[\'[abc"de"]\']'), testObject['[abc"de"]']);
console.log('>> Ok\n');
console.log('Test:', "assert.equal(get(testObject, ''), testObject);");
assert.equal(get(testObject, ''), testObject);
console.log('>> Ok\n');
console.log('Test:', "assert.equal(get(testObject, 'a.d[0]'), testObject.a.d[0]);");
assert.equal(get(testObject, 'a.d[0]'), testObject.a.d[0]);
console.log('>> Ok\n');
console.log('Test:', "assert.equal(get(testObject, ['a', 'd', 0]), testObject.a.d[0]);");
assert.equal(get(testObject, ['a', 'd', 0]), testObject.a.d[0]);
console.log('>> Ok\n');
console.log('Test:', "assert.equal(get(testObject, 'a.b.c[0]'), testObject.a.b.c[0]);");
assert.equal(get(testObject, 'a.b.c[0]'), testObject.a.b.c[0]);
console.log('>> Ok\n');
console.log('Test:', "assert.equal(get(testObject, 'a.b.c[0][\"ae\"]'), testObject.a.b.c[0][\"ae\"]);");
assert.equal(get(testObject, 'a.b.c[0]["ae"]'), testObject.a.b.c[0]["ae"]);
console.log('>> Ok\n');
console.log('Test:', "assert.equal(get(testObject, 'a.b.c[0].ae'), testObject.a.b.c[0].ae);");
assert.equal(get(testObject, 'a.b.c[0].ae'), testObject.a.b.c[0].ae);
console.log('>> Ok\n');
console.log('Test:', "assert.equal(get(testObject, 'a.b.c[1].ae2[0]'), testObject.a.b.c[1].ae2[0]);");
assert.equal(get(testObject, 'a.b.c[1].ae2[0]'), testObject.a.b.c[1].ae2[0]);
console.log('>> Ok\n');
console.log('Test:', "assert.equal(get(testObject, '[\"test-abc\"].a.b[0][2]'), testObject[\"test-abc\"].a.b[0][2]);");
assert.equal(get(testObject, '["test-abc"].a.b[0][2]'), testObject["test-abc"].a.b[0][2]);
console.log('>> Ok\n');
console.log('Test:', "assert.equal(get(testObject.arr, '[0].a.b'), testObject.arr[0].a.b);");
assert.equal(get(testObject.arr, '[0].a.b'), testObject.arr[0].a.b);
console.log('>> Ok\n');
console.log('Test:', "assert.equal(get(testObject.arr, '[0].a.b.d.e'), undefined);");
assert.equal(get(testObject.arr, '[0].a.b.d.e'), undefined);
console.log('>> Ok\n');
console.log('Test:', "set(testObject, '[\"test-abc\"].a.b[0][2]', 5);\nassert.equal(get(testObject, '[\"test-abc\"].a.b[0][2]'), 5);");
set(testObject, '["test-abc"].a.b[0][2]', 5);
assert.equal(get(testObject, '["test-abc"].a.b[0][2]'), 5);
console.log('>> Ok\n');
console.log('Test:', "set(testObject, 'a.b.c[0].ae', 10);\nassert.equal(get(testObject, 'a.b.c[0].ae'), 10);");
set(testObject, 'a.b.c[0].ae', 10);
assert.equal(get(testObject, 'a.b.c[0].ae'), 10);
console.log('>> Ok\n');
console.log('Test:', "set(testObject, 'a.b.c[0].ae.d.e.f.g.h', 10);\nassert.equal(get(testObject, 'a.b.c[0].ae.d.e.f.g.h'), undefined);");
set(testObject, 'a.b.c[0].ae.d.e.f.g.h', 10);
assert.equal(get(testObject, 'a.b.c[0].ae.d.e.f.g.h'), undefined);
console.log('>> Ok\n');
console.log('Test performance');
console.time();
for (var i = 0; i < 10000; i++) {
get(testObject, 'a.b.c');
get(testObject, '[\'abc"de"\']');
get(testObject, '[\'[abc"de"]\']');
get(testObject, 'a.d[0]');
get(testObject, 'a.b.c[0]');
get(testObject, 'a.b.c[0]["ae"]');
get(testObject, 'a.b.c[0].ae');
get(testObject, 'a.b.c[1].ae2[0]');
get(testObject, '["test-abc"].a.b[0][2]');
get(testObject.arr, '[0].a.b');
get(testObject.arr, '[0].a.b.d.e');
}
console.timeEnd();
console.log('Good job');