-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy patharray_flat.js
108 lines (102 loc) · 4.52 KB
/
array_flat.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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
function testFlat(input, output, depth)
{
assert.areEqual(output, Array.prototype.flat.call(input, depth));
}
function testFlatMap(input, output, mappingFunction, thisArg)
{
assert.areEqual(output, Array.prototype.flatMap.call(input, mappingFunction, thisArg));
}
const tests = [
{
name : "properties",
body : function ()
{
assert.areEqual("flat", Array.prototype.flat.name, "Array.prototype.flat name should be flat");
assert.areEqual("flatMap", Array.prototype.flatMap.name, "Array.prototype.flatMap name should be flatMap");
assert.areEqual(0, Array.prototype.flat.length, "Array.prototype.flat length should be 0");
assert.areEqual(1, Array.prototype.flatMap.length, "Array.prototype.flatMap length should be 1");
}
},
{
name : "flatten arrays",
body : function ()
{
testFlat([2, 3, [4, 5]], [2, 3, 4, 5]);
testFlat([2, 3, [4, [5, 6]]], [2, 3, 4, [5, 6]]);
testFlat([2, 3, [4, [5, 6]]], [2, 3, 4, 5, 6], 2);
testFlat([], []);
testFlat([[], [], 1], [1]);
const typedArr = new Int32Array(3);
const typedArr2 = new Int32Array(3);
typedArr[0] = 5;
typedArr[1] = 6;
typedArr[2] = 3;
typedArr2[0] = 5;
typedArr2[1] = 6;
typedArr2[2] = 3;
testFlat(typedArr, typedArr2);
}
},
{
name : "flatMap arrays",
body : function ()
{
testFlatMap([2, 3, 4, 5], [2, 4, 3, 6, 4, 8, 5, 10], function (a) { return [a, a * 2]});
const thisArg = { count : 0 };
testFlatMap([2, 3, 4], [2, 3, 3, 4, 4, 5], function (a) { this.count += a; return [ a, a + 1]}, thisArg);
testFlatMap([2, 3, 4], [[2], [3], [4]], function (a) { return [[a]]});
assert.areEqual(9, thisArg.count);
assert.throws(()=>{[2, 3].flatMap("Not Callable")});
assert.throws(()=>{[2, 3].flatMap(class NotCallable {})});
}
},
{
name : "flatMap abnormal this",
body : function ()
{
"use strict";
testFlatMap([2, 3], [null, null], function () { return [this]}, null);
testFlatMap([2, 3], [undefined, undefined], function () { return [this]}, undefined);
testFlatMap([2, 3], [undefined, undefined], function () { return [this]});
testFlatMap([2, 3], ["", ""], function () { return [this]}, "");
testFlatMap([2, 3], ["Test", "Test"], function () { return [this]}, "Test");
const boo = {};
testFlatMap([2, 3], [boo, boo], function () { return [this]}, boo);
}
},
{
name : "Proxied Array",
body : function ()
{
let getCount = 0, hasCount = 0;
const handler = {
get : function (t, p, r) { ++getCount; return Reflect.get(t, p, r); },
has : function (t, p, r) { ++hasCount; return Reflect.has(t, p, r); }
}
const prox = new Proxy ([2, [3, 5]], handler);
testFlat(prox, [2, 3, 5]);
assert.areEqual(4, getCount); // length and constructor are also accessed hence count 2 higher than length
assert.areEqual(2, hasCount);
const prox2 = new Proxy ([2, 3, 5], handler);
testFlatMap(prox2, [2, 4, 3, 6, 5, 10], function (a) { return [a, a * 2]});
assert.areEqual(9, getCount); // length and constructor are also accessed hence count 2 higher than length
assert.areEqual(5, hasCount);
}
},
{
name : "Invalid object",
body : function ()
{
assert.throws(() => {Array.prototype.flat.call(null)}, TypeError);
assert.throws(() => {Array.prototype.flat.call(undefined)}, TypeError);
assert.throws(() => {Array.prototype.flatMap.call(null)}, TypeError);
assert.throws(() => {Array.prototype.flatMap.call(undefined)}, TypeError);
}
}
];
testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });