StringUtils.ts 1.21 KB
/**
 * StringUtils class.
 */
export class StringUtils {

  /**
   * Check obj is not empty.
   *
   * @param {object} obj
   * @return {boolean} true(not empty)
   */
  static isNotEmpty(obj: any): boolean {
    // return (obj && obj !== '');
    // 或
    return (obj !== undefined && obj !== null && obj !== '');
  }


  static isEmpty(...params: any): boolean {
    if (params.length === 0) {
      return true;
    }
    for (const param of params) {
      if (!param) { // param === undefined || param === null || param === '';
        return true;
      }
    }
    return false;
  }


  static formatStringForJS(template: string, ...args: any[]): string {
    let formattedString = template;
    for (const arg of args) {
      formattedString = formattedString.replace(/%s/, arg.toString());
    }
    return formattedString;
  }


  static escapeDoubleQuotes(obj:any): any {
    if (typeof obj === 'string') {
      return obj.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
    } else if (typeof obj === 'object') {
      for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
          obj[key] = StringUtils.escapeDoubleQuotes(obj[key]);
        }
      }
    }
    return obj;
  }

}
// export default new StringUtils();