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 util from '@ohos.util' 17bea4f105Sopenharmony_ci 18bea4f105Sopenharmony_ci/** 19bea4f105Sopenharmony_ci * [处理工具类] 20bea4f105Sopenharmony_ci */ 21bea4f105Sopenharmony_ciexport class Util { 22bea4f105Sopenharmony_ci /** 23bea4f105Sopenharmony_ci * 判断是否为空值 24bea4f105Sopenharmony_ci * @param value 数值 25bea4f105Sopenharmony_ci * @returns 26bea4f105Sopenharmony_ci */ 27bea4f105Sopenharmony_ci static isEmpty(value: string | null | undefined): boolean { 28bea4f105Sopenharmony_ci return value === null || value === undefined || value.length === 0 29bea4f105Sopenharmony_ci } 30bea4f105Sopenharmony_ci 31bea4f105Sopenharmony_ci static systemUUid(): string { 32bea4f105Sopenharmony_ci return util.generateRandomUUID(true).toUpperCase(); 33bea4f105Sopenharmony_ci } 34bea4f105Sopenharmony_ci 35bea4f105Sopenharmony_ci static uuid(len?: number, radix?: number): string { 36bea4f105Sopenharmony_ci let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(''); 37bea4f105Sopenharmony_ci let uuids: Array<string> = []; 38bea4f105Sopenharmony_ci let i: number; 39bea4f105Sopenharmony_ci radix = radix || chars.length; 40bea4f105Sopenharmony_ci 41bea4f105Sopenharmony_ci if (len) { 42bea4f105Sopenharmony_ci // Compact form 43bea4f105Sopenharmony_ci for (i = 0; i < len; i++) { 44bea4f105Sopenharmony_ci uuids[i] = chars[0 | Math.random() * radix]; 45bea4f105Sopenharmony_ci } 46bea4f105Sopenharmony_ci } else { 47bea4f105Sopenharmony_ci // rfc4122, version 4 form 48bea4f105Sopenharmony_ci let r: number; 49bea4f105Sopenharmony_ci // rfc4122 requires these characters 50bea4f105Sopenharmony_ci uuids[8] = uuids[13] = uuids[18] = uuids[23] = '-'; 51bea4f105Sopenharmony_ci uuids[14] = '4'; 52bea4f105Sopenharmony_ci 53bea4f105Sopenharmony_ci // Fill in random data. At i==19 set the high bits of clock sequence as 54bea4f105Sopenharmony_ci // per rfc4122, sec. 4.1.5 55bea4f105Sopenharmony_ci for (i = 0; i < 36; i++) { 56bea4f105Sopenharmony_ci if (!uuids[i]) { 57bea4f105Sopenharmony_ci r = 0 | Math.random() * 16; 58bea4f105Sopenharmony_ci uuids[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r]; 59bea4f105Sopenharmony_ci } 60bea4f105Sopenharmony_ci } 61bea4f105Sopenharmony_ci } 62bea4f105Sopenharmony_ci return uuids.join(''); 63bea4f105Sopenharmony_ci } 64bea4f105Sopenharmony_ci 65bea4f105Sopenharmony_ci /** 66bea4f105Sopenharmony_ci * 只能拷贝简单格式,正则之类的拷贝不了 67bea4f105Sopenharmony_ci * 68bea4f105Sopenharmony_ci * @param obj 原始对象 69bea4f105Sopenharmony_ci * @return cloned obj 70bea4f105Sopenharmony_ci */ 71bea4f105Sopenharmony_ci static deepClone(obj): any { 72bea4f105Sopenharmony_ci let copy 73bea4f105Sopenharmony_ci if (null === obj || "object" !== typeof obj) { 74bea4f105Sopenharmony_ci return obj; 75bea4f105Sopenharmony_ci } 76bea4f105Sopenharmony_ci if (obj instanceof Array) { 77bea4f105Sopenharmony_ci copy = [] 78bea4f105Sopenharmony_ci for (let i = 0, len = obj.length; i < len; i++) { 79bea4f105Sopenharmony_ci copy[i] = Util.deepClone(obj[i]) 80bea4f105Sopenharmony_ci } 81bea4f105Sopenharmony_ci return copy 82bea4f105Sopenharmony_ci } 83bea4f105Sopenharmony_ci if (obj instanceof Object) { 84bea4f105Sopenharmony_ci copy = {} 85bea4f105Sopenharmony_ci for (let attr in obj) { 86bea4f105Sopenharmony_ci if (Object.prototype.hasOwnProperty.call(obj, attr)) { 87bea4f105Sopenharmony_ci copy[attr] = Util.deepClone(obj[attr]) 88bea4f105Sopenharmony_ci } 89bea4f105Sopenharmony_ci } 90bea4f105Sopenharmony_ci } 91bea4f105Sopenharmony_ci return copy 92bea4f105Sopenharmony_ci } 93bea4f105Sopenharmony_ci 94bea4f105Sopenharmony_ci /** 95bea4f105Sopenharmony_ci * 立即执行函数,delay时间段内不再执行 96bea4f105Sopenharmony_ci * 97bea4f105Sopenharmony_ci * @param fn 函数 98bea4f105Sopenharmony_ci * @param delay 时间毫秒 99bea4f105Sopenharmony_ci * @returns 100bea4f105Sopenharmony_ci */ 101bea4f105Sopenharmony_ci static debounceImmediate(fn, delay): () => void { 102bea4f105Sopenharmony_ci let debounceTimer = null 103bea4f105Sopenharmony_ci 104bea4f105Sopenharmony_ci return function (...args): void { 105bea4f105Sopenharmony_ci if (debounceTimer !== null) { 106bea4f105Sopenharmony_ci return 107bea4f105Sopenharmony_ci } 108bea4f105Sopenharmony_ci fn.apply(this, args) 109bea4f105Sopenharmony_ci debounceTimer = setTimeout(() => { 110bea4f105Sopenharmony_ci debounceTimer = null 111bea4f105Sopenharmony_ci }, delay) 112bea4f105Sopenharmony_ci } 113bea4f105Sopenharmony_ci } 114bea4f105Sopenharmony_ci} 115