-
Notifications
You must be signed in to change notification settings - Fork 9
/
test.js
77 lines (67 loc) · 1.96 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
'use strict';
var assert = require('assert');
var keys = require('./');
//o1, o2, msg
var expectEqual = assert.deepEqual;
describe('deep-keys', function() {
it('should return array composed of it\'s properties names', function() {
expectEqual(keys({ a:1, b:2, c:3, d:4 }), ['a', 'b', 'c', 'd']);
expectEqual(keys({}), []);
});
it('should return owned properties', function() {
var obj = {
a: { b: 1, c: 2 }
};
expectEqual(keys(obj.a), ['b', 'c']);
});
it('should return deep keys', function() {
var obj1 = {
a: 1,
b: { c: 1 },
c: { d: { e: 1 }, f: 1 },
d: { e: { f: { g: 1, h: 2 } } },
e: 2,
f: { g: [] }
};
expectEqual(keys(obj1), ['a', 'b.c', 'c.d.e', 'c.f', 'd.e.f.g', 'd.e.f.h', 'e', 'f.g']);
var obj2 = {
type: 'customer',
details: {
name: 'Ariel', age: 26, address: { city: 'Tel Aviv', country: 'Israel' }
},
isActive: true,
createdAt: new Date()
};
expectEqual(keys(obj2), [
'type',
'details.name',
'details.age',
'details.address.city',
'details.address.country',
'isActive',
'createdAt'
]);
});
it('should return deep keys including intermediate parent keys', function() {
var obj1 = {
a: 1,
b: { c: 1 },
c: { d: { e: 1 }, f: 1 },
d: { e: { f: { g: 1, h: 2 } } },
e: 2,
f: { g: [] }
};
expectEqual(keys(obj1, true), ['a', 'b', 'b.c', 'c', 'c.d', 'c.d.e', 'c.f',
'd', 'd.e', 'd.e.f', 'd.e.f.g', 'd.e.f.h', 'e', 'f', 'f.g']);
});
it('should escape . in key names', function() {
var obj1 = { a: { '.b': 1 } };
expectEqual(keys(obj1), ['a.\\.b']);
var obj2 = { 'a.': { b: 1 } };
expectEqual(keys(obj2, true), ['a\\.', 'a\\..b']);
var obj3 = { a: { 'b.d': { c: 1 } } };
expectEqual(keys(obj3), ['a.b\\.d.c']);
var obj4 = { a: { 'b.d': { c: 1 } } };
expectEqual(keys(obj4, true), ['a','a.b\\.d', 'a.b\\.d.c']);
});
});