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