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 16/** 17 * Object工具类 18 */ 19namespace ObjectUtil { 20 21/** 22 * 判断是否为null 23 */ 24 export function isNull(obj: any): boolean { 25 return obj === null; 26 } 27 /** 28 * 判断是否为undefined 29 * @param obj 30 */ 31 export function isUndefined(obj: any): boolean { 32 return obj === undefined; 33 } 34 /** 35 * 判断是否为null 或者 undefined 36 * @param obj 37 */ 38 export function isNullOrUndefined(obj: any): boolean { 39 return isNull(obj) || isUndefined(obj); 40 } 41 42 /** 43 * 返回string,如果是null or undefined返回defaultValue 44 */ 45 export function toString(obj: any, defaultValue: string = ''): string { 46 if (this.isNullOrUndefined(obj)) { 47 return defaultValue; 48 } else { 49 return obj.toString(); 50 } 51 } 52 53 /** 54 * 判断对象中是否有某个属性 55 * @param obj 校验对象 56 * @param key 校验属性 57 */ 58 export function hasKey(obj: object, key: string): boolean { 59 return Object.prototype.hasOwnProperty.call(obj, key); 60 } 61} 62 63export default ObjectUtil;