697d39abff
Proposal repo: https://tc39.github.io/proposal-flatMap/ Bug: v8:7220 Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng Change-Id: I61661fc6d5c39d084ce5c96a9e150e5c26799e2d Also-By: bmeurer@chromium.org Reviewed-on: https://chromium-review.googlesource.com/957043 Commit-Queue: Mathias Bynens <mathias@chromium.org> Reviewed-by: Sathya Gunasekaran <gsathya@chromium.org> Reviewed-by: Benedikt Meurer <bmeurer@chromium.org> Cr-Commit-Position: refs/heads/master@{#51967}
121 lines
2.0 KiB
JavaScript
121 lines
2.0 KiB
JavaScript
// Copyright 2018 the V8 project authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
// Flags: --harmony-array-flatten --allow-natives-syntax
|
|
|
|
assertEquals(Array.prototype.flatMap.length, 1);
|
|
assertEquals(Array.prototype.flatMap.name, 'flatMap');
|
|
|
|
assertEquals(
|
|
[1, 2, 3, 4].flatMap((element) => [element, element ** 2]),
|
|
[1, 1, 2, 4, 3, 9, 4, 16]
|
|
);
|
|
assertEquals(
|
|
[1, 2, 3, 4].flatMap((element) => [[element, element ** 2]]),
|
|
[[1, 1], [2, 4], [3, 9], [4, 16]]
|
|
);
|
|
|
|
const elements = new Set([
|
|
-Infinity,
|
|
-1,
|
|
-0,
|
|
+0,
|
|
+1,
|
|
Infinity,
|
|
null,
|
|
undefined,
|
|
true,
|
|
false,
|
|
'',
|
|
'foo',
|
|
/./,
|
|
[],
|
|
{},
|
|
Object.create(null),
|
|
new Proxy({}, {}),
|
|
Symbol(),
|
|
x => x ** 2,
|
|
String
|
|
]);
|
|
|
|
for (const value of elements) {
|
|
assertEquals(
|
|
[value].flatMap((element) => [element, element]),
|
|
[value, value]
|
|
);
|
|
}
|
|
|
|
const array = [42];
|
|
assertEquals(
|
|
[array].flatMap((element) => [element, element]),
|
|
[array, array]
|
|
);
|
|
|
|
const nonCallables = new Set([
|
|
-Infinity,
|
|
-1,
|
|
-0,
|
|
+0,
|
|
+1,
|
|
Infinity,
|
|
null,
|
|
undefined,
|
|
true,
|
|
false,
|
|
'',
|
|
'foo',
|
|
/./,
|
|
[],
|
|
{},
|
|
Object.create(null),
|
|
new Proxy({}, {}),
|
|
Symbol(),
|
|
]);
|
|
for (const nonCallable of nonCallables) {
|
|
assertThrows(() => {
|
|
[].flatMap(nonCallable);
|
|
}, TypeError);
|
|
}
|
|
|
|
const object = {
|
|
foo: 42,
|
|
get length() {
|
|
object.foo = 0;
|
|
}
|
|
};
|
|
const result = [object].flatMap((element) => [element, element]);
|
|
%HeapObjectVerify(result);
|
|
assertEquals(result, [object, object]);
|
|
assertEquals(result[0].foo, 42);
|
|
|
|
assertThrows(() => {
|
|
Array.prototype.flatMap.call(null, (element) => element);
|
|
}, TypeError);
|
|
assertThrows(() => {
|
|
Array.prototype.flatMap.call(undefined, (element) => element);
|
|
}, TypeError);
|
|
|
|
assertEquals(
|
|
Array.prototype.flatMap.call(
|
|
{
|
|
length: 1,
|
|
0: 'a',
|
|
1: 'b',
|
|
},
|
|
(element) => element
|
|
),
|
|
['a']
|
|
);
|
|
assertEquals(
|
|
Array.prototype.flatMap.call(
|
|
{
|
|
length: 2,
|
|
0: 'a',
|
|
1: 'b',
|
|
},
|
|
(element) => element
|
|
),
|
|
['a', 'b']
|
|
);
|