《鸿蒙第一行代码》 第七课 好用的公共方法

82 阅读1分钟
《鸿蒙第一行代码》 第七课 好用的公共方法

直接上代码,好用的代码事半功倍

import common from '@ohos.app.ability.common';

let context = getContext(this);

export function isNotEmptyArray<T>(array?: T[]): boolean {
  return array && array.length > 0?true:false;
}

export function getString(res:Resource):string{
  return context.resourceManager.getStringSync(res)
}

/**
 * @desc : 判断字符串是否为空,true不为空,有内容
 * @author : congge on 2023-12-15 15:16
 **/
export function isNotEmptyString(str: string): boolean {
  return str && str.length > 0 ?true:false
}

export function logContent<T>(tag:string,obj:T){
    if (obj === null || typeof obj === 'undefined') {
      console.info(tag+": 打印的内容为null")
    } else {
      logContentMore(tag,JSON.stringify(obj))
    }
}

/**
 * @desc : 截取的log
 * @author : congge on 2024-01-03 18:17
 **/
function logContentMore(tag:string,contentMore:string){
  if (contentMore.length > 120 ){
    console.info(tag+": "+contentMore.substring(0,120))
    logContentMore(tag,contentMore.substring(120,contentMore.length))
  } else {
    console.info(tag+": "+contentMore)
  }
}