工具库 --- 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); }); }); }); 复制代码