1bea4f105Sopenharmony_ci/*
2bea4f105Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3bea4f105Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bea4f105Sopenharmony_ci * you may not use this file except in compliance with the License.
5bea4f105Sopenharmony_ci * You may obtain a copy of the License at
6bea4f105Sopenharmony_ci *
7bea4f105Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bea4f105Sopenharmony_ci *
9bea4f105Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bea4f105Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bea4f105Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bea4f105Sopenharmony_ci * See the License for the specific language governing permissions and
13bea4f105Sopenharmony_ci * limitations under the License.
14bea4f105Sopenharmony_ci */
15bea4f105Sopenharmony_ci
16bea4f105Sopenharmony_ciimport ObjectUtil from "./ObjectUtil"
17bea4f105Sopenharmony_ci
18bea4f105Sopenharmony_ci/**
19bea4f105Sopenharmony_ci * 字符串工具类
20bea4f105Sopenharmony_ci */
21bea4f105Sopenharmony_ciexport class ArrayUtil {
22bea4f105Sopenharmony_ci  public static readonly INDEX_INVALID: number = -1;
23bea4f105Sopenharmony_ci
24bea4f105Sopenharmony_ci  /**
25bea4f105Sopenharmony_ci   * 判断array是否为空
26bea4f105Sopenharmony_ci   *
27bea4f105Sopenharmony_ci   * @param collection collection
28bea4f105Sopenharmony_ci   * @return boolean
29bea4f105Sopenharmony_ci   */
30bea4f105Sopenharmony_ci  public static isEmpty<T>(array: T[]): boolean {
31bea4f105Sopenharmony_ci    if (ObjectUtil.isNullOrUndefined(array)) {
32bea4f105Sopenharmony_ci      return true;
33bea4f105Sopenharmony_ci    }
34bea4f105Sopenharmony_ci    return array.length === 0;
35bea4f105Sopenharmony_ci  }
36bea4f105Sopenharmony_ci
37bea4f105Sopenharmony_ci  /**
38bea4f105Sopenharmony_ci   * 判断array是否包含item
39bea4f105Sopenharmony_ci   *
40bea4f105Sopenharmony_ci   * @param array
41bea4f105Sopenharmony_ci   * @param item
42bea4f105Sopenharmony_ci   */
43bea4f105Sopenharmony_ci  public static contains<T>(array: T[], item: T): boolean {
44bea4f105Sopenharmony_ci    if (this.isEmpty(array) || ObjectUtil.isNullOrUndefined(item)) {
45bea4f105Sopenharmony_ci      return false;
46bea4f105Sopenharmony_ci    }
47bea4f105Sopenharmony_ci    return array.indexOf(item) !== this.INDEX_INVALID;
48bea4f105Sopenharmony_ci  }
49bea4f105Sopenharmony_ci
50bea4f105Sopenharmony_ci  /**
51bea4f105Sopenharmony_ci   * 查找 array 中最大值
52bea4f105Sopenharmony_ci   * @param array
53bea4f105Sopenharmony_ci   */
54bea4f105Sopenharmony_ci  public static max(array: number[]): number {
55bea4f105Sopenharmony_ci    return Math.max.apply(null, array);
56bea4f105Sopenharmony_ci  }
57bea4f105Sopenharmony_ci
58bea4f105Sopenharmony_ci  /**
59bea4f105Sopenharmony_ci   * 查找 array 中最小值
60bea4f105Sopenharmony_ci   * @param array
61bea4f105Sopenharmony_ci   */
62bea4f105Sopenharmony_ci  public static min(array: number[]): number {
63bea4f105Sopenharmony_ci    return Math.min.apply(null, array);
64bea4f105Sopenharmony_ci  }
65bea4f105Sopenharmony_ci}
66