StringUtils.ts 4.04 KB
const SLEEP_TIME: number = 10;

/**
 * StringUtils class.
 */
export class StringUtils {
  /**
   *  The String utils tag.
   */
  private static readonly TAG: string = 'StringUtils';
  // 评分-整数或浮点数
  private static readonly SCORE_REG_EXP = RegExp(/^(\d+)(\.\d+)?$/);

  /**
   * Check string is empty.
   *
   * @param {object} any
   * @return {boolean} true(empty)
   */
  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;
  }

  /**
   * 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 isObject(value: any): boolean {
    return Object.prototype.toString.call(value) === '[object Object]';
  }

  static isNotNullObject(value: unknown): boolean {
    return value !== null ? typeof value === 'object' : false;
  }

  static isNullOrUndefined(value: any): boolean {
    return value === undefined || value === null;
  }

  static isFunction(object: any): boolean {
    return typeof object === 'function';
  }

  static isValidObject(objectList: any): boolean {
    if (!objectList || typeof objectList !== 'object'
      || objectList.constructor.name === 'Object') {
      return false;
    }
    return true;
  }

  static isClass(object: any): boolean {
    if (StringUtils.isFunction(object)) {
      const prototype = object.prototype;
      if (!StringUtils.isNotNullObject(prototype)) {
        return false;
      }
      const constructor = prototype.constructor;
      if (StringUtils.isFunction(constructor)) {
        return true;
      }
    }
    return false;
  }

  static getClassName(clz: any): string {
    if (StringUtils.isNullOrUndefined(clz)) {
      return clz;
    }
    let input = clz;
    if (!StringUtils.isClass(input)) {
      input = clz.constructor;
    }
    return input.className ? input.className : input.name;
  }

  static hash(value: string | number): string {
    const str = '' + value;
    let hash = 5381;
    let index = str.length;
    while (index) {
      hash = (hash * 33) ^ str.charCodeAt(--index);
    }
    return '' + (hash >>> 0);
  }

  static parseNumber(input: string): number {
    // let input: string = '135'  // 135
    // let input: string = '135T' // NaN
    // let input: string = 'ytr'  // NaN
    // let input: string = ''     // 0
    // let input: string = null   // 0
    // let input: string = undefined  // NaN
    const parsedInt = Number(input)
    if (!isNaN(parsedInt)) {
      return parsedInt
    } else {
      return 0
    }
  }

  static async sleep(times: number): Promise<void> {
    if (!times) {
      times = SLEEP_TIME;
    }
    await new Promise((res) => setTimeout(res, times)) // .then().catch().finally();
  }

  static isScore(text: string): boolean {
    return StringUtils.SCORE_REG_EXP.test(text);
  }

  //手机号校验
  static photoMatch(phone:string) : boolean  {
    /**
     * 移动号段正则表达式
     */
    let CM_NUM = "^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
    /**
     * 联通号段正则表达式
     */
    let CU_NUM = "^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
    /**
     * 电信号段正则表达式
     */
    let CT_NUM = "^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
    /**
     * 官网正则
     */
    let GW_NUM = "^1(3|4|5|6|7|8|9)\\d{9}$";

    let CM_NUM_reg : RegExp = new RegExp(CM_NUM);
    let CU_NUM_reg : RegExp = new RegExp(CU_NUM);
    let CT_NUM_reg : RegExp = new RegExp(CT_NUM);
    let GW_NUM_reg : RegExp = new RegExp(GW_NUM);

    if (CM_NUM_reg.test(phone) || CU_NUM_reg.test(phone) || CT_NUM_reg.test(phone) || GW_NUM_reg.test(phone)) {
      return true;
    }
    console.log('请输入正确的手机号')
    return false;
  }
}

// export default new StringUtils();