工具庫 --- Validator (JS正則)

阿賀呀發表於2019-03-22

工具庫 --- Validator

今天寫的是一個正則驗證類

單例模式

工具庫地址:github.com/WeForStudy/…

npm地址:www.npmjs.com/package/slm…

單例模式

減少不必要的物件生存,減少資源的佔用

由於只需要new一次,專案中其他專案共用一個物件

  • 靜態屬性 instance --> private

  • 靜態方法getInstance --> public

  • 其他方法,靜態

  • 原始碼

    包含(郵箱、手機、數字、http地址、純英文、包含漢字等驗證)

    
    export class ValidatorUtil {
    
      private static instance = null;
    
      /**
       * @description private the constructor in case other new it
       */
      private constructor() {
      }
    
      /**
       * @description get the instance of Validator
       */
      public static getInstance(): ValidatorUtil {
        if (!this.instance) {
          this.instance = new ValidatorUtil();
        }
        return this.instance;
      }
    
      /**
       * @description inside method will be use in every method
       * @param {String} str the string will be checked later
       * @returns {Boolean} the result
       */
      private regTest(reg: RegExp, str: any): boolean {
        return reg.test(str);
      }
    
      /**
       * @description check string whether is a url or not
       * @param {String} str the string will be checked later
       * @returns {Boolean} the result
       */
      private formatUrl(str: string): boolean {
        const reg = /^(https|http):\/\//g;
        return this.regTest(reg, str);
      }
    
      
      /**
       * @description check string whether is a email url or not
       * @param {String} str the string will be checked later
       * @returns {Boolean} the result
       */
      private formatEmail(str: string): boolean {
        const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/g;
        return this.regTest(reg, str);
      }
    
      /**
       * @description check string whether is a url or not
       * @param {String} str the string will be checked later
       * @returns {Boolean} the result
       */
      private formatCnPhone(str: string): boolean {
        const reg = /^1(3|4|5|6|7|8|9)\d{9}$/g;
        return this.regTest(reg, str);
      }
    
      /**
       * @description check string whether is only a en char or not
       * @param {String} str the string will be checked later
       * @returns {Boolean} the result
       */
      private onlyIncludeEn(str: string): boolean {
        const reg = /[^a-zA-Z]/g;
        return this.regTest(reg, str);
      }
    
      /**
       * @description check string whether is Number or not
       * @param {String} str the string will be checked later
       * @returns {Boolean} the result
       */
      private includeNumber(str: any): boolean {
        return this.regTest(/[0-9]/g, str);
      }
    
      /**
       * @description check string whether is only include number or not
       * @param {String} str the string will be checked later
       * @returns {Boolean} the result
       */
      private onlyNumber(str: any): boolean {
        return !this.regTest(/[^0-9]/g, str);
      }
    
      /**
       * @description check string whether include blank or not
       * @param {String} str the string will be checked later
       * @returns {Boolean} the result
       */
      private includeBlank(str: any): boolean {
        return this.regTest(/\s+|\s+$/g, str);
      }
    
      /**
       * @description check string whether is special char or not
       * @param {String} str the string will be checked later
       * @returns {Boolean} the result
       */
      private includeSpecialChar(str: string): boolean {
        const regEn = /[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im,
        regCn = /[·!#¥(——):;“”‘、,|《。》?、【】[\]]/im;
        if (this.includeBlank(str)) return true;
        if (this.regTest(regEn, str)) return true;
        return this.regTest(regCn, str);
      }
    
      /**
       * @description check string whether include blank or not
       * @param {String} str the string will be checked later
       * @returns {Boolean} the result
       */
      private includeEnlishAndChineseChar(str: any): boolean {
        return this.regTest(/^[\u4e00-\u9fa5a-z]+$/gi, str);
      }
    
      /**
       * @description check string whether include special char or not
       * @param {String} str the string will be checked later
       * @returns {Boolean} the result
       */
      private includeSpecCharAndExcludeSimpleChar(str: string): boolean {
        const regEn = /[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im,
        regCn = /[·!#¥(——)|《》【】[\]]/im; // exclude , 、“”‘’ 。 ?;:
        if (this.regTest(regEn, str)) return true;
        return this.regTest(regCn, str);
      }
    
    }
    
    複製程式碼
  • 使用案例

    const slmUtils = require('../build');
    const assert = require('assert');
    
    const { Validator } = slmUtils;
    describe('Util test', () => {
      describe('Validator check', () => {
        it('should be true when value include number (includeNumber)', () => {
          const objA = '123abc';
          assert.equal(Validator.getInstance().includeNumber(objA), true);
        });
        it('should be false when value not only include number (onlyNumber)', () => {
          const objA = '123abc';
          assert.equal(Validator.getInstance().onlyNumber(objA), false);
        });
        it('should be true when values format include http:// or https:// (formatUrl)', () => {
          const objA = 'https://';
          assert.equal(Validator.getInstance().formatUrl(objA), true);
        });
        it('should be true when values format normal Phone', () => {
          const objA = '13212312321';
          assert.equal(Validator.getInstance().formatCnPhone(objA), true);
        });
        it('should be true when values format normal Emial address', () => {
          const objA = 'trest@xxx.com';
          assert.equal(Validator.getInstance().formatEmail(objA), true);
        });
        it('should be false when values not only include EN Char', () => {
          const objA = 'asd12';
          assert.equal(Validator.getInstance().formatCnPhone(objA), false);
        });
        it('should be true when values include Blank', () => {
          const objA = 'asd 12';
          assert.equal(Validator.getInstance().includeBlank(objA), true);
        });
        it('should be true when values include Special char', () => {
          const objA = 'asd 12';
          assert.equal(Validator.getInstance().includeSpecialChar(objA), true);
        });
        it('should be true when values include Chinese char', () => {
          const objA = '你';
          assert.equal(Validator.getInstance().includeEnlishAndChineseChar(objA), true);
        });
        it('should be false when values include , char', () => {
          const objA = 'ab,d';
          assert.equal(Validator.getInstance().includeSpecCharAndExcludeSimpleChar(objA), true);
        });
      });
    });
    複製程式碼

相關文章