All files / angle-util/src TextUtil.ts

18.18% Statements 2/11
12.5% Branches 1/8
50% Functions 1/2
18.18% Lines 2/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51                              1x             1x                                                        
/**
 * this util module is about to verify, fix strings like phone, QR code and so on
 * @module angle-util/textUtil
 */
export default {
  /**
   * 修正电话号码格式
   * @author youngbeen
   * @param {number|string} phone - 待修正的手机号
   * @return {string} 修正后的号码
   * @example
   * let phone = '12345ab384eb(2'
   * textUtil.fixPhoneStr(phone)     // '123453842'
   */
  fixPhoneStr (phone: number | string): string {
    Iif (phone) {
      // change phone into string type at first
      phone = phone.toString()
      // remove all invalid characters in phone
      phone = phone.replace(/[^0-9,]/g, '')
      return phone
    } else {
      return ''
    }
  },
  /**
   * 校验手机号码格式是否合法
   * @author youngbeen
   * @param {number|string} phone - 待校验的手机号
   * @return {boolean} 校验结果
   * @example
   * let phone = '12345'
   * textUtil.verifyPhoneStr(phone)     // false
   * phone = '18611112222'
   * textUtil.verifyPhoneStr(phone)     //true
   */
  verifyPhoneStr (phone: string | number): boolean {
    if (phone) {
      // change phone into string type at first
      phone = phone.toString()
      if (phone.length !== 11 || !/^1\d{10}$/.test(phone)) {
        return false
      } else {
        return true
      }
    } else {
      return false
    }
  }
}