forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array-groupBy.js
162 lines (112 loc) · 6.59 KB
/
array-groupBy.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
151
152
153
154
155
156
157
158
159
160
161
162
//@ requireOptions("--useArrayGroupMethod=1")
function shouldBe(actual, expected) {
if (actual !== expected)
throw new Error(`FAIL: expected '${expected}' actual '${actual}'`);
}
function shouldBeObject(actual, expected) {
function replacer(key, value) {
if (value === undefined)
return "%undefined%";
return value;
}
let actualJSON = JSON.stringify(actual, replacer).replaceAll("\"%undefined%\"", "undefined");
let expectedJSON = JSON.stringify(expected, replacer).replaceAll("\"%undefined%\"", "undefined");
shouldBe(actualJSON, expectedJSON);
}
function notReached() {
throw new Error("should not reach here");
}
// Helper variables
Array.prototype.__test = 42;
let symbol = Symbol("symbol");
let sparseArrayLength = 6;
let mixPartialAndFast = new Array(sparseArrayLength);
mixPartialAndFast[sparseArrayLength - 1] = sparseArrayLength - 1;
for(let i = 0; i < 3; ++i)
mixPartialAndFast[i] = i;
function toObject(array) {
let result = {};
result.length = array.length;
for (let i in array)
result[i] = array[i];
result.group = Array.prototype.group;
return result;
}
function reverseInsertionOrder(array) {
let obj = toObject(array);
let props = [];
for (let i in obj)
props.push(i);
let result = {};
for (let i = props.length - 1; i >= 0; i--)
result[props[i]] = obj[props[i]];
result.group = Array.prototype.group;
return result;
}
let objectWithToStringThatThrows = {
toString: notReached,
};
let objectWithValueOfThatThrows = {
valueOf: notReached,
};
// Basic
shouldBe(Array.prototype.group.length, 1);
shouldBe(Array.prototype.group.name, "group");
shouldBeObject([undefined].group((x) => x === undefined ? "a" : "b"), {"a": [undefined]});
shouldBeObject([undefined].group((x) => x === undefined), {"true": [undefined]});
shouldBeObject((new Array(4)).group((x) => x === undefined ? "a" : "b"), {"a": [undefined, undefined, undefined, undefined]});
shouldBeObject((new Array(4)).group((x) => x === undefined), {"true": [undefined, undefined, undefined, undefined]});
shouldBeObject([0, 1, 2, 3].group((x) => !(x & 1) ? "a" : "b"), {"a": [0, 2], "b": [1, 3]});
shouldBeObject([0, 1, 2, 3].group((x) => !(x & 1)), {"true": [0, 2], "false": [1, 3]});
shouldBeObject([0, 1, 2, 3].group((x, i) => i >= 2 ? "a" : "b"), {"b": [0, 1], "a": [2, 3]});
shouldBeObject([0, 1, 2, 3].group((x, i) => i >= 2), {"false": [0, 1], "true": [2, 3]});
shouldBeObject(mixPartialAndFast.group((x, i) => i >= 2 ? "a" : "b"), {"b": [0, 1], "a": [2, undefined, undefined, sparseArrayLength - 1]});
shouldBeObject(mixPartialAndFast.group((x, i) => i >= 2), {"false": [0, 1], "true": [2, undefined, undefined, sparseArrayLength - 1]});
// Generic Object
shouldBeObject(toObject([undefined]).group((x) => x === undefined ? "a" : "b"), {"a": [undefined]});
shouldBeObject(toObject([undefined]).group((x) => x === undefined), {"true": [undefined]});
shouldBeObject(toObject(new Array(4)).group((x) => x === undefined ? "a" : "b"), {"a": [undefined, undefined, undefined, undefined]});
shouldBeObject(toObject(new Array(4)).group((x) => x === undefined), {"true": [undefined, undefined, undefined, undefined]});
shouldBeObject(toObject([0, 1, 2, 3]).group((x) => !(x & 1) ? "a" : "b"), {"a": [0, 2], "b": [1, 3]});
shouldBeObject(toObject([0, 1, 2, 3]).group((x) => !(x & 1)), {"true": [0, 2], "false": [1, 3]});
shouldBeObject(toObject([0, 1, 2, 3]).group((x, i) => i >= 2 ? "a" : "b"), {"b": [0, 1], "a": [2, 3]});
shouldBeObject(toObject([0, 1, 2, 3]).group((x, i) => i >= 2), {"false": [0, 1], "true": [2, 3]});
shouldBeObject(toObject(mixPartialAndFast).group((x, i) => i >= 2 ? "a" : "b"), {"b": [0, 1], "a": [2, undefined, undefined, sparseArrayLength - 1]});
shouldBeObject(toObject(mixPartialAndFast).group((x, i) => i >= 2), {"false": [0, 1], "true": [2, undefined, undefined, sparseArrayLength - 1]});
// Array-like object with invalid lengths
shouldBeObject(Array.prototype.group.call({ 0: 0, 1: 1, 2: 2, 3: 3, length: 0 }, notReached), {});
shouldBeObject(Array.prototype.group.call({ 0: 0, 1: 1, 2: 2, 3: 3, length: -0 }, notReached), {});
shouldBeObject(Array.prototype.group.call({ 0: 0, 1: 1, 2: 2, 3: 3, length: -4 }, notReached), {});
// Reversed generic Object
shouldBeObject(reverseInsertionOrder([undefined]).group((x) => x === undefined ? "a" : "b"), {"a": [undefined]});
shouldBeObject(reverseInsertionOrder([undefined]).group((x) => x === undefined), {"true": [undefined]});
shouldBeObject(reverseInsertionOrder(new Array(4)).group((x) => x === undefined ? "a" : "b"), {"a": [undefined, undefined, undefined, undefined]});
shouldBeObject(reverseInsertionOrder(new Array(4)).group((x) => x === undefined), {"true": [undefined, undefined, undefined, undefined]});
shouldBeObject(reverseInsertionOrder([0, 1, 2, 3]).group((x) => !(x & 1) ? "a" : "b"), {"a": [0, 2], "b": [1, 3]});
shouldBeObject(reverseInsertionOrder([0, 1, 2, 3]).group((x) => !(x & 1)), {"true": [0, 2], "false": [1, 3]});
shouldBeObject(reverseInsertionOrder([0, 1, 2, 3]).group((x, i) => i >= 2 ? "a" : "b"), {"b": [0, 1], "a": [2, 3]});
shouldBeObject(reverseInsertionOrder([0, 1, 2, 3]).group((x, i) => i >= 2), {"false": [0, 1], "true": [2, 3]});
shouldBeObject(reverseInsertionOrder(mixPartialAndFast).group((x, i) => i >= 2 ? "a" : "b"), {"b": [0, 1], "a": [2, undefined, undefined, sparseArrayLength - 1]});
shouldBeObject(reverseInsertionOrder(mixPartialAndFast).group((x, i) => i >= 2), {"false": [0, 1], "true": [2, undefined, undefined, sparseArrayLength - 1]});
// Extra callback parameters
shouldBeObject([0, 1, 2, 3].group((i, j, k, l, m) => m = !m), {"true": [0, 1, 2, 3]});
// Special keys
shouldBeObject([0, 1, 2, 3].group((x) => "constructor").constructor, [0, 1, 2, 3]);
shouldBeObject([0, 1, 2, 3].group((x) => "prototype").prototype, [0, 1, 2, 3]);
shouldBeObject([0, 1, 2, 3].group((x) => "__proto__").__proto__, [0, 1, 2, 3]);
shouldBeObject([0, 1, 2, 3].group((x) => -0)[0], [0, 1, 2, 3]);
shouldBeObject([0, 1, 2, 3].group((x) => 0)[0], [0, 1, 2, 3]);
let objectWithToStringCounter = {
counter: 0,
toString() { return this.counter++; },
};
shouldBeObject([0, 1, 2, 3].group((x) => objectWithToStringCounter), {"0": [0], "1": [1], "2": [2], "3": [3]});
shouldBe(objectWithToStringCounter.counter, 4);
try {
shouldBeObject([0, 1, 2, 3].group((x) => objectWithToStringThatThrows), {});
notReached();
} catch (e) {
shouldBe(e.message, "should not reach here");
}
shouldBeObject([0, 1, 2, 3].group((x) => objectWithValueOfThatThrows)["[object Object]"], [0, 1, 2, 3]);
shouldBeObject([0, 1, 2, 3].group((x) => symbol)[symbol], [0, 1, 2, 3]);