1e484b35bSopenharmony_ci/*
2e484b35bSopenharmony_ci * Licensed to the Apache Software Foundation (ASF) under one
3e484b35bSopenharmony_ci * or more contributor license agreements.  See the NOTICE file
4e484b35bSopenharmony_ci * distributed with this work for additional information
5e484b35bSopenharmony_ci * regarding copyright ownership.  The ASF licenses this file
6e484b35bSopenharmony_ci * to you under the Apache License, Version 2.0 (the
7e484b35bSopenharmony_ci * "License"); you may not use this file except in compliance
8e484b35bSopenharmony_ci * with the License.  You may obtain a copy of the License at
9e484b35bSopenharmony_ci *
10e484b35bSopenharmony_ci *   http://www.apache.org/licenses/LICENSE-2.0
11e484b35bSopenharmony_ci *
12e484b35bSopenharmony_ci * Unless required by applicable law or agreed to in writing,
13e484b35bSopenharmony_ci * software distributed under the License is distributed on an
14e484b35bSopenharmony_ci * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15e484b35bSopenharmony_ci * KIND, either express or implied.  See the License for the
16e484b35bSopenharmony_ci * specific language governing permissions and limitations
17e484b35bSopenharmony_ci * under the License.
18e484b35bSopenharmony_ci */
19e484b35bSopenharmony_ci/*
20e484b35bSopenharmony_ci * 2021.01.08 - Add some utils.
21e484b35bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
22e484b35bSopenharmony_ci */
23e484b35bSopenharmony_ci
24e484b35bSopenharmony_ciimport { hasOwn } from './index';
25e484b35bSopenharmony_ci
26e484b35bSopenharmony_ci/**
27e484b35bSopenharmony_ci * Verify whether the variable is empty.
28e484b35bSopenharmony_ci * @param {*} any - The variable which should be verified.
29e484b35bSopenharmony_ci * @return {boolean} The result whether the variable is empty.
30e484b35bSopenharmony_ci */
31e484b35bSopenharmony_ciexport function isEmpty(any) {
32e484b35bSopenharmony_ci  if (!any || typeof any !== 'object') {
33e484b35bSopenharmony_ci    return true;
34e484b35bSopenharmony_ci  }
35e484b35bSopenharmony_ci  for (const item in any) {
36e484b35bSopenharmony_ci    if (hasOwn(any, item)) {
37e484b35bSopenharmony_ci      return false;
38e484b35bSopenharmony_ci    }
39e484b35bSopenharmony_ci  }
40e484b35bSopenharmony_ci  return true;
41e484b35bSopenharmony_ci}
42e484b35bSopenharmony_ci
43e484b35bSopenharmony_ci/**
44e484b35bSopenharmony_ci * Verify whether the valiable is null or undefined.
45e484b35bSopenharmony_ci * @param {*} any - The valiable which should be verified.
46e484b35bSopenharmony_ci * @return {boolean} The result whether the variable is null or undefined.
47e484b35bSopenharmony_ci */
48e484b35bSopenharmony_ciexport function isNull(any: any): boolean {
49e484b35bSopenharmony_ci  return any === null || any === undefined;
50e484b35bSopenharmony_ci}
51e484b35bSopenharmony_ci
52e484b35bSopenharmony_ci/**
53e484b35bSopenharmony_ci * Remove an item from an array.
54e484b35bSopenharmony_ci * @param {*[]} arr - The array from which removes an item.
55e484b35bSopenharmony_ci * @param {*} item - The item which to be removed.
56e484b35bSopenharmony_ci * @return {*} The item which has been removed.
57e484b35bSopenharmony_ci */
58e484b35bSopenharmony_ciexport function removeItem(array: any[], item: any) {
59e484b35bSopenharmony_ci  if (!array.length) {
60e484b35bSopenharmony_ci    return;
61e484b35bSopenharmony_ci  }
62e484b35bSopenharmony_ci  if (typeof item !== 'number') {
63e484b35bSopenharmony_ci    item = array.indexOf(item);
64e484b35bSopenharmony_ci  }
65e484b35bSopenharmony_ci  if (item > -1) {
66e484b35bSopenharmony_ci    array.splice(item, 1);
67e484b35bSopenharmony_ci  }
68e484b35bSopenharmony_ci}
69e484b35bSopenharmony_ci
70e484b35bSopenharmony_ci/**
71e484b35bSopenharmony_ci* Find the value of the key.
72e484b35bSopenharmony_ci* @param {string} key - The key.
73e484b35bSopenharmony_ci* @param {Object} message - The object which to be checked.
74e484b35bSopenharmony_ci* @return {*} The value of the key.
75e484b35bSopenharmony_ci*/
76e484b35bSopenharmony_ciexport function getValue(key: string, message: object): any {
77e484b35bSopenharmony_ci  const keys: string[] = key.split('.');
78e484b35bSopenharmony_ci  if (keys.length === 0) {
79e484b35bSopenharmony_ci    return null;
80e484b35bSopenharmony_ci  }
81e484b35bSopenharmony_ci  let value = message;
82e484b35bSopenharmony_ci  for (const i in keys) {
83e484b35bSopenharmony_ci    value = value[keys[i]];
84e484b35bSopenharmony_ci    if (isNull(value)) {
85e484b35bSopenharmony_ci      return null;
86e484b35bSopenharmony_ci    }
87e484b35bSopenharmony_ci  }
88e484b35bSopenharmony_ci  return value;
89e484b35bSopenharmony_ci}
90e484b35bSopenharmony_ci
91e484b35bSopenharmony_ci/**
92e484b35bSopenharmony_ci * This class provide log.
93e484b35bSopenharmony_ci */
94e484b35bSopenharmony_ciexport class Log {
95e484b35bSopenharmony_ci  /**
96e484b35bSopenharmony_ci   * Provide debug log.
97e484b35bSopenharmony_ci   * @param {*[]} message - The debug message.
98e484b35bSopenharmony_ci   * @example
99e484b35bSopenharmony_ci   * Log.debug('This is a debug message.');
100e484b35bSopenharmony_ci   */
101e484b35bSopenharmony_ci  public static debug(...message: any[]): void {
102e484b35bSopenharmony_ci    aceConsole.debug('[JS Framework] (debug) %s', message);
103e484b35bSopenharmony_ci  }
104e484b35bSopenharmony_ci
105e484b35bSopenharmony_ci  /**
106e484b35bSopenharmony_ci   * Provide info log.
107e484b35bSopenharmony_ci   * @param {*[]} message - The info message.
108e484b35bSopenharmony_ci   * @example
109e484b35bSopenharmony_ci   * Log.info('This is an info message.');
110e484b35bSopenharmony_ci   */
111e484b35bSopenharmony_ci  public static info(...message: any[]): void {
112e484b35bSopenharmony_ci    aceConsole.info('[JS Framework] (info) %s', message);
113e484b35bSopenharmony_ci  }
114e484b35bSopenharmony_ci
115e484b35bSopenharmony_ci  /**
116e484b35bSopenharmony_ci   * Provide warn log.
117e484b35bSopenharmony_ci   * @param {*[]} message - The warn message.
118e484b35bSopenharmony_ci   * @example
119e484b35bSopenharmony_ci   * Log.warn('This is a warn message.');
120e484b35bSopenharmony_ci   */
121e484b35bSopenharmony_ci  public static warn(...message: any[]): void {
122e484b35bSopenharmony_ci    aceConsole.warn('[JS Framework] (warn) %s', message);
123e484b35bSopenharmony_ci  }
124e484b35bSopenharmony_ci
125e484b35bSopenharmony_ci  /**
126e484b35bSopenharmony_ci   * Provide error log.
127e484b35bSopenharmony_ci   * @param {*[]} message - The error message.
128e484b35bSopenharmony_ci   * @example
129e484b35bSopenharmony_ci   * Log.error('This is an error message.');
130e484b35bSopenharmony_ci   */
131e484b35bSopenharmony_ci  public static error(...message: any[]): void {
132e484b35bSopenharmony_ci    aceConsole.error('[JS Framework] (error) %s', message);
133e484b35bSopenharmony_ci  }
134e484b35bSopenharmony_ci}
135