• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16{
17    let arr = [1, , 2, , 3];
18    let res = arr.flatMap(x => [x]);
19    print(JSON.stringify(res));
20}
21{
22    let arr = new Array(3000);
23    arr[200] = 1;
24    let res = arr.flatMap(x => [x]);
25    print(JSON.stringify(res));
26}
27{
28    class MyArray extends Array {
29        static get [Symbol.species]() {
30            return this;
31        }
32    }
33    const wannabe = new MyArray();
34    const result = wannabe.flatMap(x => [x, x]);
35    print(result instanceof MyArray);
36}
37
38var arr1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
39var arr2 = new Array();
40function testFunction(element, index, array) {
41    if (index == 0) {
42        array.length = 6;
43    }
44    return [element, element * element];
45}
46for (let i = 0; i < 10; i++) arr2[i] = i;
47
48var result1 = arr1.flatMap(testFunction);
49print(result1);
50var result2 = arr2.flatMap(testFunction);
51print(result2);
52
53var arr3 = [0, 1, , , 4, , 6, 7, 8, 9];
54arr3.__proto__.push(0);
55arr3.__proto__.push(1);
56arr3.__proto__.push(2);
57arr3.__proto__.push(3);
58function testFunction(element, index, array) {
59    if (index == 0) {
60        array.length = 6;
61    }
62    if (index < 3)
63        return 1;
64    else
65        return [element, element * element];
66}
67var result3 = arr3.flatMap(testFunction);
68print(result3);
69arr3.__proto__.pop(0);
70arr3.__proto__.pop(1);
71arr3.__proto__.pop(2);
72arr3.__proto__.pop(3);
73
74let arr4 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
75var result4 = arr4.flatMap(testFunction);
76print(result4);
77
78let arr = [1,,,,,5];
79let res = arr.flatMap((x)=>{
80    let ret=[x,x+1];
81    ret[105] = x+2;
82    Object.defineProperty(ret,2000,{value:x+3});
83    return ret;
84})
85print(res);
86print(res.length)
87
88
89