vue-types
Version:
Prop types utility for Vue
3 lines (2 loc) • 9.84 kB
JavaScript
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)}}function t(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}function r(){return(r=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function n(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,o(e,t)}function o(e,t){return(o=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function i(e,t){if(null==e)return{};var r,n,o={},i=Object.keys(e);for(n=0;n<i.length;n++)t.indexOf(r=i[n])>=0||(o[r]=e[r]);return o}function u(e){return"[object Object]"===Object.prototype.toString.call(e)}var a={silent:!1},f=["validator"],c=Object.prototype,s=c.toString,l=c.hasOwnProperty,p=/^\s*function (\w+)/;function y(e){var t,r=null!==(t=null==e?void 0:e.type)&&void 0!==t?t:e;if(r){var n=r.toString().match(p);return n?n[1]:""}return""}var v=function(e){var t,r;return!1!==u(e)&&(void 0===(t=e.constructor)||!1!==u(r=t.prototype)&&!1!==r.hasOwnProperty("isPrototypeOf"))},d=function(e){return e},h=d;if("production"!==process.env.NODE_ENV){var b="undefined"!=typeof console;h=b?function(e){!1===a.silent&&console.warn("[VueTypes warn]: "+e)}:d}var O=function(e,t){return l.call(e,t)},g=Number.isInteger||function(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e},m=Array.isArray||function(e){return"[object Array]"===s.call(e)},j=function(e){return"[object Function]"===s.call(e)},_=function(e){return v(e)&&O(e,"_vueTypes_name")},x=function(e){return v(e)&&(O(e,"type")||["_vueTypes_name","validator","default","required"].some(function(t){return O(e,t)}))};function T(e,t){return Object.defineProperty(e.bind(t),"__original",{value:e})}function w(e,t,r){var n;void 0===r&&(r=!1);var o=!0,i="";n=v(e)?e:{type:e};var u=_(n)?n._vueTypes_name+" - ":"";if(x(n)&&null!==n.type){if(void 0===n.type||!0===n.type)return o;if(!n.required&&void 0===t)return o;m(n.type)?(o=n.type.some(function(e){return!0===w(e,t,!0)}),i=n.type.map(function(e){return y(e)}).join(" or ")):o="Array"===(i=y(n))?m(t):"Object"===i?v(t):"String"===i||"Number"===i||"Boolean"===i||"Function"===i?function(e){if(null==e)return"";var t=e.constructor.toString().match(p);return t?t[1]:""}(t)===i:t instanceof n.type}if(!o){var a=u+'value "'+t+'" should be of type "'+i+'"';return!1===r?(h(a),!1):a}if(O(n,"validator")&&j(n.validator)){var f=h,c=[];if(h=function(e){c.push(e)},o=n.validator(t),h=f,!o){var s=(c.length>1?"* ":"")+c.join("\n* ");return c.length=0,!1===r?(h(s),o):s}}return o}function P(e,t){var r=Object.defineProperties(t,{_vueTypes_name:{value:e,writable:!0},isRequired:{get:function(){return this.required=!0,this}},def:{value:function(e){return void 0===e?(O(this,"default")&&delete this.default,this):j(e)||!0===w(this,e,!0)?(this.default=m(e)?function(){return[].concat(e)}:v(e)?function(){return Object.assign({},e)}:e,this):(h(this._vueTypes_name+' - invalid default value: "'+e+'"'),this)}}}),n=r.validator;return j(n)&&(r.validator=T(n,r)),r}function k(e,t){var r=P(e,t);return Object.defineProperty(r,"validate",{value:function(e){return j(this.validator)&&h(this._vueTypes_name+" - calling .validate() will overwrite the current custom validator function. Validator info:\n"+JSON.stringify(this)),this.validator=T(e,this),this}})}function E(e,t,r){var n,o,u=(n=t,o={},Object.getOwnPropertyNames(n).forEach(function(e){o[e]=Object.getOwnPropertyDescriptor(n,e)}),Object.defineProperties({},o));if(u._vueTypes_name=e,!v(r))return u;var a,c,s=r.validator,l=i(r,f);if(j(s)){var p=u.validator;p&&(p=null!==(c=(a=p).__original)&&void 0!==c?c:a),u.validator=T(p?function(e){return p.call(this,e)&&s.call(this,e)}:s,u)}return Object.assign(u,l)}function N(e){return e.replace(/^(?!\s*$)/gm," ")}var V=function(){return k("any",{})},q=function(){return k("function",{type:Function})},A=function(){return k("boolean",{type:Boolean})},S=function(){return k("string",{type:String})},F=function(){return k("number",{type:Number})},D=function(){return k("array",{type:Array})},L=function(){return k("object",{type:Object})},Y=function(){return P("integer",{type:Number,validator:function(e){return g(e)}})},B=function(){return P("symbol",{validator:function(e){return"symbol"==typeof e}})};function M(e,t){if(void 0===t&&(t="custom validation failed"),"function"!=typeof e)throw new TypeError("[VueTypes error]: You must provide a function as argument");return P(e.name||"<<anonymous function>>",{validator:function(r){var n=e(r);return n||h(this._vueTypes_name+" - "+t),n}})}function I(e){if(!m(e))throw new TypeError("[VueTypes error]: You must provide an array as argument.");var t='oneOf - value should be one of "'+e.join('", "')+'".',r=e.reduce(function(e,t){if(null!=t){var r=t.constructor;-1===e.indexOf(r)&&e.push(r)}return e},[]);return P("oneOf",{type:r.length>0?r:void 0,validator:function(r){var n=-1!==e.indexOf(r);return n||h(t),n}})}function J(e){if(!m(e))throw new TypeError("[VueTypes error]: You must provide an array as argument");for(var t=!1,r=[],n=0;n<e.length;n+=1){var o=e[n];if(x(o)){if(_(o)&&"oneOf"===o._vueTypes_name&&o.type){r=r.concat(o.type);continue}if(j(o.validator)&&(t=!0),!0!==o.type&&o.type){r=r.concat(o.type);continue}}r.push(o)}return r=r.filter(function(e,t){return r.indexOf(e)===t}),P("oneOfType",t?{type:r,validator:function(t){var r=[],n=e.some(function(e){var n=w(_(e)&&"oneOf"===e._vueTypes_name?e.type||null:e,t,!0);return"string"==typeof n&&r.push(n),!0===n});return n||h("oneOfType - provided value does not match any of the "+r.length+" passed-in validators:\n"+N(r.join("\n"))),n}}:{type:r})}function R(e){return P("arrayOf",{type:Array,validator:function(t){var r,n=t.every(function(t){return!0===(r=w(e,t,!0))});return n||h("arrayOf - value validation error:\n"+N(r)),n}})}function $(e){return P("instanceOf",{type:e})}function z(e){return P("objectOf",{type:Object,validator:function(t){var r,n=Object.keys(t).every(function(n){return!0===(r=w(e,t[n],!0))});return n||h("objectOf - value validation error:\n"+N(r)),n}})}function C(e){var t=Object.keys(e),r=t.filter(function(t){var r;return!(null===(r=e[t])||void 0===r||!r.required)}),n=P("shape",{type:Object,validator:function(n){var o=this;if(!v(n))return!1;var i=Object.keys(n);if(r.length>0&&r.some(function(e){return-1===i.indexOf(e)})){var u=r.filter(function(e){return-1===i.indexOf(e)});return h(1===u.length?'shape - required property "'+u[0]+'" is not defined.':'shape - required properties "'+u.join('", "')+'" are not defined.'),!1}return i.every(function(r){if(-1===t.indexOf(r))return!0===o._vueTypes_isLoose||(h('shape - shape definition does not include a "'+r+'" property. Allowed keys: "'+t.join('", "')+'".'),!1);var i=w(e[r],n[r],!0);return"string"==typeof i&&h('shape - "'+r+'" property validation error:\n '+N(i)),!0===i})}});return Object.defineProperty(n,"_vueTypes_isLoose",{writable:!0,value:!1}),Object.defineProperty(n,"loose",{get:function(){return this._vueTypes_isLoose=!0,this}}),n}var G=["name","validate","getter"],H=function(){var e;return(e=function(){function e(){}return e.extend=function(e){var t=this;if(m(e))return e.forEach(function(e){return t.extend(e)}),this;var r=e.name,n=e.validate,o=void 0!==n&&n,u=e.getter,a=void 0!==u&&u,f=i(e,G);if(O(this,r))throw new TypeError('[VueTypes error]: Type "'+r+'" already defined');var c,s=f.type;return _(s)?(delete f.type,Object.defineProperty(this,r,a?{get:function(){return E(r,s,f)}}:{value:function(){var e,t=E(r,s,f);return t.validator&&(t.validator=(e=t.validator).bind.apply(e,[t].concat([].slice.call(arguments)))),t}})):(c=a?{get:function(){var e=Object.assign({},f);return o?k(r,e):P(r,e)},enumerable:!0}:{value:function(){var e,t,n=Object.assign({},f);return e=o?k(r,n):P(r,n),n.validator&&(e.validator=(t=n.validator).bind.apply(t,[e].concat([].slice.call(arguments)))),e},enumerable:!0},Object.defineProperty(this,r,c))},t(e,null,[{key:"any",get:function(){return V()}},{key:"func",get:function(){return q().def(this.defaults.func)}},{key:"bool",get:function(){return A().def(this.defaults.bool)}},{key:"string",get:function(){return S().def(this.defaults.string)}},{key:"number",get:function(){return F().def(this.defaults.number)}},{key:"array",get:function(){return D().def(this.defaults.array)}},{key:"object",get:function(){return L().def(this.defaults.object)}},{key:"integer",get:function(){return Y().def(this.defaults.integer)}},{key:"symbol",get:function(){return B()}}]),e}()).defaults={},e.config=a,e.custom=M,e.oneOf=I,e.instanceOf=$,e.oneOfType=J,e.arrayOf=R,e.objectOf=z,e.shape=C,e.utils={validate:function(e,t){return!0===w(t,e,!0)},toType:function(e,t,r){return void 0===r&&(r=!1),r?k(e,t):P(e,t)}},e}();function K(e){var o;return void 0===e&&(e={func:function(){},bool:!0,string:"",number:0,array:function(){return[]},object:function(){return{}},integer:0}),(o=function(o){function i(){return o.apply(this,arguments)||this}return n(i,o),t(i,null,[{key:"sensibleDefaults",get:function(){return r({},this.defaults)},set:function(t){this.defaults=!1!==t?r({},!0!==t?t:e):{}}}]),i}(H)).defaults=r({},e),o}var Q=function(e){function t(){return e.apply(this,arguments)||this}return n(t,e),t}(K());Object.defineProperty(exports,"__esModule",{value:!0}),exports.any=V,exports.array=D,exports.arrayOf=R,exports.bool=A,exports.config=a,exports.createTypes=K,exports.custom=M,exports.default=Q,exports.fromType=E,exports.func=q,exports.instanceOf=$,exports.integer=Y,exports.number=F,exports.object=L,exports.objectOf=z,exports.oneOf=I,exports.oneOfType=J,exports.shape=C,exports.string=S,exports.symbol=B,exports.toType=P,exports.toValidableType=k,exports.validateType=w;
//# sourceMappingURL=vue-types.cjs.map