• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 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
16const someTestCases = [
17    () => {
18        let log = [];
19        const result = [1, 2, 3].some((x, i, arr) => {
20            log.push({ value: x, index: i, array: [...arr] });
21            return x > 2;
22        });
23        print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: true
24    },
25
26    () => {
27        let log = [];
28        const result = [1, 2, 3].some((x, i, arr) => {
29            log.push({ value: x, index: i, array: [...arr] });
30            return x > 3;
31        });
32        print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: false
33    },
34
35    () => {
36        let log = [];
37        const result = [].some((x, i, arr) => {
38            log.push({ value: x, index: i, array: [...arr] });
39            return x > 0;
40        });
41        print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: false
42    },
43
44    () => {
45        let log = [];
46        const result = [false, 0, ''].some((x, i, arr) => {
47            log.push({ value: x, index: i, array: [...arr] });
48            return x;
49        });
50        print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: false
51    },
52
53    () => {
54        let log = [];
55        const result = [1, 2, 3].some((x, i, arr) => {
56            log.push({ value: x, index: i, array: [...arr] });
57            return x > 1;
58        });
59        print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: true
60    },
61
62    () => {
63        let log = [];
64        const result = [NaN, 2, 3].some((x, i, arr) => {
65            log.push({ value: x, index: i, array: [...arr] });
66            return isNaN(x);
67        });
68        print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: true
69    },
70
71    () => {
72        let log = [];
73        let arr = [1, 2, 3];
74        const result = arr.some((x, i, arrRef) => {
75            log.push({ value: x, index: i, array: [...arrRef] });
76            if (i === 1) arrRef.push(4);
77            return x === 4;
78        });
79        print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: true
80    },
81
82    () => {
83        let log = [];
84        const result = [{ id: 1 }, { id: 2 }].some((x, i, arr) => {
85            log.push({ value: x, index: i, array: [...arr] });
86            return x.id === 2;
87        });
88        print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: true
89    },
90
91    () => {
92        let log = [];
93        const context = { value: 2 };
94        const result = [1, 2, 3].some(function(x, i, arr) {
95            log.push({ value: x, index: i, array: [...arr] });
96            return x === this.value;
97        }, context);
98        print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: true
99    },
100
101    () => {
102        let log = [];
103        const result = [1, 2, 3].some((x, i, arr) => {
104            log.push({ value: x, index: i, array: [...arr] });
105            return false;
106        });
107        print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: false
108    },
109
110    () => {
111        let log = [];
112        const result = [1, 2, 3].some((x, i, arr) => {
113            log.push({ value: x, index: i, array: [...arr] });
114            return true;
115        });
116        print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: true
117    },
118
119    () => {
120        let log = [];
121        const result = [null, undefined, 0].some((x, i, arr) => {
122            log.push({ value: x, index: i, array: [...arr] });
123            return x == null;
124        });
125        print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: true
126    },
127
128    () => {
129        let log = [];
130        let arr = [1, 2, 3];
131        const result = arr.some((x, i, arrRef) => {
132            log.push({ value: x, index: i, array: [...arrRef] });
133            arrRef[i] = x + 1; // 修改当前元素
134            return x === 2;
135        });
136        print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: true
137    },
138
139    () => {
140        let log = [];
141        const result = [[1], [2], [3]].some((x, i, arr) => {
142            log.push({ value: x, index: i, array: [...arr] });
143            return x[0] === 2;
144        });
145        print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: true
146    },
147
148    () => {
149        let log = [];
150        const result = [1, 2, 3].some((x, i, arr) => {
151            log.push({ value: x, index: i, array: [...arr] });
152            return i === 1;
153        });
154        print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: true
155        print("Callback executed count:", log.length); // Should be 2
156    },
157
158    () => {
159        let log = [];
160        const result = [1, 2, -3].some((x, i, arr) => {
161            log.push({ value: x, index: i, array: [...arr] });
162            return x < 0;
163        });
164        print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: true
165    },
166
167    () => {
168        let log = [];
169        const result = [].some((x, i, arr) => {
170            log.push({ value: x, index: i, array: [...arr] });
171            return typeof x === "object";
172        });
173        print("Log:", JSON.stringify(log), "Result:", JSON.stringify(result)); // Result: false
174    },
175];
176
177someTestCases.forEach((test, i) => {
178    print(`Some Test Case ${i + 1}:`);
179    test();
180    print('---');
181});
182
183