-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
test.js
150 lines (123 loc) · 3.89 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
import test from 'ava';
import sortKeys from '.';
function deepEqualInOrder(t, actual, expected) {
t.deepEqual(actual, expected);
const seen = new Set();
function assertSameKeysInOrder(object1, object2) {
// This function assumes the objects given are already deep equal.
if (seen.has(object1) && seen.has(object2)) {
return;
}
seen.add(object1);
seen.add(object2);
if (Array.isArray(object1)) {
for (const index of object1.keys()) {
assertSameKeysInOrder(object1[index], object2[index]);
}
} else if (typeof object1 === 'object') {
const keys1 = Object.keys(object1);
const keys2 = Object.keys(object2);
t.deepEqual(keys1, keys2);
for (const index of keys1.keys()) {
assertSameKeysInOrder(object1[keys1[index]], object2[keys2[index]]);
}
}
}
assertSameKeysInOrder(actual, expected);
}
test('sort the keys of an object', t => {
deepEqualInOrder(t, sortKeys({c: 0, a: 0, b: 0}), {a: 0, b: 0, c: 0});
});
test('custom compare function', t => {
const compare = (a, b) => b.localeCompare(a);
deepEqualInOrder(t, sortKeys({c: 0, a: 0, b: 0}, {compare}), {c: 0, b: 0, a: 0});
});
test('deep option', t => {
deepEqualInOrder(t, sortKeys({c: {c: 0, a: 0, b: 0}, a: 0, b: 0}, {deep: true}), {a: 0, b: 0, c: {a: 0, b: 0, c: 0}});
t.notThrows(() => {
const object = {a: 0};
object.circular = object;
sortKeys(object, {deep: true});
});
const object = {z: 0};
object.circular = object;
const sortedObject = sortKeys(object, {deep: true});
t.is(sortedObject, sortedObject.circular);
t.deepEqual(Object.keys(sortedObject), ['circular', 'z']);
const object1 = {b: 0};
const object2 = {d: 0};
const object3 = {a: [{b: 0}]};
const object4 = {a: [{d: 0}]};
object1.a = object2;
object2.c = object1;
object3.a[0].a = object4.a[0];
object4.a[0].c = object3.a[0];
t.notThrows(() => {
sortKeys(object1, {deep: true});
sortKeys(object2, {deep: true});
sortKeys(object3, {deep: true});
sortKeys(object4, {deep: true});
});
const sorted = sortKeys(object1, {deep: true});
const deepSorted = sortKeys(object3, {deep: true});
t.is(sorted, sorted.a.c);
deepEqualInOrder(t, deepSorted.a[0], deepSorted.a[0].a.c);
t.deepEqual(Object.keys(sorted), ['a', 'b']);
t.deepEqual(Object.keys(deepSorted.a[0]), ['a', 'b']);
deepEqualInOrder(t, sortKeys({c: {c: 0, a: 0, b: 0}, a: 0, b: 0, z: [9, 8, 7, 6, 5]}, {deep: true}), {a: 0, b: 0, c: {a: 0, b: 0, c: 0}, z: [9, 8, 7, 6, 5]});
t.deepEqual(Object.keys(sortKeys({a: [{b: 0, a: 0}]}, {deep: true}).a[0]), ['a', 'b']);
});
test('deep arrays', t => {
const object = {
b: 0,
a: [
{b: 0, a: 0},
[{b: 0, a: 0}]
]
};
object.a.push(object);
object.a[1].push(object.a[1]);
t.notThrows(() => {
sortKeys(object, {deep: true});
});
const sorted = sortKeys(object, {deep: true});
t.is(sorted.a[2], sorted);
t.is(sorted.a[1][1], sorted.a[1]);
t.deepEqual(Object.keys(sorted), ['a', 'b']);
t.deepEqual(Object.keys(sorted.a[0]), ['a', 'b']);
t.deepEqual(Object.keys(sorted.a[1][0]), ['a', 'b']);
});
test('top-level array', t => {
const array = [{b: 0, a: 0}, {c: 0, d: 0}];
const sorted = sortKeys(array);
t.not(sorted, array, 'should make a copy');
t.is(sorted[0], array[0]);
t.is(sorted[1], array[1]);
const deepSorted = sortKeys(array, {deep: true});
t.not(deepSorted, array);
t.not(deepSorted[0], array[0]);
t.not(deepSorted[1], array[1]);
t.deepEqual(Object.keys(deepSorted[0]), ['a', 'b']);
t.deepEqual(Object.keys(deepSorted[1]), ['c', 'd']);
});
test('keeps property descriptors intact', t => {
const descriptors = {
b: {
value: 1,
configurable: true,
enumerable: true,
writable: false
},
a: {
value: 2,
configurable: false,
enumerable: true,
writable: true
}
};
const object = {};
Object.defineProperties(object, descriptors);
const sorted = sortKeys(object);
deepEqualInOrder(t, sorted, {a: 2, b: 1});
t.deepEqual(Object.getOwnPropertyDescriptors(sorted), descriptors);
});