• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
3 */
4
5import ObjectUtil from "./ObjectUtil"
6
7/**
8 * 字符串工具类
9 */
10export class ArrayUtil {
11  public static readonly INDEX_INVALID: number = -1;
12
13  /**
14   * 判断array是否为空
15   *
16   * @param collection collection
17   * @return boolean
18   */
19  public static isEmpty<T>(array: T[]): boolean {
20    if (ObjectUtil.isNullOrUndefined(array)) {
21      return true;
22    }
23    return array.length === 0;
24  }
25
26  /**
27   * 判断array是否包含item
28   *
29   * @param array
30   * @param item
31   */
32  public static contains<T>(array: T[], item: T): boolean {
33    if (this.isEmpty(array) || ObjectUtil.isNullOrUndefined(item)) {
34      return false;
35    }
36    return array.indexOf(item) !== this.INDEX_INVALID;
37  }
38
39  /**
40   * 查找 array 中最大值
41   * @param array
42   */
43  public static max(array: number[]): number {
44    return Math.max.apply(null, array);
45  }
46
47  /**
48   * 查找 array 中最小值
49   * @param array
50   */
51  public static min(array: number[]): number {
52    return Math.min.apply(null, array);
53  }
54}
55