@jaysalvat/smart-model
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -6,3 +6,3 @@ | ||
* https://github.com/jaysalvat/smart-model | ||
* @version 0.1.3 built 2021-02-18 19:03:38 | ||
* @version 0.1.4 built 2021-02-18 19:15:57 | ||
* @license ISC | ||
@@ -66,3 +66,3 @@ * @author Jay Salvat http://jaysalvat.com | ||
if (!rule(value)) { | ||
if (rule(value)) { | ||
errors.push({ | ||
@@ -72,3 +72,3 @@ message: `Invalid value '${key}' on property '${property}'`, | ||
value: value, | ||
expected: rule(value) | ||
expected: rule | ||
}); | ||
@@ -75,0 +75,0 @@ } |
@@ -1,2 +0,2 @@ | ||
/*! smartModel v0.1.3 */ | ||
function e(e){return""===e||null==e}function t(e){return"function"==typeof e}function n(e){return Array.isArray(e)}function r(t,r,o){const s=[];if(t.required&&e(o)&&s.push({message:`Invalid value 'required' on property '${r}'`,code:"required",value:o}),t.type){let c;const u=n(t.type)?t.type:[t.type];c=!(t.required||!e(o))||u.some((e=>typeof o==typeof e()||o instanceof e)),c||s.push({message:`Invalid type '${typeof o}' on property '${r}'`,code:"type",value:o,expected:u})}return t.rule&&Object.keys(t.rule).forEach((e=>{const n=t.rule[e];n(o)||s.push({message:`Invalid value '${e}' on property '${r}'`,code:e,value:o,expected:n(o)})})),s}class o extends Error{constructor(e){super(e.message),Object.assign(this,e)}}class s{constructor(e={}){this.schema=e}set(e,n,s){const u=this.schema,a=u[n],f=e[n],i=(p=s,l=f,!(JSON.stringify(p)===JSON.stringify(l)));var p,l;function y(t){return Reflect.apply(t,e,[n,s,f,u])}if(t(a))return!1;if(a.transform&&(s=a.transform(s)),y(e.onBeforeSet),i&&y(e.onBeforeUpdate),c.settings.exceptions){const e=r(a,n,s);e.length&&e.forEach((e=>{throw new o({message:e.message,property:n,code:e.code,value:e.value,expected:e.expected})}))}return e[n]=s,y(e.onSet),i&&y(e.onUpdate),!0}get(e,n){let r=e[n];const o=this.schema,s=o[n];if(!s)return e[n];function c(t,n){return Reflect.apply(t,e,n)}return c(e.onBeforeGet,[n,r,o]),t(s)&&(r=c(s,[e])),t(s.format)&&(r=c(s.format,[r,e])),c(e.onGet,[n,r,o]),r}}class c{constructor(e={},t={}){Object.keys(e).forEach((t=>{e[t].default&&(this[t]=e[t].default)})),this.feed(t)}feed(e){Object.keys(e).forEach((t=>{this[t]=e[t]}))}onSet(){}onGet(){}onUpdate(){}onBeforeSet(){}onBeforeGet(){}onBeforeUpdate(){}}c.settings={exceptions:!0},c.create=function(e,t={},n={}){const r={[e]:class extends c{constructor(e){return super(t,e),new Proxy(this,new s(t))}}};return Object.assign(r[e].prototype,n),r[e]},c.throwExeptions=function(e){c.settings.exeptions=e},c.checkErrors=function(e,t){const n={};return Object.keys(e).forEach((o=>{const s=t[o],c=e[o],u=r(c,o,s);"function"!=typeof c&&u.length&&(n[o]=u)})),!!Object.keys(n).length&&n},c.hydrate=function(e,t){return n(t)?t.map((t=>new e(t))):new e(t)};export default c; | ||
/*! smartModel v0.1.4 */ | ||
function e(e){return""===e||null==e}function t(e){return"function"==typeof e}function n(e){return Array.isArray(e)}function r(t,r,o){const s=[];if(t.required&&e(o)&&s.push({message:`Invalid value 'required' on property '${r}'`,code:"required",value:o}),t.type){let c;const u=n(t.type)?t.type:[t.type];c=!(t.required||!e(o))||u.some((e=>typeof o==typeof e()||o instanceof e)),c||s.push({message:`Invalid type '${typeof o}' on property '${r}'`,code:"type",value:o,expected:u})}return t.rule&&Object.keys(t.rule).forEach((e=>{const n=t.rule[e];n(o)&&s.push({message:`Invalid value '${e}' on property '${r}'`,code:e,value:o,expected:n})})),s}class o extends Error{constructor(e){super(e.message),Object.assign(this,e)}}class s{constructor(e={}){this.schema=e}set(e,n,s){const u=this.schema,a=u[n],f=e[n],i=(p=s,l=f,!(JSON.stringify(p)===JSON.stringify(l)));var p,l;function y(t){return Reflect.apply(t,e,[n,s,f,u])}if(t(a))return!1;if(a.transform&&(s=a.transform(s)),y(e.onBeforeSet),i&&y(e.onBeforeUpdate),c.settings.exceptions){const e=r(a,n,s);e.length&&e.forEach((e=>{throw new o({message:e.message,property:n,code:e.code,value:e.value,expected:e.expected})}))}return e[n]=s,y(e.onSet),i&&y(e.onUpdate),!0}get(e,n){let r=e[n];const o=this.schema,s=o[n];if(!s)return e[n];function c(t,n){return Reflect.apply(t,e,n)}return c(e.onBeforeGet,[n,r,o]),t(s)&&(r=c(s,[e])),t(s.format)&&(r=c(s.format,[r,e])),c(e.onGet,[n,r,o]),r}}class c{constructor(e={},t={}){Object.keys(e).forEach((t=>{e[t].default&&(this[t]=e[t].default)})),this.feed(t)}feed(e){Object.keys(e).forEach((t=>{this[t]=e[t]}))}onSet(){}onGet(){}onUpdate(){}onBeforeSet(){}onBeforeGet(){}onBeforeUpdate(){}}c.settings={exceptions:!0},c.create=function(e,t={},n={}){const r={[e]:class extends c{constructor(e){return super(t,e),new Proxy(this,new s(t))}}};return Object.assign(r[e].prototype,n),r[e]},c.throwExeptions=function(e){c.settings.exeptions=e},c.checkErrors=function(e,t){const n={};return Object.keys(e).forEach((o=>{const s=t[o],c=e[o],u=r(c,o,s);"function"!=typeof c&&u.length&&(n[o]=u)})),!!Object.keys(n).length&&n},c.hydrate=function(e,t){return n(t)?t.map((t=>new e(t))):new e(t)};export default c; |
@@ -6,3 +6,3 @@ | ||
* https://github.com/jaysalvat/smart-model | ||
* @version 0.1.3 built 2021-02-18 19:03:38 | ||
* @version 0.1.4 built 2021-02-18 19:15:57 | ||
* @license ISC | ||
@@ -72,3 +72,3 @@ * @author Jay Salvat http://jaysalvat.com | ||
if (!rule(value)) { | ||
if (rule(value)) { | ||
errors.push({ | ||
@@ -78,3 +78,3 @@ message: `Invalid value '${key}' on property '${property}'`, | ||
value: value, | ||
expected: rule(value) | ||
expected: rule | ||
}); | ||
@@ -81,0 +81,0 @@ } |
@@ -1,2 +0,2 @@ | ||
/*! smartModel v0.1.3 */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).smartModel={})}(this,(function(e){"use strict";function t(e){return""===e||null==e}function n(e){return"function"==typeof e}function o(e){return Array.isArray(e)}function r(e,n,r){const s=[];if(e.required&&t(r)&&s.push({message:`Invalid value 'required' on property '${n}'`,code:"required",value:r}),e.type){let c;const u=o(e.type)?e.type:[e.type];c=!(e.required||!t(r))||u.some((e=>typeof r==typeof e()||r instanceof e)),c||s.push({message:`Invalid type '${typeof r}' on property '${n}'`,code:"type",value:r,expected:u})}return e.rule&&Object.keys(e.rule).forEach((t=>{const o=e.rule[t];o(r)||s.push({message:`Invalid value '${t}' on property '${n}'`,code:t,value:r,expected:o(r)})})),s}class s extends Error{constructor(e){super(e.message),Object.assign(this,e)}}class c{constructor(e={}){this.schema=e}set(e,t,o){const c=this.schema,f=c[t],i=e[t],a=(p=o,d=i,!(JSON.stringify(p)===JSON.stringify(d)));var p,d;function l(n){return Reflect.apply(n,e,[t,o,i,c])}if(n(f))return!1;if(f.transform&&(o=f.transform(o)),l(e.onBeforeSet),a&&l(e.onBeforeUpdate),u.settings.exceptions){const e=r(f,t,o);e.length&&e.forEach((e=>{throw new s({message:e.message,property:t,code:e.code,value:e.value,expected:e.expected})}))}return e[t]=o,l(e.onSet),a&&l(e.onUpdate),!0}get(e,t){let o=e[t];const r=this.schema,s=r[t];if(!s)return e[t];function c(t,n){return Reflect.apply(t,e,n)}return c(e.onBeforeGet,[t,o,r]),n(s)&&(o=c(s,[e])),n(s.format)&&(o=c(s.format,[o,e])),c(e.onGet,[t,o,r]),o}}class u{constructor(e={},t={}){Object.keys(e).forEach((t=>{e[t].default&&(this[t]=e[t].default)})),this.feed(t)}feed(e){Object.keys(e).forEach((t=>{this[t]=e[t]}))}onSet(){}onGet(){}onUpdate(){}onBeforeSet(){}onBeforeGet(){}onBeforeUpdate(){}}u.settings={exceptions:!0},u.create=function(e,t={},n={}){const o={[e]:class extends u{constructor(e){return super(t,e),new Proxy(this,new c(t))}}};return Object.assign(o[e].prototype,n),o[e]},u.throwExeptions=function(e){u.settings.exeptions=e},u.checkErrors=function(e,t){const n={};return Object.keys(e).forEach((o=>{const s=t[o],c=e[o],u=r(c,o,s);"function"!=typeof c&&u.length&&(n[o]=u)})),!!Object.keys(n).length&&n},u.hydrate=function(e,t){return o(t)?t.map((t=>new e(t))):new e(t)},e.default=u,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
/*! smartModel v0.1.4 */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).smartModel={})}(this,(function(e){"use strict";function t(e){return""===e||null==e}function n(e){return"function"==typeof e}function o(e){return Array.isArray(e)}function r(e,n,r){const s=[];if(e.required&&t(r)&&s.push({message:`Invalid value 'required' on property '${n}'`,code:"required",value:r}),e.type){let c;const u=o(e.type)?e.type:[e.type];c=!(e.required||!t(r))||u.some((e=>typeof r==typeof e()||r instanceof e)),c||s.push({message:`Invalid type '${typeof r}' on property '${n}'`,code:"type",value:r,expected:u})}return e.rule&&Object.keys(e.rule).forEach((t=>{const o=e.rule[t];o(r)&&s.push({message:`Invalid value '${t}' on property '${n}'`,code:t,value:r,expected:o})})),s}class s extends Error{constructor(e){super(e.message),Object.assign(this,e)}}class c{constructor(e={}){this.schema=e}set(e,t,o){const c=this.schema,f=c[t],i=e[t],a=(p=o,d=i,!(JSON.stringify(p)===JSON.stringify(d)));var p,d;function l(n){return Reflect.apply(n,e,[t,o,i,c])}if(n(f))return!1;if(f.transform&&(o=f.transform(o)),l(e.onBeforeSet),a&&l(e.onBeforeUpdate),u.settings.exceptions){const e=r(f,t,o);e.length&&e.forEach((e=>{throw new s({message:e.message,property:t,code:e.code,value:e.value,expected:e.expected})}))}return e[t]=o,l(e.onSet),a&&l(e.onUpdate),!0}get(e,t){let o=e[t];const r=this.schema,s=r[t];if(!s)return e[t];function c(t,n){return Reflect.apply(t,e,n)}return c(e.onBeforeGet,[t,o,r]),n(s)&&(o=c(s,[e])),n(s.format)&&(o=c(s.format,[o,e])),c(e.onGet,[t,o,r]),o}}class u{constructor(e={},t={}){Object.keys(e).forEach((t=>{e[t].default&&(this[t]=e[t].default)})),this.feed(t)}feed(e){Object.keys(e).forEach((t=>{this[t]=e[t]}))}onSet(){}onGet(){}onUpdate(){}onBeforeSet(){}onBeforeGet(){}onBeforeUpdate(){}}u.settings={exceptions:!0},u.create=function(e,t={},n={}){const o={[e]:class extends u{constructor(e){return super(t,e),new Proxy(this,new c(t))}}};return Object.assign(o[e].prototype,n),o[e]},u.throwExeptions=function(e){u.settings.exeptions=e},u.checkErrors=function(e,t){const n={};return Object.keys(e).forEach((o=>{const s=t[o],c=e[o],u=r(c,o,s);"function"!=typeof c&&u.length&&(n[o]=u)})),!!Object.keys(n).length&&n},u.hydrate=function(e,t){return o(t)?t.map((t=>new e(t))):new e(t)},e.default=u,Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@jaysalvat/smart-model", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Javascript object model", | ||
@@ -5,0 +5,0 @@ "main": "./build/smart-model.umd.js", |
@@ -44,2 +44,4 @@ [![npm version](https://badge.fury.io/js/%40jaysalvat%2Fsmart-model.svg)](https://badge.fury.io/js/%40jaysalvat%2Fsmart-model) | ||
```javascript | ||
import readingTime from 'reading-time'; | ||
const Post = SmartModel.create('Post', { | ||
@@ -53,2 +55,6 @@ title: { | ||
type: String, | ||
rule: { | ||
'tooShort': (calue) => value.length < 100, | ||
'tooLong': (calue) => value.length > 1000, | ||
} | ||
}, | ||
@@ -59,3 +65,3 @@ createdAt: { | ||
transform: (value) => new Date(value), | ||
format: (value) => new Date().toLocaleString() | ||
format: (value) => value.toLocaleString() | ||
}, | ||
@@ -66,11 +72,14 @@ updatedAt: { | ||
transform: (value) => new Date(value), | ||
format: (value) => new Date().toLocaleString() | ||
format: (value) => value.toLocaleString() | ||
}, | ||
bodyLength: (post) => post.body.length, | ||
readingTime: (post) => readingTime(post.body) | ||
}, { | ||
onUpdate() { | ||
this.updateAt = new Date() | ||
this.updatedAt = new Date() | ||
} | ||
}) | ||
``` | ||
```javascript | ||
const post = new Post({ | ||
@@ -77,0 +86,0 @@ title: 'my new post', |
import { isFn, isEqual, checkErrors } from './utils.js' | ||
// import { isFn, isEqual, checkErrors } from './utils.js' | ||
import Model from './Model.js' | ||
import ModelError from './ModelError.js' | ||
// import Model from './Model.js' | ||
// import ModelError from './ModelError.js' | ||
class ModelProxy { | ||
#schema; | ||
// class ModelProxy { | ||
// #schema; | ||
constructor(modelSchema = {}) { | ||
this.#schema = modelSchema | ||
// constructor(modelSchema = {}) { | ||
// this.#schema = modelSchema | ||
const proxy = new Proxy(this, { | ||
// const proxy = new Proxy(this, { | ||
// Setter | ||
// // Setter | ||
set(target, property, value) { | ||
const schema = target.#schema | ||
const entry = schema[property] | ||
const oldValue = target[property] | ||
const updated = !isEqual(value, oldValue) | ||
// set(target, property, value) { | ||
// const schema = target.#schema | ||
// const entry = schema[property] | ||
// const oldValue = target[property] | ||
// const updated = !isEqual(value, oldValue) | ||
console.log(entry, property, value); | ||
// console.log(entry, property, value); | ||
function trigger(method) { | ||
return Reflect.apply(method, target, [ property, value, oldValue, schema ]) | ||
} | ||
// function trigger(method) { | ||
// return Reflect.apply(method, target, [ property, value, oldValue, schema ]) | ||
// } | ||
if (isFn(entry)) { | ||
return false | ||
} | ||
// if (isFn(entry)) { | ||
// return false | ||
// } | ||
if (entry.transform) { | ||
value = entry.transform(value) | ||
} | ||
// if (entry.transform) { | ||
// value = entry.transform(value) | ||
// } | ||
trigger(target.onBeforeSet) | ||
// trigger(target.onBeforeSet) | ||
if (updated) { | ||
trigger(target.onBeforeUpdate) | ||
} | ||
// if (updated) { | ||
// trigger(target.onBeforeUpdate) | ||
// } | ||
if (Model.settings.exeptions) { | ||
const errors = checkErrors(entry, property, value) | ||
// if (Model.settings.exeptions) { | ||
// const errors = checkErrors(entry, property, value) | ||
if (errors.length) { | ||
errors.forEach((error) => { | ||
throw new ModelError({ | ||
message: error.message, | ||
property: property, | ||
type: error.type, | ||
value: error.value, | ||
expected: error.expected | ||
}) | ||
}) | ||
} | ||
} | ||
// if (errors.length) { | ||
// errors.forEach((error) => { | ||
// throw new ModelError({ | ||
// message: error.message, | ||
// property: property, | ||
// type: error.type, | ||
// value: error.value, | ||
// expected: error.expected | ||
// }) | ||
// }) | ||
// } | ||
// } | ||
target[property] = value | ||
// target[property] = value | ||
trigger(target.onSet) | ||
// trigger(target.onSet) | ||
if (updated) { | ||
trigger(target.onUpdate) | ||
} | ||
// if (updated) { | ||
// trigger(target.onUpdate) | ||
// } | ||
return true | ||
}, | ||
// return true | ||
// }, | ||
// Getter | ||
// // Getter | ||
get(target, property) { | ||
if(target.hasOwnProperty(property)) { | ||
return target[property] | ||
} | ||
// get(target, property) { | ||
// if(target.hasOwnProperty(property)) { | ||
// return target[property] | ||
// } | ||
let value = target[property] | ||
const schema = target.#schema | ||
const entry = schema[property] | ||
// let value = target[property] | ||
// const schema = target.#schema | ||
// const entry = schema[property] | ||
if (!entry) { | ||
return | ||
} | ||
// if (!entry) { | ||
// return | ||
// } | ||
function trigger(method, args) { | ||
return Reflect.apply(method, target, args) | ||
} | ||
// function trigger(method, args) { | ||
// return Reflect.apply(method, target, args) | ||
// } | ||
trigger(target.onBeforeGet, [ property, value, schema ]) | ||
// trigger(target.onBeforeGet, [ property, value, schema ]) | ||
if (isFn(entry)) { | ||
value = trigger(entry, [ target ]) | ||
} | ||
// if (isFn(entry)) { | ||
// value = trigger(entry, [ target ]) | ||
// } | ||
if (isFn(entry.format)) { | ||
value = trigger(entry.format, [ value, target ]) | ||
} | ||
// if (isFn(entry.format)) { | ||
// value = trigger(entry.format, [ value, target ]) | ||
// } | ||
trigger(target.onGet, [ property, value, schema ]) | ||
// trigger(target.onGet, [ property, value, schema ]) | ||
return value | ||
} | ||
}) | ||
// return value | ||
// } | ||
// }) | ||
return proxy | ||
} | ||
} | ||
// return proxy | ||
// } | ||
// } | ||
export default ModelProxy | ||
// export default ModelProxy |
@@ -57,3 +57,3 @@ | ||
if (!rule(value)) { | ||
if (rule(value)) { | ||
errors.push({ | ||
@@ -63,3 +63,3 @@ message: `Invalid value '${key}' on property '${property}'`, | ||
value: value, | ||
expected: rule(value) | ||
expected: rule | ||
}) | ||
@@ -66,0 +66,0 @@ } |
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
29304
121