1/*
2 * Copyright (c) 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 type common from '@ohos.app.ability.common';
17import { DeviceUtils } from '../util/DeviceUtils';
18import { LogUtils } from './LogUtils';
19
20/**
21 * 格式化工具
22 *
23 * @since 2022-06-06
24 */
25export namespace FormatUtils {
26  const DECIMAL_POINT = 2;
27
28  /**
29   * 格式化文件大小
30   *
31   * @param bytes 文件大小
32   * @param decimalPoint 精确到小数点后两位
33   * @return 格式化后的字符串
34   */
35  export function formatFileSize(bytes: number, decimalPoint = DECIMAL_POINT): string {
36    if (bytes <= 0) {
37      return '0 Bytes';
38    }
39    const MAX_BYTES: number = 1024 * 1024 * 1024;
40    const DOWN_POINT: number = 0;
41    const UP_POINT: number = 5;
42    const ONE_KB = 1024;
43    let data: number = Math.min(bytes, MAX_BYTES);
44    let point: number = Math.min(Math.max(decimalPoint, DOWN_POINT), UP_POINT);
45    let sizes = ['Bytes', 'KB', 'MB', 'GB'];
46    let index = Math.floor(Math.log(data) / Math.log(ONE_KB));
47    return parseFloat((bytes / Math.pow(ONE_KB, index)).toFixed(point)) + ' ' + sizes[index];
48  }
49
50  /**
51   * 格式化字符串
52   *
53   * @param 待格式化的字符串
54   * @param args 待匹配的内容
55   * @return 格式化后的字符串
56   */
57  export function formatStr(message: string, ...args: (string | number)[]): string {
58    if (!message) {
59      return '';
60    }
61    const PLACE_HOLDER = new RegExp('%s|%d|%[0-9]\\$s');
62    let segments = message.split(PLACE_HOLDER);
63    let formattedStr: string = '';
64    for (let i = 0; i < segments.length; i++) {
65      formattedStr += segments[i];
66      if (i !== segments.length - 1) {
67        formattedStr += args[i] ? args[i] : '';
68      }
69    }
70    return formattedStr;
71  }
72
73  /**
74   * 字符串或字符串资源转大写
75   *
76   * @param context 上下文
77   * @param text 待格式化的字符串
78   * @param args 待匹配的内容
79   * @return 转大写后的字符串
80   */
81  export function toUpperCase(context: common.Context, text: ResourceStr, ...args: (string | number)[]): string {
82    if (!text) {
83      return '';
84    }
85    if (typeof text === 'string') {
86      return text.toUpperCase();
87    } else {
88      let message: string = context?.resourceManager.getStringSync(text.id);
89      return formatStr(message, ...args).toUpperCase();
90    }
91  }
92
93  /**
94   * 数字格式化
95   *
96   * @param num 待格式化数字
97   * @return 格式化之后的数字
98   */
99  export function getNumberFormat(num: number): string {
100    let language: string = DeviceUtils.getSystemLanguage();
101    let numfmt: Intl.NumberFormat = new Intl.NumberFormat(language, {style:'percent', notation:'standard'});
102    return numfmt.format(num);
103  }
104
105  /**
106   * JSON stringify方法封装
107   *
108   * @param value JS对象
109   * @return json字符串
110   */
111  export function stringify<T>(value: T): string {
112    if (value) {
113      try {
114        return JSON.stringify(value);
115      } catch (exception) {
116        LogUtils.error('FormateUtils', 'JSON.stringify failed !!');
117        return '';
118      }
119    }
120    return '';
121  }
122
123  /**
124   * json 字符串解析
125   *
126   * @param content json 字符串
127   * @return T 解析后返回值
128   */
129  export function parseJson<T>(content: string): T | null {
130    if (!content) {
131      return null;
132    }
133    try {
134      return JSON.parse(content) as T;
135    } catch (exception) {
136      LogUtils.error('FormateUtils', 'paramJson failed !!');
137    }
138    return null;
139  }
140}