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