@jaysalvat/smart-model
Advanced tools
Comparing version 0.3.4 to 0.4.0
@@ -5,3 +5,3 @@ /**! | ||
* https://github.com/jaysalvat/smart-model | ||
* @version 0.3.4 built 2021-02-23 18:42:31 | ||
* @version 0.4.0 built 2021-02-24 12:33:33 | ||
* @license ISC | ||
@@ -60,3 +60,3 @@ * @author Jay Salvat http://jaysalvat.com | ||
if (isSmartModel(target[key])) { | ||
target[key] = target[key].$eject(); | ||
target[key] = target[key].$get(); | ||
} | ||
@@ -219,3 +219,3 @@ })); | ||
let value = target[property]; | ||
if (property === "$eject") { | ||
if ([ "$get" ].includes(property)) { | ||
return function() { | ||
@@ -298,2 +298,5 @@ return eject(target); | ||
} | ||
$post(data) { | ||
return this.$put(data); | ||
} | ||
$delete(properties) { | ||
@@ -337,3 +340,3 @@ toArray(properties).forEach((key => { | ||
}[name]; | ||
Model.checkErrors = function(payload, filters) { | ||
Model.$check = function(payload, filters) { | ||
const invalidations = {}; | ||
@@ -346,3 +349,3 @@ keys(schema, (property => { | ||
if (Nested) { | ||
subErrors = Nested.checkErrors(value, filters); | ||
subErrors = Nested.$check(value, filters); | ||
} | ||
@@ -363,3 +366,3 @@ let errors = checkErrors(entry, property, value, false, settings); | ||
}; | ||
Model.hydrate = function(payload) { | ||
Model.$hydrate = function(payload) { | ||
if (isArray(payload)) { | ||
@@ -366,0 +369,0 @@ return payload.map((item => new Model(item))); |
@@ -1,2 +0,2 @@ | ||
/*! SmartModel v0.3.4 */ | ||
function e(e){return Array.isArray(e)}function t(e){return void 0===e}function r(e){return"function"==typeof e}function n(e){return e&&e.toString().startsWith("class")}function o(e){return e.prototype instanceof l||e instanceof l}function s(e){return e&&"[object Object]"===e.toString()}function c(e,t=function(){}){return Object.keys(e).map(t)}function i(e){return[].concat([],e)}function u(e,t){return t=Object.assign({},e,t),c(e,(r=>{s(e[r])&&s(t[r])&&(t[r]=Object.assign({},e[r],u(e[r],t[r])))})),t}class a extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function f(t,r,u,a,f){const p=[];return!f.strict||t&&c(t).length||p.push({message:`Property "${r}" can't be set in strict mode`,code:"strict"}),t.required&&f.empty(u)?(p.push({message:`Property "${r}" is "required"`,code:"required"}),p):t.readonly&&!a?(p.push({message:`Property '${r}' is 'readonly'`,code:"readonly"}),p):(void 0===u||(!t.type||!t.required&&f.empty(u)||o(t.type)&&s(u)||i(t.type).some((t=>function(t,r){const o=r&&r.toString().match(/^\s*function (\w+)/),s=(o?o[1]:"object").toLowerCase();if("date"===s&&t instanceof r)return!0;if("array"===s&&e(t))return!0;if("object"===s){if(n(r)&&t instanceof r)return!0;if(!n(r)&&typeof t===s)return!0}else if(typeof t===s)return!0;return!1}(u,t)))||p.push({message:`Property "${r}" has an invalid type "${typeof u}"`,code:"type"}),t.rule&&c(t.rule,(e=>{(0,t.rule[e])(u)&&p.push({message:`Property "${r}" breaks the "${e}" rule`,code:"rule:"+e})}))),p)}function p(e={},t,r){if(!e.type)return!1;const n=!!o(e.type)&&e.type,c=!!s(e.type)&&e.type;if(n||c){const o=n||l.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z1-9]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),c,r);return e.type=o,o}return!1}a.throw=function(e,t,r,n,o){const c=t.split(":")[0];if(!0===e.exceptions||s(e.exceptions)&&e.exceptions[c])throw new a({message:r,property:n,code:t,source:o&&o.constructor.name})};class l extends class{constructor(e,n){return new Proxy(this,{set(o,s,i){const u=e[s]||{},l=o[s],y=t(l),d=!(y||(h=i,$=l,JSON.stringify(h)===JSON.stringify($)));var h,$;const g=p(u,s,n);function m(r,n){const c=Reflect.apply(r,o,n||[s,i,l,e]);return t(c)?i:c}i=m(o.$onBeforeSet),d&&(i=m(o.$onBeforeUpdate)),r(u.transform)&&(i=m(u.transform,[i,e]));const b=f(u,s,i,y,n);if(b.length){if(!n.exceptions)return!0;a.throw(n,b[0].code,b[0].message,s,o)}return n.strict&&!c(u).length||(g&&(i=new g(i)),o[s]=i,m(o.$onSet),d&&m(o.$onUpdate)),!0},get(n,s){const i=e[s];let u=n[s];if("$eject"===s)return function(){return function(e){return c(e=Object.assign({},e),(t=>{o(e[t])&&(e[t]=e[t].$eject())})),e}(n)};if(!i)return n[s];function a(r,o){const c=Reflect.apply(r,n,o||[s,u,e]);return t(c)?u:c}return u=a(n.$onBeforeGet),r(i)&&(u=a(i,[n,e])),r(i.format)&&(u=a(i.format,[u,e])),u=a(n.$onGet),u},deleteProperty(t,r){const o=t[r];function s(n,s){return Reflect.apply(n,t,s||[r,o,e])}return(e[r]||{}).required&&a.throw(n,"required",`Property "${r}" is "required"`,r,t),s(t.$onBeforeDelete),Reflect.deleteProperty(t,r),s(t.$onDelete),s(t.$onUpdate),!0}})}}{constructor(e={},n={},o){super(e,o),c(e,(o=>{t(n[o])&&(t(e[o].default)?r(e[o])||(this[o]=n[o]):this[o]=e[o].default)})),this.$patch(n)}$patch(e){c(e,(t=>{this[t]=e[t]}))}$put(e){c(this,(t=>{e[t]?o(this[t])?this[t].$put(e[t]):this[t]=e[t]:this.$delete(t)})),c(e,(t=>{this[t]||(this[t]=e[t])}))}$delete(e){i(e).forEach((e=>{Reflect.deleteProperty(this,e)}))}}l.settings={empty:e=>""===e||null===e||t(e),strict:!1,exceptions:{readonly:!1,required:!0,rule:!0,strict:!1,type:!0},methods:{$onBeforeGet:()=>{},$onBeforeSet:()=>{},$onBeforeUpdate:()=>{},$onDelete:()=>{},$onGet:()=>{},$onBeforeDelete:()=>{},$onSet:()=>{},$onUpdate:()=>{}}},l.create=function(t,r,n){n=u(l.settings,n);const o={[t]:class extends l{constructor(e){super(r,e,n)}}}[t];return o.checkErrors=function(e,t){const o={};return c(r,(s=>{let c;const u=e[s],a=r[s],l=p(a,s,n);l&&(c=l.checkErrors(u,t));let y=f(a,s,u,!1,n);c?o[s]=c:y.length&&(t&&(y=y.filter((e=>!i(t).includes(e.code)))),y.length&&(o[s]=y))})),!!c(o).length&&o},o.hydrate=function(t){return e(t)?t.map((e=>new o(e))):new o(t)},Object.assign(o.prototype,n.methods),o.schema=r,o};export default l; | ||
/*! SmartModel v0.4.0 */ | ||
function e(e){return Array.isArray(e)}function t(e){return void 0===e}function r(e){return"function"==typeof e}function n(e){return e&&e.toString().startsWith("class")}function o(e){return e.prototype instanceof l||e instanceof l}function s(e){return e&&"[object Object]"===e.toString()}function c(e,t=function(){}){return Object.keys(e).map(t)}function i(e){return[].concat([],e)}function u(e,t){return t=Object.assign({},e,t),c(e,(r=>{s(e[r])&&s(t[r])&&(t[r]=Object.assign({},e[r],u(e[r],t[r])))})),t}class a extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function p(t,r,u,a,p){const f=[];return!p.strict||t&&c(t).length||f.push({message:`Property "${r}" can't be set in strict mode`,code:"strict"}),t.required&&p.empty(u)?(f.push({message:`Property "${r}" is "required"`,code:"required"}),f):t.readonly&&!a?(f.push({message:`Property '${r}' is 'readonly'`,code:"readonly"}),f):(void 0===u||(!t.type||!t.required&&p.empty(u)||o(t.type)&&s(u)||i(t.type).some((t=>function(t,r){const o=r&&r.toString().match(/^\s*function (\w+)/),s=(o?o[1]:"object").toLowerCase();if("date"===s&&t instanceof r)return!0;if("array"===s&&e(t))return!0;if("object"===s){if(n(r)&&t instanceof r)return!0;if(!n(r)&&typeof t===s)return!0}else if(typeof t===s)return!0;return!1}(u,t)))||f.push({message:`Property "${r}" has an invalid type "${typeof u}"`,code:"type"}),t.rule&&c(t.rule,(e=>{(0,t.rule[e])(u)&&f.push({message:`Property "${r}" breaks the "${e}" rule`,code:"rule:"+e})}))),f)}function f(e={},t,r){if(!e.type)return!1;const n=!!o(e.type)&&e.type,c=!!s(e.type)&&e.type;if(n||c){const o=n||l.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z1-9]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),c,r);return e.type=o,o}return!1}a.throw=function(e,t,r,n,o){const c=t.split(":")[0];if(!0===e.exceptions||s(e.exceptions)&&e.exceptions[c])throw new a({message:r,property:n,code:t,source:o&&o.constructor.name})};class l extends class{constructor(e,n){return new Proxy(this,{set(o,s,i){const u=e[s]||{},l=o[s],d=t(l),y=!(d||(h=i,$=l,JSON.stringify(h)===JSON.stringify($)));var h,$;const g=f(u,s,n);function m(r,n){const c=Reflect.apply(r,o,n||[s,i,l,e]);return t(c)?i:c}i=m(o.$onBeforeSet),y&&(i=m(o.$onBeforeUpdate)),r(u.transform)&&(i=m(u.transform,[i,e]));const b=p(u,s,i,d,n);if(b.length){if(!n.exceptions)return!0;a.throw(n,b[0].code,b[0].message,s,o)}return n.strict&&!c(u).length||(g&&(i=new g(i)),o[s]=i,m(o.$onSet),y&&m(o.$onUpdate)),!0},get(n,s){const i=e[s];let u=n[s];if(["$get"].includes(s))return function(){return function(e){return c(e=Object.assign({},e),(t=>{o(e[t])&&(e[t]=e[t].$get())})),e}(n)};if(!i)return n[s];function a(r,o){const c=Reflect.apply(r,n,o||[s,u,e]);return t(c)?u:c}return u=a(n.$onBeforeGet),r(i)&&(u=a(i,[n,e])),r(i.format)&&(u=a(i.format,[u,e])),u=a(n.$onGet),u},deleteProperty(t,r){const o=t[r];function s(n,s){return Reflect.apply(n,t,s||[r,o,e])}return(e[r]||{}).required&&a.throw(n,"required",`Property "${r}" is "required"`,r,t),s(t.$onBeforeDelete),Reflect.deleteProperty(t,r),s(t.$onDelete),s(t.$onUpdate),!0}})}}{constructor(e={},n={},o){super(e,o),c(e,(o=>{t(n[o])&&(t(e[o].default)?r(e[o])||(this[o]=n[o]):this[o]=e[o].default)})),this.$patch(n)}$patch(e){c(e,(t=>{this[t]=e[t]}))}$put(e){c(this,(t=>{e[t]?o(this[t])?this[t].$put(e[t]):this[t]=e[t]:this.$delete(t)})),c(e,(t=>{this[t]||(this[t]=e[t])}))}$post(e){return this.$put(e)}$delete(e){i(e).forEach((e=>{Reflect.deleteProperty(this,e)}))}}l.settings={empty:e=>""===e||null===e||t(e),strict:!1,exceptions:{readonly:!1,required:!0,rule:!0,strict:!1,type:!0},methods:{$onBeforeGet:()=>{},$onBeforeSet:()=>{},$onBeforeUpdate:()=>{},$onDelete:()=>{},$onGet:()=>{},$onBeforeDelete:()=>{},$onSet:()=>{},$onUpdate:()=>{}}},l.create=function(t,r,n){n=u(l.settings,n);const o={[t]:class extends l{constructor(e){super(r,e,n)}}}[t];return o.$check=function(e,t){const o={};return c(r,(s=>{let c;const u=e[s],a=r[s],l=f(a,s,n);l&&(c=l.$check(u,t));let d=p(a,s,u,!1,n);c?o[s]=c:d.length&&(t&&(d=d.filter((e=>!i(t).includes(e.code)))),d.length&&(o[s]=d))})),!!c(o).length&&o},o.$hydrate=function(t){return e(t)?t.map((e=>new o(e))):new o(t)},Object.assign(o.prototype,n.methods),o.schema=r,o};export default l; |
@@ -5,3 +5,3 @@ /**! | ||
* https://github.com/jaysalvat/smart-model | ||
* @version 0.3.4 built 2021-02-23 18:42:31 | ||
* @version 0.4.0 built 2021-02-24 12:33:33 | ||
* @license ISC | ||
@@ -52,3 +52,3 @@ * @author Jay Salvat http://jaysalvat.com | ||
if (isSmartModel(target[key])) { | ||
target[key] = target[key].$eject(); | ||
target[key] = target[key].$get(); | ||
} | ||
@@ -204,3 +204,3 @@ })); | ||
let value = target[property]; | ||
if (property === "$eject") { | ||
if ([ "$get" ].includes(property)) { | ||
return function() { | ||
@@ -282,2 +282,5 @@ return eject(target); | ||
} | ||
$post(data) { | ||
return this.$put(data); | ||
} | ||
$delete(properties) { | ||
@@ -319,3 +322,3 @@ toArray(properties).forEach((key => { | ||
}[name]; | ||
Model.checkErrors = function(payload, filters) { | ||
Model.$check = function(payload, filters) { | ||
const invalidations = {}; | ||
@@ -328,3 +331,3 @@ keys(schema, (property => { | ||
if (Nested) { | ||
subErrors = Nested.checkErrors(value, filters); | ||
subErrors = Nested.$check(value, filters); | ||
} | ||
@@ -345,3 +348,3 @@ let errors = checkErrors(entry, property, value, false, settings); | ||
}; | ||
Model.hydrate = function(payload) { | ||
Model.$hydrate = function(payload) { | ||
if (isArray(payload)) { | ||
@@ -348,0 +351,0 @@ return payload.map((item => new Model(item))); |
@@ -1,2 +0,2 @@ | ||
/*! SmartModel v0.3.4 */ | ||
var SmartModel=function(){"use strict";function e(e){return Array.isArray(e)}function t(e){return void 0===e}function r(e){return"function"==typeof e}function n(e){return e&&e.toString().startsWith("class")}function o(e){return e.prototype instanceof l||e instanceof l}function s(e){return e&&"[object Object]"===e.toString()}function c(e,t=function(){}){return Object.keys(e).map(t)}function i(e){return[].concat([],e)}function u(e,t){return t=Object.assign({},e,t),c(e,(r=>{s(e[r])&&s(t[r])&&(t[r]=Object.assign({},e[r],u(e[r],t[r])))})),t}class a extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function f(t,r,u,a,f){const p=[];return!f.strict||t&&c(t).length||p.push({message:`Property "${r}" can't be set in strict mode`,code:"strict"}),t.required&&f.empty(u)?(p.push({message:`Property "${r}" is "required"`,code:"required"}),p):t.readonly&&!a?(p.push({message:`Property '${r}' is 'readonly'`,code:"readonly"}),p):(void 0===u||(!t.type||!t.required&&f.empty(u)||o(t.type)&&s(u)||i(t.type).some((t=>function(t,r){const o=r&&r.toString().match(/^\s*function (\w+)/),s=(o?o[1]:"object").toLowerCase();if("date"===s&&t instanceof r)return!0;if("array"===s&&e(t))return!0;if("object"===s){if(n(r)&&t instanceof r)return!0;if(!n(r)&&typeof t===s)return!0}else if(typeof t===s)return!0;return!1}(u,t)))||p.push({message:`Property "${r}" has an invalid type "${typeof u}"`,code:"type"}),t.rule&&c(t.rule,(e=>{(0,t.rule[e])(u)&&p.push({message:`Property "${r}" breaks the "${e}" rule`,code:"rule:"+e})}))),p)}function p(e={},t,r){if(!e.type)return!1;const n=!!o(e.type)&&e.type,c=!!s(e.type)&&e.type;if(n||c){const o=n||l.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z1-9]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),c,r);return e.type=o,o}return!1}a.throw=function(e,t,r,n,o){const c=t.split(":")[0];if(!0===e.exceptions||s(e.exceptions)&&e.exceptions[c])throw new a({message:r,property:n,code:t,source:o&&o.constructor.name})};class l extends class{constructor(e,n){return new Proxy(this,{set(o,s,i){const u=e[s]||{},l=o[s],y=t(l),d=!(y||(h=i,$=l,JSON.stringify(h)===JSON.stringify($)));var h,$;const g=p(u,s,n);function m(r,n){const c=Reflect.apply(r,o,n||[s,i,l,e]);return t(c)?i:c}i=m(o.$onBeforeSet),d&&(i=m(o.$onBeforeUpdate)),r(u.transform)&&(i=m(u.transform,[i,e]));const b=f(u,s,i,y,n);if(b.length){if(!n.exceptions)return!0;a.throw(n,b[0].code,b[0].message,s,o)}return n.strict&&!c(u).length||(g&&(i=new g(i)),o[s]=i,m(o.$onSet),d&&m(o.$onUpdate)),!0},get(n,s){const i=e[s];let u=n[s];if("$eject"===s)return function(){return function(e){return c(e=Object.assign({},e),(t=>{o(e[t])&&(e[t]=e[t].$eject())})),e}(n)};if(!i)return n[s];function a(r,o){const c=Reflect.apply(r,n,o||[s,u,e]);return t(c)?u:c}return u=a(n.$onBeforeGet),r(i)&&(u=a(i,[n,e])),r(i.format)&&(u=a(i.format,[u,e])),u=a(n.$onGet),u},deleteProperty(t,r){const o=t[r];function s(n,s){return Reflect.apply(n,t,s||[r,o,e])}return(e[r]||{}).required&&a.throw(n,"required",`Property "${r}" is "required"`,r,t),s(t.$onBeforeDelete),Reflect.deleteProperty(t,r),s(t.$onDelete),s(t.$onUpdate),!0}})}}{constructor(e={},n={},o){super(e,o),c(e,(o=>{t(n[o])&&(t(e[o].default)?r(e[o])||(this[o]=n[o]):this[o]=e[o].default)})),this.$patch(n)}$patch(e){c(e,(t=>{this[t]=e[t]}))}$put(e){c(this,(t=>{e[t]?o(this[t])?this[t].$put(e[t]):this[t]=e[t]:this.$delete(t)})),c(e,(t=>{this[t]||(this[t]=e[t])}))}$delete(e){i(e).forEach((e=>{Reflect.deleteProperty(this,e)}))}}return l.settings={empty:e=>""===e||null===e||t(e),strict:!1,exceptions:{readonly:!1,required:!0,rule:!0,strict:!1,type:!0},methods:{$onBeforeGet:()=>{},$onBeforeSet:()=>{},$onBeforeUpdate:()=>{},$onDelete:()=>{},$onGet:()=>{},$onBeforeDelete:()=>{},$onSet:()=>{},$onUpdate:()=>{}}},l.create=function(t,r,n){n=u(l.settings,n);const o={[t]:class extends l{constructor(e){super(r,e,n)}}}[t];return o.checkErrors=function(e,t){const o={};return c(r,(s=>{let c;const u=e[s],a=r[s],l=p(a,s,n);l&&(c=l.checkErrors(u,t));let y=f(a,s,u,!1,n);c?o[s]=c:y.length&&(t&&(y=y.filter((e=>!i(t).includes(e.code)))),y.length&&(o[s]=y))})),!!c(o).length&&o},o.hydrate=function(t){return e(t)?t.map((e=>new o(e))):new o(t)},Object.assign(o.prototype,n.methods),o.schema=r,o},l}(); | ||
/*! SmartModel v0.4.0 */ | ||
var SmartModel=function(){"use strict";function e(e){return Array.isArray(e)}function t(e){return void 0===e}function r(e){return"function"==typeof e}function n(e){return e&&e.toString().startsWith("class")}function o(e){return e.prototype instanceof l||e instanceof l}function s(e){return e&&"[object Object]"===e.toString()}function c(e,t=function(){}){return Object.keys(e).map(t)}function i(e){return[].concat([],e)}function u(e,t){return t=Object.assign({},e,t),c(e,(r=>{s(e[r])&&s(t[r])&&(t[r]=Object.assign({},e[r],u(e[r],t[r])))})),t}class a extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function p(t,r,u,a,p){const f=[];return!p.strict||t&&c(t).length||f.push({message:`Property "${r}" can't be set in strict mode`,code:"strict"}),t.required&&p.empty(u)?(f.push({message:`Property "${r}" is "required"`,code:"required"}),f):t.readonly&&!a?(f.push({message:`Property '${r}' is 'readonly'`,code:"readonly"}),f):(void 0===u||(!t.type||!t.required&&p.empty(u)||o(t.type)&&s(u)||i(t.type).some((t=>function(t,r){const o=r&&r.toString().match(/^\s*function (\w+)/),s=(o?o[1]:"object").toLowerCase();if("date"===s&&t instanceof r)return!0;if("array"===s&&e(t))return!0;if("object"===s){if(n(r)&&t instanceof r)return!0;if(!n(r)&&typeof t===s)return!0}else if(typeof t===s)return!0;return!1}(u,t)))||f.push({message:`Property "${r}" has an invalid type "${typeof u}"`,code:"type"}),t.rule&&c(t.rule,(e=>{(0,t.rule[e])(u)&&f.push({message:`Property "${r}" breaks the "${e}" rule`,code:"rule:"+e})}))),f)}function f(e={},t,r){if(!e.type)return!1;const n=!!o(e.type)&&e.type,c=!!s(e.type)&&e.type;if(n||c){const o=n||l.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z1-9]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),c,r);return e.type=o,o}return!1}a.throw=function(e,t,r,n,o){const c=t.split(":")[0];if(!0===e.exceptions||s(e.exceptions)&&e.exceptions[c])throw new a({message:r,property:n,code:t,source:o&&o.constructor.name})};class l extends class{constructor(e,n){return new Proxy(this,{set(o,s,i){const u=e[s]||{},l=o[s],d=t(l),y=!(d||(h=i,$=l,JSON.stringify(h)===JSON.stringify($)));var h,$;const g=f(u,s,n);function m(r,n){const c=Reflect.apply(r,o,n||[s,i,l,e]);return t(c)?i:c}i=m(o.$onBeforeSet),y&&(i=m(o.$onBeforeUpdate)),r(u.transform)&&(i=m(u.transform,[i,e]));const b=p(u,s,i,d,n);if(b.length){if(!n.exceptions)return!0;a.throw(n,b[0].code,b[0].message,s,o)}return n.strict&&!c(u).length||(g&&(i=new g(i)),o[s]=i,m(o.$onSet),y&&m(o.$onUpdate)),!0},get(n,s){const i=e[s];let u=n[s];if(["$get"].includes(s))return function(){return function(e){return c(e=Object.assign({},e),(t=>{o(e[t])&&(e[t]=e[t].$get())})),e}(n)};if(!i)return n[s];function a(r,o){const c=Reflect.apply(r,n,o||[s,u,e]);return t(c)?u:c}return u=a(n.$onBeforeGet),r(i)&&(u=a(i,[n,e])),r(i.format)&&(u=a(i.format,[u,e])),u=a(n.$onGet),u},deleteProperty(t,r){const o=t[r];function s(n,s){return Reflect.apply(n,t,s||[r,o,e])}return(e[r]||{}).required&&a.throw(n,"required",`Property "${r}" is "required"`,r,t),s(t.$onBeforeDelete),Reflect.deleteProperty(t,r),s(t.$onDelete),s(t.$onUpdate),!0}})}}{constructor(e={},n={},o){super(e,o),c(e,(o=>{t(n[o])&&(t(e[o].default)?r(e[o])||(this[o]=n[o]):this[o]=e[o].default)})),this.$patch(n)}$patch(e){c(e,(t=>{this[t]=e[t]}))}$put(e){c(this,(t=>{e[t]?o(this[t])?this[t].$put(e[t]):this[t]=e[t]:this.$delete(t)})),c(e,(t=>{this[t]||(this[t]=e[t])}))}$post(e){return this.$put(e)}$delete(e){i(e).forEach((e=>{Reflect.deleteProperty(this,e)}))}}return l.settings={empty:e=>""===e||null===e||t(e),strict:!1,exceptions:{readonly:!1,required:!0,rule:!0,strict:!1,type:!0},methods:{$onBeforeGet:()=>{},$onBeforeSet:()=>{},$onBeforeUpdate:()=>{},$onDelete:()=>{},$onGet:()=>{},$onBeforeDelete:()=>{},$onSet:()=>{},$onUpdate:()=>{}}},l.create=function(t,r,n){n=u(l.settings,n);const o={[t]:class extends l{constructor(e){super(r,e,n)}}}[t];return o.$check=function(e,t){const o={};return c(r,(s=>{let c;const u=e[s],a=r[s],l=f(a,s,n);l&&(c=l.$check(u,t));let d=p(a,s,u,!1,n);c?o[s]=c:d.length&&(t&&(d=d.filter((e=>!i(t).includes(e.code)))),d.length&&(o[s]=d))})),!!c(o).length&&o},o.$hydrate=function(t){return e(t)?t.map((e=>new o(e))):new o(t)},Object.assign(o.prototype,n.methods),o.schema=r,o},l}(); |
@@ -5,3 +5,3 @@ /**! | ||
* https://github.com/jaysalvat/smart-model | ||
* @version 0.3.4 built 2021-02-23 18:42:31 | ||
* @version 0.4.0 built 2021-02-24 12:33:33 | ||
* @license ISC | ||
@@ -55,3 +55,3 @@ * @author Jay Salvat http://jaysalvat.com | ||
if (isSmartModel(target[key])) { | ||
target[key] = target[key].$eject(); | ||
target[key] = target[key].$get(); | ||
} | ||
@@ -207,3 +207,3 @@ })); | ||
let value = target[property]; | ||
if (property === "$eject") { | ||
if ([ "$get" ].includes(property)) { | ||
return function() { | ||
@@ -285,2 +285,5 @@ return eject(target); | ||
} | ||
$post(data) { | ||
return this.$put(data); | ||
} | ||
$delete(properties) { | ||
@@ -322,3 +325,3 @@ toArray(properties).forEach((key => { | ||
}[name]; | ||
Model.checkErrors = function(payload, filters) { | ||
Model.$check = function(payload, filters) { | ||
const invalidations = {}; | ||
@@ -331,3 +334,3 @@ keys(schema, (property => { | ||
if (Nested) { | ||
subErrors = Nested.checkErrors(value, filters); | ||
subErrors = Nested.$check(value, filters); | ||
} | ||
@@ -348,3 +351,3 @@ let errors = checkErrors(entry, property, value, false, settings); | ||
}; | ||
Model.hydrate = function(payload) { | ||
Model.$hydrate = function(payload) { | ||
if (isArray(payload)) { | ||
@@ -351,0 +354,0 @@ return payload.map((item => new Model(item))); |
@@ -1,2 +0,2 @@ | ||
/*! SmartModel v0.3.4 */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).SmartModel=t()}(this,(function(){"use strict";function e(e){return Array.isArray(e)}function t(e){return void 0===e}function r(e){return"function"==typeof e}function n(e){return e&&e.toString().startsWith("class")}function o(e){return e.prototype instanceof l||e instanceof l}function s(e){return e&&"[object Object]"===e.toString()}function c(e,t=function(){}){return Object.keys(e).map(t)}function i(e){return[].concat([],e)}function u(e,t){return t=Object.assign({},e,t),c(e,(r=>{s(e[r])&&s(t[r])&&(t[r]=Object.assign({},e[r],u(e[r],t[r])))})),t}class f extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function a(t,r,u,f,a){const p=[];return!a.strict||t&&c(t).length||p.push({message:`Property "${r}" can't be set in strict mode`,code:"strict"}),t.required&&a.empty(u)?(p.push({message:`Property "${r}" is "required"`,code:"required"}),p):t.readonly&&!f?(p.push({message:`Property '${r}' is 'readonly'`,code:"readonly"}),p):(void 0===u||(!t.type||!t.required&&a.empty(u)||o(t.type)&&s(u)||i(t.type).some((t=>function(t,r){const o=r&&r.toString().match(/^\s*function (\w+)/),s=(o?o[1]:"object").toLowerCase();if("date"===s&&t instanceof r)return!0;if("array"===s&&e(t))return!0;if("object"===s){if(n(r)&&t instanceof r)return!0;if(!n(r)&&typeof t===s)return!0}else if(typeof t===s)return!0;return!1}(u,t)))||p.push({message:`Property "${r}" has an invalid type "${typeof u}"`,code:"type"}),t.rule&&c(t.rule,(e=>{(0,t.rule[e])(u)&&p.push({message:`Property "${r}" breaks the "${e}" rule`,code:"rule:"+e})}))),p)}function p(e={},t,r){if(!e.type)return!1;const n=!!o(e.type)&&e.type,c=!!s(e.type)&&e.type;if(n||c){const o=n||l.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z1-9]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),c,r);return e.type=o,o}return!1}f.throw=function(e,t,r,n,o){const c=t.split(":")[0];if(!0===e.exceptions||s(e.exceptions)&&e.exceptions[c])throw new f({message:r,property:n,code:t,source:o&&o.constructor.name})};class l extends class{constructor(e,n){return new Proxy(this,{set(o,s,i){const u=e[s]||{},l=o[s],d=t(l),y=!(d||(h=i,$=l,JSON.stringify(h)===JSON.stringify($)));var h,$;const g=p(u,s,n);function m(r,n){const c=Reflect.apply(r,o,n||[s,i,l,e]);return t(c)?i:c}i=m(o.$onBeforeSet),y&&(i=m(o.$onBeforeUpdate)),r(u.transform)&&(i=m(u.transform,[i,e]));const b=a(u,s,i,d,n);if(b.length){if(!n.exceptions)return!0;f.throw(n,b[0].code,b[0].message,s,o)}return n.strict&&!c(u).length||(g&&(i=new g(i)),o[s]=i,m(o.$onSet),y&&m(o.$onUpdate)),!0},get(n,s){const i=e[s];let u=n[s];if("$eject"===s)return function(){return function(e){return c(e=Object.assign({},e),(t=>{o(e[t])&&(e[t]=e[t].$eject())})),e}(n)};if(!i)return n[s];function f(r,o){const c=Reflect.apply(r,n,o||[s,u,e]);return t(c)?u:c}return u=f(n.$onBeforeGet),r(i)&&(u=f(i,[n,e])),r(i.format)&&(u=f(i.format,[u,e])),u=f(n.$onGet),u},deleteProperty(t,r){const o=t[r];function s(n,s){return Reflect.apply(n,t,s||[r,o,e])}return(e[r]||{}).required&&f.throw(n,"required",`Property "${r}" is "required"`,r,t),s(t.$onBeforeDelete),Reflect.deleteProperty(t,r),s(t.$onDelete),s(t.$onUpdate),!0}})}}{constructor(e={},n={},o){super(e,o),c(e,(o=>{t(n[o])&&(t(e[o].default)?r(e[o])||(this[o]=n[o]):this[o]=e[o].default)})),this.$patch(n)}$patch(e){c(e,(t=>{this[t]=e[t]}))}$put(e){c(this,(t=>{e[t]?o(this[t])?this[t].$put(e[t]):this[t]=e[t]:this.$delete(t)})),c(e,(t=>{this[t]||(this[t]=e[t])}))}$delete(e){i(e).forEach((e=>{Reflect.deleteProperty(this,e)}))}}return l.settings={empty:e=>""===e||null===e||t(e),strict:!1,exceptions:{readonly:!1,required:!0,rule:!0,strict:!1,type:!0},methods:{$onBeforeGet:()=>{},$onBeforeSet:()=>{},$onBeforeUpdate:()=>{},$onDelete:()=>{},$onGet:()=>{},$onBeforeDelete:()=>{},$onSet:()=>{},$onUpdate:()=>{}}},l.create=function(t,r,n){n=u(l.settings,n);const o={[t]:class extends l{constructor(e){super(r,e,n)}}}[t];return o.checkErrors=function(e,t){const o={};return c(r,(s=>{let c;const u=e[s],f=r[s],l=p(f,s,n);l&&(c=l.checkErrors(u,t));let d=a(f,s,u,!1,n);c?o[s]=c:d.length&&(t&&(d=d.filter((e=>!i(t).includes(e.code)))),d.length&&(o[s]=d))})),!!c(o).length&&o},o.hydrate=function(t){return e(t)?t.map((e=>new o(e))):new o(t)},Object.assign(o.prototype,n.methods),o.schema=r,o},l})); | ||
/*! SmartModel v0.4.0 */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).SmartModel=t()}(this,(function(){"use strict";function e(e){return Array.isArray(e)}function t(e){return void 0===e}function r(e){return"function"==typeof e}function n(e){return e&&e.toString().startsWith("class")}function o(e){return e.prototype instanceof l||e instanceof l}function s(e){return e&&"[object Object]"===e.toString()}function i(e,t=function(){}){return Object.keys(e).map(t)}function c(e){return[].concat([],e)}function u(e,t){return t=Object.assign({},e,t),i(e,(r=>{s(e[r])&&s(t[r])&&(t[r]=Object.assign({},e[r],u(e[r],t[r])))})),t}class f extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function a(t,r,u,f,a){const p=[];return!a.strict||t&&i(t).length||p.push({message:`Property "${r}" can't be set in strict mode`,code:"strict"}),t.required&&a.empty(u)?(p.push({message:`Property "${r}" is "required"`,code:"required"}),p):t.readonly&&!f?(p.push({message:`Property '${r}' is 'readonly'`,code:"readonly"}),p):(void 0===u||(!t.type||!t.required&&a.empty(u)||o(t.type)&&s(u)||c(t.type).some((t=>function(t,r){const o=r&&r.toString().match(/^\s*function (\w+)/),s=(o?o[1]:"object").toLowerCase();if("date"===s&&t instanceof r)return!0;if("array"===s&&e(t))return!0;if("object"===s){if(n(r)&&t instanceof r)return!0;if(!n(r)&&typeof t===s)return!0}else if(typeof t===s)return!0;return!1}(u,t)))||p.push({message:`Property "${r}" has an invalid type "${typeof u}"`,code:"type"}),t.rule&&i(t.rule,(e=>{(0,t.rule[e])(u)&&p.push({message:`Property "${r}" breaks the "${e}" rule`,code:"rule:"+e})}))),p)}function p(e={},t,r){if(!e.type)return!1;const n=!!o(e.type)&&e.type,i=!!s(e.type)&&e.type;if(n||i){const o=n||l.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z1-9]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),i,r);return e.type=o,o}return!1}f.throw=function(e,t,r,n,o){const i=t.split(":")[0];if(!0===e.exceptions||s(e.exceptions)&&e.exceptions[i])throw new f({message:r,property:n,code:t,source:o&&o.constructor.name})};class l extends class{constructor(e,n){return new Proxy(this,{set(o,s,c){const u=e[s]||{},l=o[s],d=t(l),y=!(d||(h=c,$=l,JSON.stringify(h)===JSON.stringify($)));var h,$;const g=p(u,s,n);function m(r,n){const i=Reflect.apply(r,o,n||[s,c,l,e]);return t(i)?c:i}c=m(o.$onBeforeSet),y&&(c=m(o.$onBeforeUpdate)),r(u.transform)&&(c=m(u.transform,[c,e]));const b=a(u,s,c,d,n);if(b.length){if(!n.exceptions)return!0;f.throw(n,b[0].code,b[0].message,s,o)}return n.strict&&!i(u).length||(g&&(c=new g(c)),o[s]=c,m(o.$onSet),y&&m(o.$onUpdate)),!0},get(n,s){const c=e[s];let u=n[s];if(["$get"].includes(s))return function(){return function(e){return i(e=Object.assign({},e),(t=>{o(e[t])&&(e[t]=e[t].$get())})),e}(n)};if(!c)return n[s];function f(r,o){const i=Reflect.apply(r,n,o||[s,u,e]);return t(i)?u:i}return u=f(n.$onBeforeGet),r(c)&&(u=f(c,[n,e])),r(c.format)&&(u=f(c.format,[u,e])),u=f(n.$onGet),u},deleteProperty(t,r){const o=t[r];function s(n,s){return Reflect.apply(n,t,s||[r,o,e])}return(e[r]||{}).required&&f.throw(n,"required",`Property "${r}" is "required"`,r,t),s(t.$onBeforeDelete),Reflect.deleteProperty(t,r),s(t.$onDelete),s(t.$onUpdate),!0}})}}{constructor(e={},n={},o){super(e,o),i(e,(o=>{t(n[o])&&(t(e[o].default)?r(e[o])||(this[o]=n[o]):this[o]=e[o].default)})),this.$patch(n)}$patch(e){i(e,(t=>{this[t]=e[t]}))}$put(e){i(this,(t=>{e[t]?o(this[t])?this[t].$put(e[t]):this[t]=e[t]:this.$delete(t)})),i(e,(t=>{this[t]||(this[t]=e[t])}))}$post(e){return this.$put(e)}$delete(e){c(e).forEach((e=>{Reflect.deleteProperty(this,e)}))}}return l.settings={empty:e=>""===e||null===e||t(e),strict:!1,exceptions:{readonly:!1,required:!0,rule:!0,strict:!1,type:!0},methods:{$onBeforeGet:()=>{},$onBeforeSet:()=>{},$onBeforeUpdate:()=>{},$onDelete:()=>{},$onGet:()=>{},$onBeforeDelete:()=>{},$onSet:()=>{},$onUpdate:()=>{}}},l.create=function(t,r,n){n=u(l.settings,n);const o={[t]:class extends l{constructor(e){super(r,e,n)}}}[t];return o.$check=function(e,t){const o={};return i(r,(s=>{let i;const u=e[s],f=r[s],l=p(f,s,n);l&&(i=l.$check(u,t));let d=a(f,s,u,!1,n);i?o[s]=i:d.length&&(t&&(d=d.filter((e=>!c(t).includes(e.code)))),d.length&&(o[s]=d))})),!!i(o).length&&o},o.$hydrate=function(t){return e(t)?t.map((e=>new o(e))):new o(t)},Object.assign(o.prototype,n.methods),o.schema=r,o},l})); |
{ | ||
"name": "@jaysalvat/smart-model", | ||
"version": "0.3.4", | ||
"version": "0.4.0", | ||
"description": "Javascript object model", | ||
@@ -5,0 +5,0 @@ "main": "./build/smart-model.cjs.min.cjs", |
229
README.md
@@ -27,3 +27,3 @@ ``` | ||
- [x] Live cycle events | ||
- [ ] Proper documentation ^^ | ||
- [ ] Better documentation ^^ | ||
@@ -113,3 +113,3 @@ Works on modern browsers. | ||
{ | ||
strict: false, | ||
strict: true, | ||
exceptions: true, | ||
@@ -141,3 +141,3 @@ // Events | ||
```ssh | ||
```javascript | ||
Post { | ||
@@ -175,13 +175,80 @@ createdAt: 2020-01-31T09:50:00.000Z, | ||
| :---------- | :------ | :-- | ||
| type | any | The required type (*) of a value. You can set a schema or another model (*) in order to nest models | ||
| required | bool | The value is required. See `settings.empty` for the empty check function | ||
| readonly | bool | The value can't be overwritten | ||
| default | any | The default value if the property is undefined | ||
| transform | fn | A function to transform the value to set | ||
| format | fn | A function to format the value to get | ||
| rule | object | An object which contains the validation rules (**) | ||
| default | any | Default value if the property is undefined | ||
| format | fn | Function to format the value when its read | ||
| readonly | bool | Boolean to prevent a value to be overwritten | ||
| required | bool | Boolean to prevent a value to be empty | ||
| rule | object | Object which contains the validation rules | ||
| transform | fn | Function to transform the value when its written | ||
| type | any | Required type of a value | ||
#### [*] Type | ||
#### default | ||
Type can be `String`, `Boolean`, `Number`, `Date`, `Function` or a class. | ||
The default value if the property is undefined. | ||
#### format | ||
A function to format the value when its read. | ||
```javascript | ||
const Event = SmartModel.create('Event', { | ||
name: { | ||
type: String | ||
}, | ||
date: { | ||
type Date, | ||
format: (value) => new Date(value).toLocaleString() | ||
} | ||
}) | ||
``` | ||
** Use with caution **, it could be unexpected effects when object are copied (the formated value becomes the new value). | ||
Consider using a computed property instead. | ||
#### readonly | ||
The value can't be overwritten after init. | ||
#### required | ||
The value is required. See `settings.empty` for the empty value check function. | ||
#### rule | ||
An object which contains the validation rules | ||
Multiple rules of validation can be set on a property. | ||
```javascript | ||
const Discount = SmartModel.create('Discount', { | ||
percent: { | ||
type: Number, | ||
rule: { | ||
'min': {value) => value < 0, | ||
'max': {value) => value > 100 | ||
} | ||
} | ||
}) | ||
``` | ||
An exception is throw with the rule code when the condition if true. | ||
#### transform | ||
Function to transform the value when its written. | ||
```javascript | ||
const Event = SmartModel.create('Event', { | ||
name: { | ||
type: String | ||
}, | ||
date: { | ||
type Date, | ||
transform: (value) => new Date(value) | ||
} | ||
}) | ||
``` | ||
#### type | ||
The required type of a value. | ||
Type can be `Array`, `Boolean`, `Date`, `Function`, `Object`, `String` or a class. | ||
If a schema is set as a type, a nested model will be created. | ||
@@ -205,3 +272,3 @@ | ||
}, | ||
lastnaame: { | ||
lastname: { | ||
type: String | ||
@@ -214,3 +281,3 @@ } | ||
An existing Model can be set as a type in order to nest this Model. | ||
An existing model can be set as a type in order to nest this model. | ||
@@ -222,3 +289,3 @@ ```javascript | ||
}, | ||
lastnaame: { | ||
lastname: { | ||
type: String | ||
@@ -247,29 +314,15 @@ } | ||
#### [**]s Rule | ||
Multiple rules of validation can be set on a property. | ||
```javascript | ||
const Discount = SmartModel.create('Discount', { | ||
percent: { | ||
type: Number, | ||
rule: { | ||
'min': {value) => value < 0, | ||
'max': {value) => value > 100 | ||
} | ||
} | ||
}) | ||
``` | ||
### Settings | ||
| Option | Type | Default | Description | ||
| :---------- | :---------- | :------------- | :-- | ||
| strict | bool | false | Allow to set property not present in the schema | ||
| empty | fn | fn (***) | Function to check if a value is empty if required | ||
| exceptions | bool/object | object (****) | Throw exceptions on errors. can be `boolean` or òbject` for advanced settings | ||
| Option | Type | Default | Description | ||
| :---------- | :---------- | :-------- | :-- | ||
| empty | fn | fn | Function to check if a value is empty if required | ||
| exceptions | bool/object | object | Object of exceptions settings | ||
| methods | object | object | Object of custom methods | ||
| strict | bool | false | Boolean to set only properties defined in the schema | ||
#### [***] Empty check function | ||
#### empty | ||
The default function to check if a value is empty is: | ||
The function to check if a value is empty or not. | ||
Default function is the function below. | ||
@@ -280,4 +333,11 @@ ```javascript | ||
#### [****] Exceptions object | ||
#### strict | ||
If `strict`set to `true`, only properties defined in the schema will be set. | ||
#### exceptions | ||
Any type of thrown exceptions can be set individually. | ||
By default the `exceptions` settings is an object these default values: | ||
| Option | Type | Default | ||
@@ -291,11 +351,44 @@ | :-------- | :--- | :------ | ||
### Methods | ||
If `exceptions` is set to `true`, all types of exceptions will be thrown. | ||
If `exceptions` is set to `false`, all types of exceptions will be thrown. | ||
#### $put | ||
### methods | ||
Replace the model properties. Existing properties are deleted. | ||
Custom `methods` can be added to model. Those methods will be accessible in any instances of the model. | ||
```javascript | ||
const Article = SmartModel.create('Article', { | ||
body: { | ||
type: String | ||
} | ||
}, { | ||
methods: { | ||
excerpt(limit = 10) { | ||
return this.body.substr(0, limit) + '…' | ||
} | ||
} | ||
}) | ||
``` | ||
### Native methods | ||
#### $get | ||
Returns standard JSON from the model content. | ||
```javascript | ||
const article = new Article() | ||
const json = article.$get() | ||
``` | ||
#### $post / $put | ||
Replaces the model properties. | ||
Existing properties are deleted. | ||
An exception is thrown if the properties are required. | ||
```javascript | ||
const article = new Article() | ||
article.$put({ | ||
@@ -308,35 +401,49 @@ title: 'My article' | ||
Same as $put, but only passed property are updated. | ||
Same as $post / $put, but only passed properties are updated. | ||
#### $eject | ||
#### $delete | ||
Delete a or muliple properties of the model. | ||
An exception is thrown if the properties are required. | ||
```javascript | ||
const article = new Article() | ||
const json = article.$eject() | ||
article.$delete([ 'title', 'body' ]) | ||
``` | ||
### Custom methods | ||
#### $check | ||
Methods can be added to models. | ||
Static method. | ||
Returns an array of potential errors if a payload where passed to the model. | ||
```javascript | ||
const Article = SmartModel.create('Article', { | ||
body: { | ||
type: String | ||
} | ||
}, { | ||
methods: { | ||
excerpt(limit = 10) { | ||
return this.body.substr(0, limit) + '…' | ||
} | ||
} | ||
}) | ||
const errors = Article.$check(payload) | ||
``` | ||
## Callbacks | ||
#### $hydrate | ||
Models have some callbacks methods that are called when properties are set, get, updated or deleted. | ||
Static method. | ||
Turns an object or an array of objects into models. | ||
Useful with API responses. | ||
```javascript | ||
fetch('https://api.com/post') | ||
.then(response => response.json()) | ||
.then(response => Post.$hydrate(response)) | ||
.then(response) => { /* Array of hydrated Post models */ } | ||
``` | ||
```javascript | ||
fetch('https://api.com/post/1234') | ||
.then(response => response.json()) | ||
.then(response => Post.$hydrate(response)) | ||
.then(response) => { /* Hydrated Post model */ } | ||
``` | ||
## Hooks | ||
Hooks are triggered before and after properties are set, get, updated or deleted. | ||
```javascript | ||
const User = SmartModel.create('User', { | ||
@@ -348,2 +455,3 @@ username: { | ||
methods: { | ||
$onBeforeDelete() {} | ||
$onBeforeGet() {} | ||
@@ -354,3 +462,2 @@ $onBeforeSet() {} | ||
$onGet() {} | ||
$onBeforeDelete() {} | ||
$onSet() {} | ||
@@ -357,0 +464,0 @@ $onUpdate() {} |
@@ -45,10 +45,10 @@ /* eslint-disable camelcase */ | ||
mangle: { | ||
toplevel: false | ||
toplevel: true | ||
}, | ||
compress: { | ||
toplevel: false, | ||
toplevel: true, | ||
reduce_funcs: true, | ||
keep_infinity: true, | ||
pure_getters: true, | ||
passes: 10 | ||
passes: 3 | ||
} | ||
@@ -64,2 +64,7 @@ } | ||
if (format === 'iife' && minify) { | ||
terserMinify.mangle.toplevel = false | ||
terserMinify.compress.toplevel = false | ||
} | ||
configs.push({ | ||
@@ -66,0 +71,0 @@ input: SRC + '/index.js', |
@@ -24,2 +24,4 @@ | ||
// Virtual $get | ||
$patch(data) { | ||
@@ -51,2 +53,6 @@ keys(data, (key) => { | ||
$post(data) { | ||
return this.$put(data) | ||
} | ||
$delete(properties) { | ||
@@ -90,3 +96,3 @@ toArray(properties).forEach((key) => { | ||
Model.checkErrors = function (payload, filters) { | ||
Model.$check = function (payload, filters) { | ||
const invalidations = {} | ||
@@ -101,3 +107,3 @@ | ||
if (Nested) { | ||
subErrors = Nested.checkErrors(value, filters) | ||
subErrors = Nested.$check(value, filters) | ||
} | ||
@@ -123,3 +129,3 @@ | ||
Model.hydrate = function (payload) { | ||
Model.$hydrate = function (payload) { | ||
if (isArray(payload)) { | ||
@@ -126,0 +132,0 @@ return payload.map((item) => new Model(item)) |
@@ -65,3 +65,3 @@ import SmartModelError from './SmartModelError.js' | ||
if (property === '$eject') { | ||
if ([ '$get' ].includes(property)) { | ||
return function () { | ||
@@ -68,0 +68,0 @@ return eject(target) |
@@ -58,3 +58,3 @@ /* eslint-disable new-cap */ | ||
if (isSmartModel(target[key])) { | ||
target[key] = target[key].$eject() | ||
target[key] = target[key].$get() | ||
} | ||
@@ -61,0 +61,0 @@ }) |
@@ -433,3 +433,25 @@ /* eslint-disable prefer-reflect */ | ||
describe('Methods', function () { | ||
describe('$put', function () { | ||
describe('$get', function () { | ||
it('should works', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop1: { default: 'string1' }, | ||
prop2: { | ||
type: { | ||
nestedProp: { default: 'string2' } | ||
} | ||
} | ||
}) | ||
const model = new Model() | ||
const obj = model.$get() | ||
expect(JSON.stringify(obj)).to.be.deep.equal(JSON.stringify(model)) | ||
expect(model).to.be.instanceOf(SmartModel) | ||
expect(model.prop2).to.be.instanceOf(SmartModel) | ||
expect(obj).to.not.be.instanceOf(SmartModel) | ||
expect(obj.prop2).to.not.be.instanceOf(SmartModel) | ||
}) | ||
}) | ||
describe('$post / $put', function () { | ||
it('should replace model data', function () { | ||
@@ -449,3 +471,3 @@ const Model = SmartModel.create('Model', { | ||
model.$put({ | ||
model.$post({ | ||
prop1: 'newString1', | ||
@@ -532,28 +554,6 @@ prop3: { | ||
}) | ||
describe('$eject', function () { | ||
it('should works', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop1: { default: 'string1' }, | ||
prop2: { | ||
type: { | ||
nestedProp: { default: 'string2' } | ||
} | ||
} | ||
}) | ||
const model = new Model() | ||
const obj = model.$eject() | ||
expect(JSON.stringify(obj)).to.be.deep.equal(JSON.stringify(model)) | ||
expect(model).to.be.instanceOf(SmartModel) | ||
expect(model.prop2).to.be.instanceOf(SmartModel) | ||
expect(obj).to.not.be.instanceOf(SmartModel) | ||
expect(obj.prop2).to.not.be.instanceOf(SmartModel) | ||
}) | ||
}) | ||
}) | ||
describe('Static methods', function () { | ||
describe('CheckErrors', function () { | ||
describe('$check', function () { | ||
it('should return an array of model errors', function () { | ||
@@ -572,3 +572,3 @@ const Model = SmartModel.create('Model', { | ||
const errrors = Model.checkErrors({ | ||
const errrors = Model.$check({ | ||
prop2: 0, | ||
@@ -596,3 +596,3 @@ prop3: 'string', | ||
const errrors = Model.checkErrors({ | ||
const errrors = Model.$check({ | ||
prop2: 0, | ||
@@ -617,3 +617,3 @@ prop4: 0 | ||
const errrors = Model.checkErrors({ | ||
const errrors = Model.$check({ | ||
prop1: 'string', | ||
@@ -631,3 +631,3 @@ prop3: 'string' | ||
const errrors = Model.checkErrors({ | ||
const errrors = Model.$check({ | ||
prop: 'string' | ||
@@ -640,3 +640,3 @@ }) | ||
describe('Hydrate', function () { | ||
describe('$hydrate', function () { | ||
it('should hydrate a object', function () { | ||
@@ -652,3 +652,3 @@ const obj = { | ||
const model = Model.hydrate(obj) | ||
const model = Model.$hydrate(obj) | ||
@@ -671,3 +671,3 @@ expect(model.prop1).to.be.equal('format: string') | ||
const models = Model.hydrate(objs) | ||
const models = Model.$hydrate(objs) | ||
@@ -674,0 +674,0 @@ models.forEach((model, i) => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
118244
2900
513