-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1 lines (1 loc) · 7.89 KB
/
index.js
1
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Validator=t():e.Validator=t()}("undefined"!=typeof self?self:this,function(){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var u=t[n]={i:n,l:!1,exports:{}};return e[n].call(u.exports,u,u.exports,r),u.l=!0,u.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:n})},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=2)}([function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=t.getType=function(e){return Object.prototype.toString.call(e).slice(8,-1)},u=t.isValidDate=function(e){if(!e)return!1;if("string"==typeof e){var t=e.indexOf(".");t>0&&t<=6&&(e=e.replace(/\./g,"-"))}var r=new Date(e),n=r.getFullYear(),u=r.getMonth();return!(n<1e3)&&!isNaN(u)};t.dateCompare=function(e,t,r){if(!u(e)||!u(t))return!1;var i="Date"===n(e)?e:new Date(e);return"after"===r?i.getTime()>new Date(t).getTime():"after_equal"===r?i.getTime()>=new Date(t).getTime():"before"===r?i.getTime()<new Date(t).getTime():"before_equal"===r?i.getTime()<=new Date(t).getTime():void 0},t.numCompare=function(e,t,r){return"Number"===n(e)&&("lt"===r?parseFloat(e)<parseFloat(t):"lte"===r?parseFloat(e)<=parseFloat(t):"gt"===r?parseFloat(e)>parseFloat(t):"gte"===r?parseFloat(e)>=parseFloat(t):"equal"===r?parseFloat(e)===parseFloat(t):void 0)},t.isContain=function(e,t,r){var u=null;if("Number"===n(e)&&(u=function(e,t){return e==t}),"Boolean"===n(e)&&(u=function(e,t){return e===("true"===t)}),"String"===n(e)&&(u=function(e,t){return e===t}),null===u)return!1;for(var i=0;i<t.length;i++){if(u(e,t[i]))return"in"===r}return"in"!==r}},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),u=r(0);var i=function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.rules={}}return n(e,[{key:"add",value:function(e){for(var t in this.check(e),e)this.rules[t]=e[t]}},{key:"get",value:function(e){return this.rules[e]}},{key:"check",value:function(e){if("Object"!==(0,u.getType)(e))throw new Error("Custom rules must be Object");for(var t in e)if("RegExp"!==(0,u.getType)(e[t])&&"Function"!==(0,u.getType)(e[t]))throw new TypeError("Rule ["+t+"] must be Function or RegExp")}}]),e}();t.default=new i},function(e,t,r){"use strict";var n=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),u=r(0),i=a(r(3)),o=a(r(1));function a(e){return e&&e.__esModule?e:{default:e}}var f=function(){function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this._rules={},this._tips={},this._optional={},"Array"===(0,u.getType)(t)&&this.use(t)}return n(e,[{key:"_runSingleRule",value:function(e,t){if("Function"===(0,u.getType)(t))return t(e);if("String"===(0,u.getType)(t))return(0,i.default)(t,e);if("RegExp"!==(0,u.getType)(t))throw new TypeError("rule type error!! rule type must be Function or RegExp or String!");return t.test(e)}},{key:"_runRuleSet",value:function(e,t,r){if(!r)throw new TypeError("There are no ruleset that name "+e);if(this._optional[e]&&!t)return-1;for(var n=0;n<r.length;n++){var u=r[n];if(!this._runSingleRule(t,u))return n}return-1}},{key:"_getResult",value:function(e,t){if(-1===t)return{isError:!1,isPass:!0,name:e};var r=this._tips[e]||["Error"];return{isError:!0,isPass:!1,name:e,tip:1===r.length?r[0]:r[t]}}},{key:"use",value:function(e){for(var t=0;t<e.length;t++){var r=e[t];r.optional&&(this._optional[r.name]=!0),r.tips&&(this._tips[r.name]=r.tips),this._rules[r.name]=r.rules}}},{key:"check",value:function(e,t){if("String"===(0,u.getType)(t)){var r=this._runRuleSet(t,e,this._rules[t]);return this._getResult(t,r)}throw new TypeError("params: ["+t+"] should be String")}},{key:"checkWithDiff",value:function(e,t,r){if("Object"===(0,u.getType)(e)){var n=e.name,i=e.value,o=this._runRuleSet(n,i,this._rules[n]);return this._getResult(n,o)}if("Array"===(0,u.getType)(e)&&r){var a=void 0;r.forEach(function(r,n){"RegExp"===(0,u.getType)(r)&&r.test(e[0])&&(a=t[n])});var f=this._runRuleSet(a,e[1],this._rules[a]);return this._getResult(a,f)}throw new TypeError("If the first args is not an Object, then all args should be Array")}},{key:"test",value:function(e,t){return this._runSingleRule(e,t)}},{key:"registerRules",value:function(e){o.default.add(e)}}]),e}();t.default=f,e.exports=t.default},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=function(){return function(e,t){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return function(e,t){var r=[],n=!0,u=!1,i=void 0;try{for(var o,a=e[Symbol.iterator]();!(n=(o=a.next()).done)&&(r.push(o.value),!t||r.length!==t);n=!0);}catch(e){u=!0,i=e}finally{try{!n&&a.return&&a.return()}finally{if(u)throw i}}return r}(e,t);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}();t.default=function(e,t){if(!e)throw new Error("Can not parse rule of undefined");return e=e.replace(/\s/g,""),/&&|\|\|/.test(e)?f(e,t):a(e,t)};var u=r(0),i=r(4),o=function(e){return e&&e.__esModule?e:{default:e}}(r(1));var a=function(e,t){var r=e.split(":"),a=n(r,2),f=a[0],l=a[1],s="",c=i.presetRules[f]||o.default.get(f);if(!c)throw new Error("Does not has the rule "+f);return"RegExp"===(0,u.getType)(c)?c.test(t):(!i.presetRules[f]&&o.default.get(f)&&"Function"===(0,u.getType)(o.default.get(f))&&(s=f),c(t,l=(s?new RegExp("(size|in|not_in|"+s+")"):/(size|in|not_in)/).test(f)?l&&l.split(/,|-|~/):l))},f=function(e,t){var r=function(e){return e.split("||").map(function(e){return e?/&&/.test(e)?e.split("&&").map(function(e){return e?a(e,t):""}).join("&&"):a(e,t):""}).join("||")},n=void 0;e.indexOf("(")>-1?n=e.split("(").map(function(e){return e.split(")").map(function(e){return r(e)}).join(")")}).join("("):n=r(e);return new Function("return "+n+";")()}},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.presetRules=void 0;var n=r(0);t.presetRules={required:/^.{1,}$/,phone:/(^[1][3-9][0-9]{9}$)/,email:/^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,5})+$/,url:/^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?)(?::\d{2,5})?(?:[/?#]\S*)?$/i,integer:/^-?\d+$/,id_card:/^(\d{18,18}|\d{15,15}|\d{17,17}(X|x))$/,after:function(e,t){return(0,n.dateCompare)(e,t,"after")},after_equal:function(e,t){return(0,n.dateCompare)(e,t,"after_equal")},before:function(e,t){return(0,n.dateCompare)(e,t,"before")},before_equal:function(e,t){return(0,n.dateCompare)(e,t,"before_equal")},size:function(e,t){if("String"!==(0,n.getType)(e))return!1;1===t.length&&t.unshift(0);var r=e.length;return r<=t[1]&&r>=t[0]},gt:function(e,t){return(0,n.numCompare)(e,t,"gt")},gte:function(e,t){return(0,n.numCompare)(e,t,"gte")},lt:function(e,t){return(0,n.numCompare)(e,t,"lt")},lte:function(e,t){return(0,n.numCompare)(e,t,"lte")},equal:function(e,t){return(0,n.numCompare)(e,t,"equal")},in:function(e,t){return(0,n.isContain)(e,t,"in")},not_in:function(e,t){return(0,n.isContain)(e,t,"not_in")}}}])});