funtool
Version:
A modern, efficient, and modular JavaScript utility library designed to enhance developer productivity.
3 lines (2 loc) • 4.44 kB
JavaScript
"use strict";class t{constructor(){this.plugins=new Map,this.internalRuleNames=new Set,this.listeners=[],this.plugins=new Map,this.internalRuleNames=new Set,this.listeners=[]}static getInstance(){return t.instance||(t.instance=new t),t.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()}}t.instance=null;const e=t.getInstance();class n{constructor(t){this.input="",this.result=!0,this.isNegated=!1,this.input=t}use(t){let n=!1;if(t instanceof RegExp)n=t.test(this.input);else{const a=e.get(t);if(!a)throw new Error(`Unknown rule: '${t}'`);n=a.validate({input:this.input,name:a.name,pattern:a.pattern})}return this.result=this.isNegated?!n:n,this.isNegated=!1,this}not(){return this.isNegated=!0,this}isValid(){return this.result}}class a{constructor(t){this.input=t}use(t){if(t instanceof RegExp)this.pattern=t;else{const n=e.get(t);if(!n)throw new Error(`Unknown rule: '${t}'`);this.pattern=n.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 i(t){return e.register(t,!0),t}const s={email:i({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:i({name:"mobile",pattern:/^1[3|4|5|6|7|8|9][0-9]{9}$/,validate:t=>t.pattern.test(t.input)}),alpha:i({name:"alpha",pattern:/^[a-zA-Z]+$/,validate:t=>t.pattern.test(t.input)}),chinese:i({name:"chinese",pattern:/^\p{Script=Han}+$/u,validate:t=>t.pattern.test(t.input)}),ipv6:i({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:i({name:"postal",pattern:/\d{6}/,validate:t=>t.pattern.test(t.input)}),username:i({name:"username",pattern:/^[a-zA-Z][a-zA-Z0-9_]{4,15}$/,validate:t=>t.pattern.test(t.input)}),ipv4:i({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:i({name:"IDCard",pattern:/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,validate:t=>t.pattern.test(t.input)}),url:i({name:"url",pattern:/^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+/,validate:t=>t.pattern.test(t.input)}),qq:i({name:"qq",pattern:/^[1-9][0-9]{4,9}$/,validate:t=>t.pattern.test(t.input)}),landline:i({name:"landline",pattern:/^0\d{2,3}-?\d{7,8}$/,validate:t=>t.pattern.test(t.input)}),number:i({name:"number",pattern:/^[0-9]$/,validate:t=>t.pattern.test(t.input)}),nonAscii:i({name:"nonAscii",pattern:/[^\x00-\xff]/,validate:t=>t.pattern.test(t.input)}),nonLatin:i({name:"nonLatin",pattern:/[^\u0000-\u024F]/,validate:t=>t.pattern.test(t.input)}),password:i({name:"password",pattern:/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,validate:t=>t.pattern.test(t.input)}),html:i({name:"html",pattern:/<("[^"]*"|'[^']*'|[^'">])*>/,validate:t=>t.pattern.test(t.input)})};class r{constructor(){for(const t in s)this[t]=s[t]}static getInstance(){return r.instance||(r.instance=new r),r.instance}checker(t){return new n(t)}replacer(t){return new a(t)}definePlugin(t){if(e.isInternal(t.name))throw new Error(`Cannot register plugin with reserved name: '${t.name}'`);return e.register(t),t}}r.instance=null;const l=r.getInstance();exports.regex=l;
//# sourceMappingURL=index.cjs.js.map