1/* 2 * Copyright (c) 2021-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 16import ObjectUtil from "./ObjectUtil" 17 18/** 19 * 字符串工具类 20 */ 21export class ArrayUtil { 22 public static readonly INDEX_INVALID: number = -1; 23 24 /** 25 * 判断array是否为空 26 * 27 * @param collection collection 28 * @return boolean 29 */ 30 public static isEmpty<T>(array: T[]): boolean { 31 if (ObjectUtil.isNullOrUndefined(array)) { 32 return true; 33 } 34 return array.length === 0; 35 } 36 37 /** 38 * 判断array是否包含item 39 * 40 * @param array 41 * @param item 42 */ 43 public static contains<T>(array: T[], item: T): boolean { 44 if (this.isEmpty(array) || ObjectUtil.isNullOrUndefined(item)) { 45 return false; 46 } 47 return array.indexOf(item) !== this.INDEX_INVALID; 48 } 49 50 /** 51 * 查找 array 中最大值 52 * @param array 53 */ 54 public static max(array: number[]): number { 55 return Math.max.apply(null, array); 56 } 57 58 /** 59 * 查找 array 中最小值 60 * @param array 61 */ 62 public static min(array: number[]): number { 63 return Math.min.apply(null, array); 64 } 65} 66