-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex2.test.js
More file actions
33 lines (26 loc) · 704 Bytes
/
ex2.test.js
File metadata and controls
33 lines (26 loc) · 704 Bytes
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
'use strict';
const { orderedEntries } = require('./ex2');
function assert(condition, message) {
if (!condition) throw new Error(message || 'Assertion failed');
}
const obj = {};
obj.b = 1;
obj[2] = 2;
obj.a = 3;
obj[1] = 4;
const keys = orderedEntries(obj).map(([k]) => k);
assert(
JSON.stringify(keys) === JSON.stringify(['1', '2', 'b', 'a']),
'object iteration order must place integer-like keys first'
);
const map = new Map();
map.set('b', 1);
map.set(2, 2);
map.set('a', 3);
map.set(1, 4);
const mapKeys = orderedEntries(map).map(([k]) => k);
assert(
JSON.stringify(mapKeys) === JSON.stringify(['b', 2, 'a', 1]),
'map preserves insertion order'
);
console.log('ex2 tests passed');