• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-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
16export default class ArrayUtils {
17
18  /*
19   * When performing a 'forEach' operation on an array,
20   * elements that meet the conditions are allowed to be executed at the end.
21   */
22  static forEachWithDefer<T>(
23    list: readonly T[],
24    deferChecker: (value: T, index: number, array: readonly T[]) => boolean,
25    callbackfn: (value: T, index: number, array: readonly T[]) => void,
26    thisArg?: object
27  ): void {
28    if (!list.length) {
29      return;
30    }
31
32    const temp: number[] = [];
33    list.forEach((item, index, array) => {
34      if (deferChecker(item, index, array)) {
35        temp.push(index);
36        return;
37      }
38      callbackfn.call(thisArg, item, index, array);
39    });
40
41    temp.forEach((index) => {
42      callbackfn.call(thisArg, list[index], index, list);
43    });
44  }
45}
46