Comparing version 1.3.1 to 1.3.2
@@ -96,5 +96,13 @@ 'use strict'; | ||
} | ||
this.default = isArray(def) || (0, _lodash2.default)(def) ? function () { | ||
return def; | ||
} : def; | ||
if (isArray(def)) { | ||
this.default = function () { | ||
return [].concat(def); | ||
}; | ||
} else if ((0, _lodash2.default)(def)) { | ||
this.default = function () { | ||
return Object.assign({}, def); | ||
}; | ||
} else { | ||
this.default = def; | ||
} | ||
return this; | ||
@@ -101,0 +109,0 @@ }, |
@@ -84,5 +84,13 @@ import isPlainObject from 'lodash.isplainobject'; | ||
} | ||
this.default = isArray(def) || isPlainObject(def) ? function () { | ||
return def; | ||
} : def; | ||
if (isArray(def)) { | ||
this.default = function () { | ||
return [].concat(def); | ||
}; | ||
} else if (isPlainObject(def)) { | ||
this.default = function () { | ||
return Object.assign({}, def); | ||
}; | ||
} else { | ||
this.default = def; | ||
} | ||
return this; | ||
@@ -89,0 +97,0 @@ }, |
{ | ||
"name": "vue-types", | ||
"version": "1.3.1", | ||
"version": "1.3.2", | ||
"description": "Prop types utility for Vue", | ||
@@ -26,3 +26,3 @@ "author": "Marco Solazzi", | ||
"build:umd": "rimraf umd && cross-env BABEL_ENV=rollup rollup -c rollup.config.js", | ||
"test": "cross-env BABEL_ENV=rollup karma start karma.conf.js --single-run --browsers PhantomJS", | ||
"test": "cross-env BABEL_ENV=rollup karma start karma.conf.js --single-run --browsers ChromeHeadless", | ||
"test:all": "cross-env BABEL_ENV=rollup karma start karma.conf.js --single-run --browsers Chrome,Firefox", | ||
@@ -68,3 +68,2 @@ "test:saucelab": "cross-env BABEL_ENV=rollup karma start karma-sauce.conf.js --single-run", | ||
"karma-opera-launcher": "1.0.0", | ||
"karma-phantomjs-launcher": "1.0.4", | ||
"karma-rollup-preprocessor": "6.0.0", | ||
@@ -74,2 +73,3 @@ "karma-safari-launcher": "1.0.0", | ||
"mocha": "5.2.0", | ||
"puppeteer": "1.5.0", | ||
"rimraf": "2.6.2", | ||
@@ -76,0 +76,0 @@ "rollup": "0.59.4", |
@@ -119,2 +119,4 @@ # vue-types | ||
_Note: [Vue prop validation](https://vuejs.org/v2/guide/components-props.html#Prop-Validation) requires Array definitions to provide default value as a factory function. `VueTypes.array.def()` accepts both factory functions and arrays. In the latter case, VueTypes will convert the value to a factory function for you._ | ||
#### `VueTypes.bool` | ||
@@ -150,2 +152,4 @@ | ||
_Note: [Vue prop validation](https://vuejs.org/v2/guide/components-props.html#Prop-Validation) requires Object definitions to provide default value as a factory function. `VueTypes.object.def()` accepts both factory functions and plain objects. In the latter case, VueTypes will convert the value to a factory function for you._ | ||
#### `VueTypes.string` | ||
@@ -282,3 +286,3 @@ | ||
Validates that a prop is an object taking on a particular shape. Accepts both simple and `vue-types` types. You can set shape's types as `required` but (obviously) you cannot use `.def()` | ||
Validates that a prop is an object taking on a particular shape. Accepts both simple and `vue-types` types. You can set shape's properties as `required` but (obviously) you cannot use `.def()`. On the other hand you can use `def()` to set a default value for the shape itself. Like `VueTypes.array` and `VueTypes.object`, you can pass to `.def()` either a factory function returning an object or a plain object. | ||
@@ -292,6 +296,7 @@ ```js | ||
id: VueTypes.integer.isRequired | ||
}) | ||
}).def(() => { name: 'John' }) | ||
} | ||
} | ||
// default value = {name: 'John'} | ||
//accepts: userData = {name: 'John', age: 30, id: 1} | ||
@@ -298,0 +303,0 @@ //rejects: userData = {name: 'John', age: 'wrong data', id: 1} |
@@ -80,5 +80,9 @@ import isPlainObject from 'lodash.isplainobject' | ||
} | ||
this.default = (isArray(def) || isPlainObject(def)) ? function () { | ||
return def | ||
} : def | ||
if (isArray(def)) { | ||
this.default = () => [...def] | ||
} else if (isPlainObject(def)) { | ||
this.default = () => Object.assign({}, def) | ||
} else { | ||
this.default = def | ||
} | ||
return this | ||
@@ -85,0 +89,0 @@ }, |
@@ -8,3 +8,8 @@ // TypeScript Version: 2.8 | ||
export interface VueTypeDef<T = any, D = T> extends PropOptions<T> { | ||
export type DefaultFactory<T> = { (): T } | T | ||
export type defaultType<T> = T extends any[] ? DefaultFactory<T> | ||
: T extends object ? DefaultFactory<T> | ||
: T | ||
export interface VueTypeDef<T = any, D = defaultType<T>> extends PropOptions<T> { | ||
readonly _vueTypes_name: string; | ||
@@ -22,3 +27,3 @@ readonly def: (def: D) => this & { default: D }; | ||
export interface VueTypeShape<T> extends VueTypeDef<T> { | ||
readonly def: <P extends { [K in keyof T]?: any }>(def: P) => this & { default: P }; | ||
readonly def: <P extends defaultType<{ [K in keyof T]?: any }>>(def: P) => this & { default: P }; | ||
readonly loose: VueTypeLooseShape<T>; | ||
@@ -29,3 +34,3 @@ } | ||
readonly _vueTypes_isLoose: true; | ||
readonly def: <D extends { [K in keyof T]?: any } & { [key: string]: any }>(def: D) => this & { default: D }; | ||
readonly def: <D extends defaultType<{ [K in keyof T]?: any } & { [key: string]: any }>>(def: D) => this & { default: D }; | ||
} | ||
@@ -32,0 +37,0 @@ |
/*! vue-types - v1.3.1 | ||
/*! vue-types - v1.3.2 | ||
* https://github.com/dwightjack/vue-types | ||
@@ -226,5 +226,13 @@ * Copyright (c) 2018 - Marco Solazzi; | ||
} | ||
this.default = isArray(def) || lodash_isplainobject(def) ? function () { | ||
return def; | ||
} : def; | ||
if (isArray(def)) { | ||
this.default = function () { | ||
return [].concat(def); | ||
}; | ||
} else if (lodash_isplainobject(def)) { | ||
this.default = function () { | ||
return Object.assign({}, def); | ||
}; | ||
} else { | ||
this.default = def; | ||
} | ||
return this; | ||
@@ -231,0 +239,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
/*! vue-types - v1.3.1 | ||
/*! vue-types - v1.3.2 | ||
* https://github.com/dwightjack/vue-types | ||
@@ -6,3 +6,3 @@ * Copyright (c) 2018 - Marco Solazzi; | ||
*/ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("vue")):"function"==typeof define&&define.amd?define(["vue"],t):e.VueTypes=t(e.Vue)}(this,function(e){"use strict";e=e&&e.hasOwnProperty("default")?e.default:e;var t,r,n=Function.prototype,o=Object.prototype,u=n.toString,i=o.hasOwnProperty,a=u.call(Object),f=o.toString,c=(t=Object.getPrototypeOf,r=Object,function(e){return t(r(e))});var y=function(e){if(!(t=e)||"object"!=typeof t||"[object Object]"!=f.call(e)||function(e){var t=!1;if(null!=e&&"function"!=typeof e.toString)try{t=!!(e+"")}catch(e){}return t}(e))return!1;var t,r=c(e);if(null===r)return!0;var n=i.call(r,"constructor")&&r.constructor;return"function"==typeof n&&n instanceof n&&u.call(n)==a},l=Object.prototype,p=l.toString,s=l.hasOwnProperty,v=/^\s*function (\w+)/,d=function(e){var t=null!=e?e.type?e.type:e:null,r=t&&t.toString().match(v);return r&&r[1]},b=function(){},m=Number.isInteger||function(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e},O=Array.isArray||function(e){return"[object Array]"===p.call(e)},g=function(e){return"[object Function]"===p.call(e)},h=function(e,t){var r,n;return Object.defineProperty(t,"_vueTypes_name",{enumerable:!1,writable:!1,value:e}),r=t,Object.defineProperty(r,"isRequired",{get:function(){return this.required=!0,this},enumerable:!1}),n=t,Object.defineProperty(n,"def",{value:function(e){return(void 0!==e||this.default)&&(g(e)||j(this,e)?this.default=O(e)||y(e)?function(){return e}:e:T(this._vueTypes_name+' - invalid default value: "'+e+'"',e)),this},enumerable:!1,writable:!1}),g(t.validator)&&(t.validator=t.validator.bind(t)),t},j=function t(e,r){var n=2<arguments.length&&void 0!==arguments[2]&&arguments[2],o=e,u=!0,i=void 0;y(e)||(o={type:e});var a=o._vueTypes_name?o._vueTypes_name+" - ":"";if(s.call(o,"type")&&null!==o.type&&(O(o.type)?(u=o.type.some(function(e){return t(e,r,!0)}),i=o.type.map(function(e){return d(e)}).join(" or ")):u="Array"===(i=d(o))?O(r):"Object"===i?y(r):"String"===i||"Number"===i||"Boolean"===i||"Function"===i?function(e){if(null==e)return null;var t=e.constructor.toString().match(v);return t&&t[1]}(r)===i:r instanceof o.type),!u)return!1===n&&T(a+'value "'+r+'" should be of type "'+i+'"'),!1;if(s.call(o,"validator")&&g(o.validator)){var f=void 0;return n&&(f=T,T=b),u=o.validator(r),f&&(T=f),u||!1!==n||T(a+"custom validation failed"),u}return u},T=b,_="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},w={get any(){return h("any",{type:null})},get func(){return h("function",{type:Function}).def(P.func)},get bool(){return h("boolean",{type:Boolean}).def(P.bool)},get string(){return h("string",{type:String}).def(P.string)},get number(){return h("number",{type:Number}).def(P.number)},get array(){return h("array",{type:Array}).def(P.array)},get object(){return h("object",{type:Object}).def(P.object)},get integer(){return h("integer",{type:Number,validator:function(e){return m(e)}}).def(P.integer)},get symbol(){return h("symbol",{type:null,validator:function(e){return"symbol"===(void 0===e?"undefined":_(e))}})},custom:function(r){var n=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"custom validation failed";if("function"!=typeof r)throw new TypeError("[VueTypes error]: You must provide a function as argument");return h(r.name||"<<anonymous function>>",{validator:function(e){var t=r(e);return t||T(this._vueTypes_name+" - "+n),t}})},oneOf:function(r){if(!O(r))throw new TypeError("[VueTypes error]: You must provide an array as argument");var n='oneOf - value should be one of "'+r.join('", "')+'"',e=r.reduce(function(e,t){return null!=t&&-1===e.indexOf(t.constructor)&&e.push(t.constructor),e},[]);return h("oneOf",{type:0<e.length?e:null,validator:function(e){var t=-1!==r.indexOf(e);return t||T(n),t}})},instanceOf:function(e){return h("instanceOf",{type:e})},oneOfType:function(r){if(!O(r))throw new TypeError("[VueTypes error]: You must provide an array as argument");var n=!1,e=r.reduce(function(e,t,r){if(y(t)){if("oneOf"===t._vueTypes_name)return e.concat(t.type||[]);if(t.type&&!g(t.validator)){if(O(t.type))return e.concat(t.type);e.push(t.type)}else g(t.validator)&&(n=!0);return e}return e.push(t),e},[]);if(!n)return h("oneOfType",{type:e});var o=r.map(function(e){return e&&O(e.type)?e.type.map(d):d(e)}).reduce(function(e,t){return e.concat(O(t)?t:[t])},[]).join('", "');return this.custom(function(t){var e=r.some(function(e){return"oneOf"===e._vueTypes_name?!e.type||j(e.type,t,!0):j(e,t,!0)});return e||T('oneOfType - value type should be one of "'+o+'"'),e})},arrayOf:function(r){return h("arrayOf",{type:Array,validator:function(e){var t=e.every(function(e){return j(r,e)});return t||T('arrayOf - value must be an array of "'+d(r)+'"'),t}})},objectOf:function(r){return h("objectOf",{type:Object,validator:function(t){var e=Object.keys(t).every(function(e){return j(r,t[e])});return e||T('objectOf - value must be an object of "'+d(r)+'"'),e}})},shape:function(o){var u=Object.keys(o),e=u.filter(function(e){return o[e]&&!0===o[e].required}),t=h("shape",{type:Object,validator:function(r){var n=this;if(!y(r))return!1;var t=Object.keys(r);return 0<e.length&&e.some(function(e){return-1===t.indexOf(e)})?(T('shape - at least one of required properties "'+e.join('", "')+'" is not present'),!1):t.every(function(e){if(-1===u.indexOf(e))return!0===n._vueTypes_isLoose||(T('shape - object is missing "'+e+'" property'),!1);var t=o[e];return j(t,r[e])})}});return Object.defineProperty(t,"_vueTypes_isLoose",{enumerable:!1,writable:!0,value:!1}),Object.defineProperty(t,"loose",{get:function(){return this._vueTypes_isLoose=!0,this},enumerable:!1}),t}},S=function(){return{func:b,bool:!0,string:"",number:0,array:function(){return[]},object:function(){return{}},integer:0}},P=S();return Object.defineProperty(w,"sensibleDefaults",{enumerable:!1,set:function(e){!1===e?P={}:!0===e?P=S():y(e)&&(P=e)},get:function(){return P}}),w.utils={validate:function(e,t){return j(t,e,!0)},toType:h},w}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("vue")):"function"==typeof define&&define.amd?define(["vue"],t):e.VueTypes=t(e.Vue)}(this,function(e){"use strict";e=e&&e.hasOwnProperty("default")?e.default:e;var t,n,r=Function.prototype,o=Object.prototype,u=r.toString,i=o.hasOwnProperty,a=u.call(Object),f=o.toString,c=(t=Object.getPrototypeOf,n=Object,function(e){return t(n(e))});var y=function(e){if(!(t=e)||"object"!=typeof t||"[object Object]"!=f.call(e)||function(e){var t=!1;if(null!=e&&"function"!=typeof e.toString)try{t=!!(e+"")}catch(e){}return t}(e))return!1;var t,n=c(e);if(null===n)return!0;var r=i.call(n,"constructor")&&n.constructor;return"function"==typeof r&&r instanceof r&&u.call(r)==a},l=Object.prototype,s=l.toString,p=l.hasOwnProperty,d=/^\s*function (\w+)/,v=function(e){var t=null!=e?e.type?e.type:e:null,n=t&&t.toString().match(d);return n&&n[1]},b=function(){},m=Number.isInteger||function(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e},O=Array.isArray||function(e){return"[object Array]"===s.call(e)},g=function(e){return"[object Function]"===s.call(e)},h=function(e,t){var n,r;return Object.defineProperty(t,"_vueTypes_name",{enumerable:!1,writable:!1,value:e}),n=t,Object.defineProperty(n,"isRequired",{get:function(){return this.required=!0,this},enumerable:!1}),r=t,Object.defineProperty(r,"def",{value:function(e){return(void 0!==e||this.default)&&(g(e)||j(this,e)?O(e)?this.default=function(){return[].concat(e)}:y(e)?this.default=function(){return Object.assign({},e)}:this.default=e:T(this._vueTypes_name+' - invalid default value: "'+e+'"',e)),this},enumerable:!1,writable:!1}),g(t.validator)&&(t.validator=t.validator.bind(t)),t},j=function t(e,n){var r=2<arguments.length&&void 0!==arguments[2]&&arguments[2],o=e,u=!0,i=void 0;y(e)||(o={type:e});var a=o._vueTypes_name?o._vueTypes_name+" - ":"";if(p.call(o,"type")&&null!==o.type&&(O(o.type)?(u=o.type.some(function(e){return t(e,n,!0)}),i=o.type.map(function(e){return v(e)}).join(" or ")):u="Array"===(i=v(o))?O(n):"Object"===i?y(n):"String"===i||"Number"===i||"Boolean"===i||"Function"===i?function(e){if(null==e)return null;var t=e.constructor.toString().match(d);return t&&t[1]}(n)===i:n instanceof o.type),!u)return!1===r&&T(a+'value "'+n+'" should be of type "'+i+'"'),!1;if(p.call(o,"validator")&&g(o.validator)){var f=void 0;return r&&(f=T,T=b),u=o.validator(n),f&&(T=f),u||!1!==r||T(a+"custom validation failed"),u}return u},T=b,_="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},w={get any(){return h("any",{type:null})},get func(){return h("function",{type:Function}).def(P.func)},get bool(){return h("boolean",{type:Boolean}).def(P.bool)},get string(){return h("string",{type:String}).def(P.string)},get number(){return h("number",{type:Number}).def(P.number)},get array(){return h("array",{type:Array}).def(P.array)},get object(){return h("object",{type:Object}).def(P.object)},get integer(){return h("integer",{type:Number,validator:function(e){return m(e)}}).def(P.integer)},get symbol(){return h("symbol",{type:null,validator:function(e){return"symbol"===(void 0===e?"undefined":_(e))}})},custom:function(n){var r=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"custom validation failed";if("function"!=typeof n)throw new TypeError("[VueTypes error]: You must provide a function as argument");return h(n.name||"<<anonymous function>>",{validator:function(e){var t=n(e);return t||T(this._vueTypes_name+" - "+r),t}})},oneOf:function(n){if(!O(n))throw new TypeError("[VueTypes error]: You must provide an array as argument");var r='oneOf - value should be one of "'+n.join('", "')+'"',e=n.reduce(function(e,t){return null!=t&&-1===e.indexOf(t.constructor)&&e.push(t.constructor),e},[]);return h("oneOf",{type:0<e.length?e:null,validator:function(e){var t=-1!==n.indexOf(e);return t||T(r),t}})},instanceOf:function(e){return h("instanceOf",{type:e})},oneOfType:function(n){if(!O(n))throw new TypeError("[VueTypes error]: You must provide an array as argument");var r=!1,e=n.reduce(function(e,t,n){if(y(t)){if("oneOf"===t._vueTypes_name)return e.concat(t.type||[]);if(t.type&&!g(t.validator)){if(O(t.type))return e.concat(t.type);e.push(t.type)}else g(t.validator)&&(r=!0);return e}return e.push(t),e},[]);if(!r)return h("oneOfType",{type:e});var o=n.map(function(e){return e&&O(e.type)?e.type.map(v):v(e)}).reduce(function(e,t){return e.concat(O(t)?t:[t])},[]).join('", "');return this.custom(function(t){var e=n.some(function(e){return"oneOf"===e._vueTypes_name?!e.type||j(e.type,t,!0):j(e,t,!0)});return e||T('oneOfType - value type should be one of "'+o+'"'),e})},arrayOf:function(n){return h("arrayOf",{type:Array,validator:function(e){var t=e.every(function(e){return j(n,e)});return t||T('arrayOf - value must be an array of "'+v(n)+'"'),t}})},objectOf:function(n){return h("objectOf",{type:Object,validator:function(t){var e=Object.keys(t).every(function(e){return j(n,t[e])});return e||T('objectOf - value must be an object of "'+v(n)+'"'),e}})},shape:function(o){var u=Object.keys(o),e=u.filter(function(e){return o[e]&&!0===o[e].required}),t=h("shape",{type:Object,validator:function(n){var r=this;if(!y(n))return!1;var t=Object.keys(n);return 0<e.length&&e.some(function(e){return-1===t.indexOf(e)})?(T('shape - at least one of required properties "'+e.join('", "')+'" is not present'),!1):t.every(function(e){if(-1===u.indexOf(e))return!0===r._vueTypes_isLoose||(T('shape - object is missing "'+e+'" property'),!1);var t=o[e];return j(t,n[e])})}});return Object.defineProperty(t,"_vueTypes_isLoose",{enumerable:!1,writable:!0,value:!1}),Object.defineProperty(t,"loose",{get:function(){return this._vueTypes_isLoose=!0,this},enumerable:!1}),t}},S=function(){return{func:b,bool:!0,string:"",number:0,array:function(){return[]},object:function(){return{}},integer:0}},P=S();return Object.defineProperty(w,"sensibleDefaults",{enumerable:!1,set:function(e){!1===e?P={}:!0===e?P=S():y(e)&&(P=e)},get:function(){return P}}),w.utils={validate:function(e,t){return j(t,e,!0)},toType:h},w}); | ||
//# sourceMappingURL=vue-types.min.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
136591
2022
419