UNPKG

funtool

Version:

A modern, efficient, and modular JavaScript utility library designed to enhance developer productivity.

3 lines (2 loc) 4.66 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).funtool={})}(this,(function(t){"use strict";class e{constructor(){this.plugins=new Map,this.internalRuleNames=new Set,this.listeners=[],this.plugins=new Map,this.internalRuleNames=new Set,this.listeners=[]}static getInstance(){return e.instance||(e.instance=new e),e.instance}subscribe(t){return this.listeners.push(t),()=>{this.listeners=this.listeners.filter((e=>e!==t))}}notify(){for(const t of this.listeners)t(this.getAll())}register(t,e=!1){(Array.isArray(t)?t:[t]).forEach((t=>{if(this.plugins.has(t.name))throw new Error(`Rule '${t.name}' is already registered.`);if(e)this.internalRuleNames.add(t.name),Object.defineProperty(t,"_internal",{value:!0,writable:!1,enumerable:!1,configurable:!1});else if(this.internalRuleNames.has(t.name))throw new Error(`Rule name '${t.name}' is reserved and cannot be overridden.`);this.plugins.set(t.name,t)})),this.notify()}get(t){if(this.plugins.has(t))return this.plugins.get(t)}getAll(){return Object.fromEntries(this.plugins)}isInternal(t){return this.internalRuleNames.has(t)}reset(){this.plugins.clear(),this.internalRuleNames.clear(),this.listeners=[],this.notify()}}e.instance=null;const n=e.getInstance();class a{constructor(t){this.input="",this.result=!0,this.isNegated=!1,this.input=t}use(t){let e=!1;if(t instanceof RegExp)e=t.test(this.input);else{const a=n.get(t);if(!a)throw new Error(`Unknown rule: '${t}'`);e=a.validate({input:this.input,name:a.name,pattern:a.pattern})}return this.result=this.isNegated?!e:e,this.isNegated=!1,this}not(){return this.isNegated=!0,this}isValid(){return this.result}}class i{constructor(t){this.input=t}use(t){if(t instanceof RegExp)this.pattern=t;else{const e=n.get(t);if(!e)throw new Error(`Unknown rule: '${t}'`);this.pattern=e.pattern}return this}with(t){if(!this.pattern)throw new Error("No pattern defined. Call .use() first.");return this.input=this.input.replace(this.pattern,t),this}result(){return this.input}}function s(t){return n.register(t,!0),t}const r={email:s({name:"email",pattern:/\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/i,validate:t=>t.pattern.test(t.input)}),mobile:s({name:"mobile",pattern:/^1[3|4|5|6|7|8|9][0-9]{9}$/,validate:t=>t.pattern.test(t.input)}),alpha:s({name:"alpha",pattern:/^[a-zA-Z]+$/,validate:t=>t.pattern.test(t.input)}),chinese:s({name:"chinese",pattern:/^\p{Script=Han}+$/u,validate:t=>t.pattern.test(t.input)}),ipv6:s({name:"ipv6",pattern:/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$|^(?:[a-fA-F0-9]{1,4}:){1,7}:$|^(?:[a-fA-F0-9]{1,4}:){1,6}:[a-fA-F0-9]{1,4}$|^(?:[a-fA-F0-9]{1,4}:){1,5}(?::[a-fA-F0-9]{1,4}){1,2}$|^(?:[a-fA-F0-9]{1,4}:){1,4}(?::[a-fA-F0-9]{1,4}){1,3}$|^(?:[a-fA-F0-9]{1,4}:){1,3}(?::[a-fA-F0-9]{1,4}){1,4}$|^(?:[a-fA-F0-9]{1,4}:){1,2}(?::[a-fA-F0-9]{1,4}){1,5}$|^[a-fA-F0-9]{1,4}:(?:(?::[a-fA-F0-9]{1,4}){1,6})$|^:(?:(?::[a-fA-F0-9]{1,4}){1,7}|:)$/,validate:t=>t.pattern.test(t.input)}),postal:s({name:"postal",pattern:/\d{6}/,validate:t=>t.pattern.test(t.input)}),username:s({name:"username",pattern:/^[a-zA-Z][a-zA-Z0-9_]{4,15}$/,validate:t=>t.pattern.test(t.input)}),ipv4:s({name:"ipv4",pattern:/^(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)){3}$/,validate:t=>t.pattern.test(t.input)}),IDCard:s({name:"IDCard",pattern:/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,validate:t=>t.pattern.test(t.input)}),url:s({name:"url",pattern:/^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+/,validate:t=>t.pattern.test(t.input)}),qq:s({name:"qq",pattern:/^[1-9][0-9]{4,9}$/,validate:t=>t.pattern.test(t.input)}),landline:s({name:"landline",pattern:/^0\d{2,3}-?\d{7,8}$/,validate:t=>t.pattern.test(t.input)}),number:s({name:"number",pattern:/^[0-9]$/,validate:t=>t.pattern.test(t.input)}),nonAscii:s({name:"nonAscii",pattern:/[^\x00-\xff]/,validate:t=>t.pattern.test(t.input)}),nonLatin:s({name:"nonLatin",pattern:/[^\u0000-\u024F]/,validate:t=>t.pattern.test(t.input)}),password:s({name:"password",pattern:/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,validate:t=>t.pattern.test(t.input)}),html:s({name:"html",pattern:/<("[^"]*"|'[^']*'|[^'">])*>/,validate:t=>t.pattern.test(t.input)})};class l{constructor(){for(const t in r)this[t]=r[t]}static getInstance(){return l.instance||(l.instance=new l),l.instance}checker(t){return new a(t)}replacer(t){return new i(t)}definePlugin(t){if(n.isInternal(t.name))throw new Error(`Cannot register plugin with reserved name: '${t.name}'`);return n.register(t),t}}l.instance=null;const p=l.getInstance();t.regex=p})); //# sourceMappingURL=index.umd.js.map