rh-regexp
Version:
```shell npm install rh-regexp ```
2 lines (1 loc) • 4.85 kB
JavaScript
function e(e,t){const{min:n=4,max:r=15}=t;return e.length>=n&&e.length<r}var t=Object.freeze({__proto__:null,_length_:e,charCount:function(e,t){const{min:n=4,max:r=15,reg:o=/\*/}=t;let u=e.length,s=0;for(;u--;)if(new RegExp(o).test(e))s++;else if(s>=r)return!1;return s>=n},typeCount:function(e,t){const{regs:n=[/[a-z]/,/[A-Z]/,/[0-9]/,/[_]/],min:r=3,max:o=15}=t;let u=n.length,s=0;for(;u--;)if(new RegExp(n[u]).test(e))s++;else if(s>=o)return!1;return s>=r},phone:function(e){return/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(e)},seKeyboard:function(e,t){const{max:n=4}=t,r=[["1!","2@","3#","4$","5%","6^","7&","8*","9(","0)","-_","=+"],["q","w","e","r","t","y","u","i","o","p","{[","]}","\\|"],["a","s","d","f","g","h","j","k","l",";:","'\""],["z","x","c","v","b","n","m",",<",".>","/?"]];function o(e){let t=4,n=-2;for(;t--;)for(n=r[t].length;n--;){const o=r[t][n].toLowerCase(),u=o.length||0;if(e==o[0]||u>1&&e==o[1])return[n,t]}return[-2,-2]}let u=1,s=-2,c=-2,f=0,i=e.length;if(i<n)return!0;for(;i--;){const t=o(e[i]),r=t[0],m=t[1],p=(a=c,g=r,h=m,(l=s)<0||a<0||g<0||h<0||l==g&&a==h?0:l==g&&a==h+1?1:l+1==g&&a==h?2:l==g&&a+1==h?3:l==g+1&&a==h?4:0);if(0!=f||s==r&&c==m||(f=p),s<0||c<0||0==f||p!=f||s==r&&c==m)u=1,f=0,s=r,c=m;else if(f==p&&0!=p&&(u++,s=r,c=m),u>=n)return!1}var l,a,g,h;return!0},number:function(e){return 0!==e.length&&/^[0-9]*$/.test(e)},seNumber:function(e,t){const{min:n=0,max:r=4}=t;let o=e.length,u=0;for(;o--;)if(new RegExp(/[0-9]/).test(e[o])?u++:u=0,u>=r||u<n)return!1;return!0},seOrderNumber:function(e,t){const{min:n=0,max:r=4}=t;let o=e.length,u=0,s="-2",c=0;for(;o--;){const t=e[o],f=Number(t);if(isNaN(f))u=0,s="-2",c=0;else if(Number(s)-1===f&&1===c&&u++,Number(s)-1===f&&1!==c&&(c=1,u=2),Number(s)+1===f&&2===c&&u++,Number(s)+1===f&&2!==c&&(c=2,u=2),0===u&&u++,s=t,u>=r||u<n)return!1}return!0},numbersAsc:function(e,t){const{min:n=-1,max:r=4}=t;let o=e.length,u=0,s="-2";for(;o--;){const t=e[o],c=Number(t);if(isNaN(c))u=0,s="-2";else if(Number(s)-1!==c?u=1:u++,s=t,u>=r||u<n)return!1}return!0},numbersDesc:function(e,t){const{min:n=-1,max:r=4}=t;let o=e.length,u=0,s="-2";for(;o--;){const t=e[o],c=Number(t);if(isNaN(c))u=0,s="-2";else if(Number(s)+1!==c?u=1:u++,s=t,u>=r||u<n)return!1}return!0},lowLetter:function(e){return 0!==e.length&&/[a-z]/.test(e)},uppLetter:function(e){return 0!==e.length&&/[A-Z]/.test(e)},letterBegin:function(e){return new RegExp(/^[a-zA-Z]/).test(e)},letterEnd:function(e){return new RegExp(/[a-zA-Z]$/).test(e)},letter:function(e,t){const{max:n=4}=t;let r=0,o=e.length;for(;o--;)if(new RegExp(/[a-zA-Z]/).test(e[o])?r++:r=0,r>=n)return!1;return!0},seLetter:function(e,t){const{max:n=4}=t;let r=0,o=-1e3,u=0,s=e.length;for(;s--;){const t=e[s],c=t.charCodeAt(0);if(c<65||c>122||c<97&&c>90)r=0,o=t.charCodeAt(0);else if((o-1===c&&1===u||o+1===c&&2===u)&&r++,o+1===c&&2!==u&&(u=2,r=2),o-1===c&&1!==u&&(u=1,r=2),o=t.charCodeAt(0),r>=n)return!1}return!0},seLetterAsc:function(e,t){const{max:n=4}=t;let r=0,o="z",u=e.length;for(;u--;){const t=e[u],s=t.charCodeAt(0);if(s<65||s>122||s<97&&s>90)r=0,o="z";else if(o.charCodeAt(0)-1!==s?r=1:r++,o=t,r>=n)return!1}return!0},seLetterDesc:function(e,t){const{max:n=4}=t;let r=0,o="z",u=e.length;for(;u--;){const t=e[u],s=t.charCodeAt(0);if(s<65||s>122||s<97&&s>90)r=0,o="z";else if(o.charCodeAt(0)+1!==s?r=1:r++,o=t,r>=n)return!1}return!0},seLetterIgCase:function(e,t){const{max:n=4}=t;let r=0,o=-1e3,u=0,s=e.length;for(;s--;){const t=e[s],c=t.toUpperCase().charCodeAt(0);if(c<65||c>122||c<97&&c>90)r=0,o=t.toUpperCase().charCodeAt(0);else if((o-1===c&&1===u||o+1===c&&2===u)&&r++,o+1===c&&2!==u&&(u=2,r=2),o-1===c&&1!==u&&(u=1,r=2),o=t.toUpperCase().charCodeAt(0),r>=n)return!1}return!0},seLetterIgCaseAsc:function(e,t){const{max:n=4}=t;let r=0,o="Z",u=e.length;for(;u--;){const t=e[u],s=t.toUpperCase().charCodeAt(0);if(s<65||s>90)r=0,o="Z";else if(o.toUpperCase().charCodeAt(0)-1!==s?r=1:r++,o=t,r>=n)return!1}return!0},seLetterIgCaseDesc:function(e,t){const{max:n=4}=t;let r=0,o="Z";const u=e.split("")||[];let s=u.length;for(;s--;){const e=u[s],t=e.toUpperCase().charCodeAt(0);if(t<65||t>90)r=0,o="Z";else if(o.toUpperCase().charCodeAt(0)+1!==t?r=1:r++,o=e,r>=n)return!1}return!0},required:function(e){return e.length>0}});function n(n,r,o){const u=o;let s={min:void 0,max:void 0,reg:r};return"string"==typeof u?(Object.keys(t).includes(u)||"length"===u)&&(s.type=u):s={...s,...u},!(s.type&&"length"===s.type&&!e(n,s))&&(!(s.type&&t[s.type]&&!t[s.type](n,s))&&!(!s.type&&s.reg&&!new RegExp(s.reg).test(n)))}function r(e,t=/[0-9a-zA-Z_]/){return function(r){let o=1;if(!Array.isArray(e))return n(r,t,e);for(o=e.length;o--;)if(!n(r,t,e[o]))return!1;return!0}}function o(e,t=!1,r=/[0-9a-zA-Z_]/){return function(o){let u=1;const s=[];for(Array.isArray(e)?u=e.length:e=[e];u--;)n(o,r,e[u])===t&&s.push(e[u]);return s}}export{o as RegExpCount,r as RegExps};