@chialab/backbone
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -1,4 +0,4 @@ | ||
var J=(e,t,r)=>{if(!t.has(e))throw TypeError("Cannot "+r)};var s=(e,t,r)=>(J(e,t,"read from private field"),r?r.call(e):t.get(e)),d=(e,t,r)=>{if(t.has(e))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(e):t.set(e,r)},k=(e,t,r,o)=>(J(e,t,"write to private field"),o?o.call(e,r):t.set(e,r),r);import A from"ajv";import P from"ajv-formats";var p,x=class{constructor(){d(this,p,{})}on(t,r){s(this,p)[t]=s(this,p)[t]||[],s(this,p)[t].push(r)}off(t,r){let o=s(this,p)[t];if(!o)return;let n=o.indexOf(r);n!==-1&&s(this,p)[t].splice(n,1)}trigger(t,r){let o=s(this,p)[t];!o||o.forEach(n=>n(r))}};p=new WeakMap;var a,w=class extends x{constructor(t=[]){super();d(this,a,void 0);k(this,a,t)}get length(){return s(this,a).length}add(...t){return t=t.filter(r=>!this.has(r)),s(this,a).push(...t),this.trigger("change",{added:t,removed:[],length:this.length}),this}insert(t,...r){return s(this,a).splice(t,0,...r),this}remove(...t){return t=t.filter(r=>this.has(r)),t.forEach(r=>{s(this,a).splice(this.indexOf(r),1)}),this.trigger("change",{added:[],removed:t,length:this.length}),this}indexOf(t){return s(this,a).indexOf(t)}item(t){return s(this,a)[t]||null}itemById(t){let r=s(this,a);for(let o=0,n=r.length;o<n;o++)if(r[o].getId()===t)return r[o];return null}has(t){return this.indexOf(t)!==-1}forEach(t){return s(this,a).forEach(t)}some(t){return s(this,a).some(t)}find(t){return s(this,a).find(t)}map(t){return s(this,a).map(t)}entries(){return s(this,a).entries()}filter(t){let r=this.constructor;return new r(s(this,a).filter(t))}reduce(t,r){return s(this,a).reduce(t,r)}get(){return s(this,a).slice(0)}clone(){return new w(this.get())}toJSON(){return this.get().map(t=>t.toJSON())}},O=w;a=new WeakMap;var l=new A({keywords:[{keyword:"__esModule"}]});P(l);l.addFormat("text",e=>typeof e=="string");l.addFormat("html",e=>typeof e=="string");l.addKeyword({keyword:"formatOptions",valid:!0});l.addKeyword({keyword:"form",metaSchema:{type:"object",properties:{type:{type:"string"}}}});l.addKeyword({keyword:"collection",validate(e,t){return t instanceof O}});function j(e,t=[]){return Array.isArray(e)?e.forEach(r=>j(r,t)):e.oneOf?j(e.oneOf,t):e.$ref&&t.push(e.$ref),t}var m=class{static register(t,r){if(this.registry.has(t))throw new Error(`Model "${t}" has already been registered.`);this.registry.set(t,r)}static get(t){if(!this.registry.has(t))throw new Error(`Model "${t}" is not registered.`);return this.registry.get(t)}static entries(){return this.registry.entries()}};m.registry=new Map;function b(e){if(Object.prototype.hasOwnProperty.call(e,"schema"))return e.schema;let t=e.schema?JSON.parse(JSON.stringify(e.schema)):{};return e.schema=t,t}function u(e,t){let r=b(e),o=r.properties=r.properties||{};return o[t]=o[t]||{}}function R(e,t,r,o,n){let i=e.mappings?{...e.mappings}:{};return e.mappings=i,i[t]={name:r,type:o,serialize:n},i}function G(e){return function(t){let r=b(t);r.$id=e,r.type="object",t.type=e,t.validator=l.addSchema(r,e).compile(r),m.register(e,t)}}function Q(e){return function(t,r){let o=t.constructor,n=u(o,r);n.type=e}}function W(){return function(e,t){let r=e.constructor,o=u(r,t);o.readOnly=!0}}function X(){return function(e,t){let r=e.constructor,o=b(r);(o.required=o.required||[]).push(t)}}function Y(e,t={}){return function(r,o){let n=r.constructor,i=u(n,o);i.format=e,i.formatOptions=t}}function Z(e){return function(t,r){let o=t.constructor,n=u(o,r);n.default=e}}function tt(e){return function(t,r){let o=t.constructor,n=u(o,r);n.type="string",n.enum=Array.isArray(e)?e:Object.values(e)}}function et(e,t=!1){return function(r,o){let n=r.constructor,i=u(n,o);i.collection=!0,t?i.items={$ref:e.type}:i.items={allOf:[{$ref:e.type}]}}}function rt(e,t,r="string",o=n=>n){return function(n,i){let c=n.constructor,E=u(c,i);E.$ref=e.type,t&&R(c,i,t,r,o)}}function ot(e){return function(t,r){let o=t.constructor,n=u(o,r),i=n.form=n.form||{};Object.assign(i,e)}}function S(e){if(e instanceof F){let t={},r=e.constructor.mappings;for(let o in e){let n=e[o];r[o]?t[r[o].name]=S(r[o].serialize(n)):t[o]=S(n)}return t}return Array.isArray(e)||e instanceof O?e.map(t=>S(t)):e}function N(e,t){let r=t.schema,o=r.properties||{},n=r.mappings||{},i={};for(let c in e){let y=Object.keys(n).find(g=>n[g].name===c)||c,f=o[y];if(f){let g=e[y];if(f.type==="array"){let M=Array.isArray(f.items)?f.items[0]:f.items;if(M.$ref){let C=m.get(M.$ref);if(C){i[y]=g.map($=>N($,C));continue}}}else if(f.$ref){let M=m.get(f.$ref);if(M){i[y]=N(g,M);continue}}i[y]=g}}return new t(i)}var v,T,F=class extends x{constructor(t){super();d(this,v,[]);d(this,T,void 0);if(t){let o=this.getSchema().properties||{};for(let i in o){let c=o[i];"default"in c&&!(i in t)&&(t[i]=c.default)}let n=this.constructor.validator;if(n(t))for(let i in t)this[i]=t[i];else throw new Error(`Invalid init properties. | ||
var E=(e,t,r)=>{if(!t.has(e))throw TypeError("Cannot "+r)};var s=(e,t,r)=>(E(e,t,"read from private field"),r?r.call(e):t.get(e)),m=(e,t,r)=>{if(t.has(e))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(e):t.set(e,r)},C=(e,t,r,o)=>(E(e,t,"write to private field"),o?o.call(e,r):t.set(e,r),r);import $ from"ajv";import A from"ajv-formats";var p,x=class{constructor(){m(this,p,{})}on(t,r){s(this,p)[t]=s(this,p)[t]||[],s(this,p)[t].push(r)}off(t,r){let o=s(this,p)[t];if(!o)return;let n=o.indexOf(r);n!==-1&&s(this,p)[t].splice(n,1)}trigger(t,r){let o=s(this,p)[t];!o||o.forEach(n=>n(r))}};p=new WeakMap;var a,T=class extends x{constructor(t=[]){super();m(this,a,void 0);C(this,a,t)}get length(){return s(this,a).length}add(...t){return t=t.filter(r=>!this.has(r)),s(this,a).push(...t),this.trigger("change",{added:t,removed:[],length:this.length}),this}insert(t,...r){return s(this,a).splice(t,0,...r),this}remove(...t){return t=t.filter(r=>this.has(r)),t.forEach(r=>{s(this,a).splice(this.indexOf(r),1)}),this.trigger("change",{added:[],removed:t,length:this.length}),this}indexOf(t){return s(this,a).indexOf(t)}item(t){return s(this,a)[t]||null}itemById(t){let r=s(this,a);for(let o=0,n=r.length;o<n;o++)if(r[o].getId()===t)return r[o];return null}has(t){return this.indexOf(t)!==-1}forEach(t){return s(this,a).forEach(t)}some(t){return s(this,a).some(t)}find(t){return s(this,a).find(t)}map(t){return s(this,a).map(t)}entries(){return s(this,a).entries()}filter(t){let r=this.constructor;return new r(s(this,a).filter(t))}reduce(t,r){return s(this,a).reduce(t,r)}get(){return s(this,a).slice(0)}clone(){return new T(this.get())}toJSON(){return this.get().map(t=>t.toJSON())}*[Symbol.iterator](){for(let t of s(this,a))yield t}},O=T;a=new WeakMap;var l=new $({keywords:[{keyword:"__esModule"}]});A(l);l.addFormat("text",e=>typeof e=="string");l.addFormat("html",e=>typeof e=="string");l.addKeyword({keyword:"formatOptions",valid:!0});l.addKeyword({keyword:"form",metaSchema:{type:"object",properties:{type:{type:"string"}}}});l.addKeyword({keyword:"collection",validate(e,t){return t instanceof O}});function J(e,t=[]){return Array.isArray(e)?e.forEach(r=>J(r,t)):e.oneOf?J(e.oneOf,t):e.$ref&&t.push(e.$ref),t}var d=class{static register(t,r){if(this.registry.has(t))throw new Error(`Model "${t}" has already been registered.`);this.registry.set(t,r)}static get(t){if(!this.registry.has(t))throw new Error(`Model "${t}" is not registered.`);return this.registry.get(t)}static entries(){return this.registry.entries()}};d.registry=new Map;function k(e){if(Object.prototype.hasOwnProperty.call(e,"schema"))return e.schema;let t=e.schema?JSON.parse(JSON.stringify(e.schema)):{};return e.schema=t,t}function u(e,t){let r=k(e),o=r.properties=r.properties||{};return o[t]=o[t]||{}}function P(e,t,r,o,n){let i=e.mappings?{...e.mappings}:{};return e.mappings=i,i[t]={name:r,type:o,serialize:n},i}function B(e){return function(t){let r=k(t);r.$id=e,r.type="object",t.type=e,t.validator=l.addSchema(r,e).compile(r),d.register(e,t)}}function G(e){return function(t,r){let o=t.constructor,n=u(o,r);n.type=e}}function Q(){return function(e,t){let r=e.constructor,o=u(r,t);o.readOnly=!0}}function W(){return function(e,t){let r=e.constructor,o=k(r);(o.required=o.required||[]).push(t)}}function X(e,t={}){return function(r,o){let n=r.constructor,i=u(n,o);i.format=e,i.formatOptions=t}}function Y(e){return function(t,r){let o=t.constructor,n=u(o,r);n.default=e}}function Z(e){return function(t,r){let o=t.constructor,n=u(o,r);n.type="string",n.enum=Array.isArray(e)?e:Object.values(e)}}function tt(e,t=!1){return function(r,o){let n=r.constructor,i=u(n,o);i.collection=!0,t?i.items={$ref:e.type}:i.items={allOf:[{$ref:e.type}]}}}function et(e,t,r="string",o=n=>n){return function(n,i){let c=n.constructor,b=u(c,i);b.$ref=e.type,t&&P(c,i,t,r,o)}}function rt(e){return function(t,r){let o=t.constructor,n=u(o,r),i=n.form=n.form||{};Object.assign(i,e)}}function S(e){if(e instanceof w){let t={},r=e.constructor.mappings;for(let o in e){let n=e[o];r[o]?t[r[o].name]=S(r[o].serialize(n)):t[o]=S(n)}return t}return Array.isArray(e)||e instanceof O?e.map(t=>S(t)):e}function j(e,t){let r=t.schema,o=r.properties||{},n=r.mappings||{},i={};for(let c in e){let y=Object.keys(n).find(g=>n[g].name===c)||c,f=o[y];if(f){let g=e[y];if(f.type==="array"){let M=Array.isArray(f.items)?f.items[0]:f.items;if(M.$ref){let F=d.get(M.$ref);if(F){i[y]=g.map(N=>j(N,F));continue}}}else if(f.$ref){let M=d.get(f.$ref);if(M){i[y]=j(g,M);continue}}i[y]=g}}return new t(i)}var v,w=class extends x{constructor(t){super();m(this,v,[]);if(t){let o=this.getSchema().properties||{};for(let i in o){let c=o[i];"default"in c&&!(i in t)&&(t[i]=c.default)}let n=this.constructor.validator;if(n(t))for(let i in t)this[i]=t[i];else throw new Error(`Invalid init properties. | ||
${n.errors?.map(i=>JSON.stringify(i)).join(` | ||
`)}`)}}getId(){}getParent(){return s(this,T)}setParent(t){k(this,T,t)}getModelType(){return this.constructor.type}getSchema(){return this.constructor.schema}set(t,r=!1){let o=this.toJSON(),n=!1;for(let i in t){let c=t[i];(c!==this[i]||r)&&(n=!0,this[i]=c)}n&&(s(this,v).push(o),this.trigger("change"))}hasChanges(){return!!s(this,v).length}validate(){return this.constructor.validator(this.toJSON())}getErrors(){let t=this.constructor.validator;return t(this.toJSON())?[]:t.errors||[]}toJSON(){return S(this)}};v=new WeakMap,T=new WeakMap,F.mappings={};export{O as Collection,Z as Default,x as Emitter,tt as Enum,ot as Form,Y as Format,et as Items,F as Model,W as ReadOnly,rt as Ref,m as Registry,X as Required,G as Schema,Q as Type,j as collectReferences,l as schemaManager,S as serialize,N as unserialize}; | ||
`)}`)}}getId(){}getModelType(){return this.constructor.type}getSchema(){return this.constructor.schema}set(t,r=!1){let o=this.toJSON(),n=!1;for(let i in t){let c=t[i];(c!==this[i]||r)&&(n=!0,this[i]=c)}n&&(s(this,v).push(o),this.trigger("change"))}hasChanges(){return!!s(this,v).length}validate(){return this.constructor.validator(this.toJSON())}getErrors(){let t=this.constructor.validator;return t(this.toJSON())?[]:t.errors||[]}toJSON(){return S(this)}};v=new WeakMap,w.mappings={};export{O as Collection,Y as Default,x as Emitter,Z as Enum,rt as Form,X as Format,tt as Items,w as Model,Q as ReadOnly,et as Ref,d as Registry,W as Required,B as Schema,G as Type,J as collectReferences,l as schemaManager,S as serialize,j as unserialize}; | ||
//# sourceMappingURL=backbone.js.map |
{ | ||
"name": "@chialab/backbone", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Models, Collections and Form helpers for JS apps.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -29,2 +29,3 @@ import type { Model } from './Model'; | ||
toJSON(): import("./Model").Props[]; | ||
[Symbol.iterator](): Generator<T, void, unknown>; | ||
} |
@@ -45,4 +45,2 @@ import type { JSONSchema, ValidateFunction } from '../Lib/Schema'; | ||
getId(): number | string | void; | ||
getParent(): Model<Props> | undefined; | ||
setParent(parent: Model): void; | ||
getModelType(): string; | ||
@@ -49,0 +47,0 @@ getSchema(): JSONSchema; |
Sorry, the diff of this file is not supported yet
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
71537
208