@chialab/backbone
Advanced tools
Comparing version 1.4.1 to 1.5.0
@@ -1,4 +0,4 @@ | ||
var A=(e,t,r)=>{if(!t.has(e))throw TypeError("Cannot "+r)};var a=(e,t,r)=>(A(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)},D=(e,t,r,n)=>(A(e,t,"write to private field"),n?n.call(e,r):t.set(e,r),r);import j from"ajv";import P from"ajv-formats";import z from"ajv-keywords/dist/definitions/instanceof.js";var C=new j({keywords:[{keyword:"__esModule"},z(),{keyword:"formatOptions",valid:!0},{keyword:"form",metaSchema:{type:"object",properties:{type:{type:"string"}}}}],formats:{text:e=>typeof e=="string",html:e=>typeof e=="string",code:e=>typeof e=="string"},allowUnionTypes:!0,strictTypes:!1});P(C);function T(e,t){z.CONSTRUCTORS[e]=t}function K(e){return z.CONSTRUCTORS[e]}function F(e,t=[]){return Array.isArray(e)?e.forEach(r=>F(r,t)):e.oneOf?F(e.oneOf,t):e.allOf?F(e.allOf,t):e.$ref&&t.push(e.$ref),t}var h=class{static register(t,r){if(this.registry.has(t))throw new Error(`Model "${t}" has already been registered.`);T(t,r),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()}};h.registry=new Map;var l,x=class{constructor(){M(this,l,{})}on(t,r){a(this,l)[t]=a(this,l)[t]||[],a(this,l)[t].push(r)}off(t,r){let n=a(this,l)[t];if(!n)return;let o=n.indexOf(r);o!==-1&&a(this,l)[t].splice(o,1)}trigger(t,r){let n=a(this,l)[t];!n||n.forEach(o=>o(r))}};l=new WeakMap;var c,b=class extends x{constructor(t=[]){super();M(this,c,void 0);D(this,c,t)}get length(){return a(this,c).length}add(...t){return t=t.filter(r=>!this.has(r)),a(this,c).push(...t),this.trigger("change",{added:t,removed:[],length:this.length}),this}insert(t,...r){return r.filter(n=>this.has(n)).forEach(n=>{a(this,c).splice(this.indexOf(n),1)}),a(this,c).splice(t,0,...r),this.trigger("change",{added:r,removed:[],length:this.length}),this}remove(...t){return t=t.filter(r=>this.has(r)),t.forEach(r=>{a(this,c).splice(this.indexOf(r),1)}),this.trigger("change",{added:[],removed:t,length:this.length}),this}indexOf(t){return a(this,c).indexOf(t)}item(t){return a(this,c)[t]||null}itemById(t){let r=a(this,c);for(let n=0,o=r.length;n<o;n++)if(r[n].getId()===t)return r[n];return null}has(t){return this.indexOf(t)!==-1}forEach(t){return a(this,c).forEach(t)}some(t){return a(this,c).some(t)}find(t){return a(this,c).find(t)}map(t){return a(this,c).map(t)}entries(){return a(this,c).entries()}filter(t){let r=this.constructor;return new r(a(this,c).filter(t))}reduce(t,r){return a(this,c).reduce(t,r)}get(){return a(this,c).slice(0)}clone(){return new b(this.get())}toJSON(){return this.get().map(t=>t.toJSON())}*[Symbol.iterator](){for(let t of a(this,c))yield t}},f=b;c=new WeakMap,f.className="Collection";T(f.className,f);function E(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=E(e),n=r.properties=r.properties||{};return n[t]=n[t]||{}}function U(e,t,r,n,o){let i=e.mappings?{...e.mappings}:{};return e.mappings=i,i[t]={name:r,type:n,serialize:o},i}function rt(e){return function(t){let r=E(t);r.$id=e,r.type="object",t.type=e,t.validator=C.addSchema(r,e).compile(r),h.register(e,t)}}function nt(e){return function(t,r){let n=t.constructor,o=u(n,r);o.type=e}}function ot(){return function(e,t){let r=e.constructor,n=u(r,t);n.readOnly=!0}}function it(){return function(e,t){let r=e.constructor,n=E(r);(n.required=n.required||[]).push(t)}}function st(e,t={}){return function(r,n){let o=r.constructor,i=u(o,n);i.format=e,i.formatOptions=t}}function at(e){return function(t,r){let n=t.constructor,o=u(n,r);o.default=e}}function ct(e){return function(t,r){let n=t.constructor,o=u(n,r);o.type="string",o.enum=Array.isArray(e)?e:Object.values(e)}}function pt(e){return function(t,r){let n=t.constructor,o=u(n,r);o.instanceof=f.className;let i=Array.isArray(e)?e:[e];i.length===1?o.items={$ref:i[0].type}:o.items={allOf:i.map(s=>({$ref:s.type}))}}}function lt(e,t,r="string",n=o=>o){return function(o,i){let s=o.constructor,p=u(s,i);p.instanceof=e.type,t&&U(s,i,t,r,n)}}function ft(e){return function(t,r){let n=t.constructor,o=u(n,r),i=o.form=o.form||{};Object.assign(i,e)}}function ut(e,t){return function(r,n){let o=r.constructor,i=o.serializers?{...o.serializers}:{};o.serializers=i,i[n]={serialize:e,unserialize:t}}}function I(e,t,r=!0){let n={},o=t.mappings||{},i=t.serializers||{};for(let s in e){let p=e[s],w=i[s]?.serialize;r&&o[s]?n[o[s].name]=v(o[s].serialize(p)):w?n[s]=w(p):n[s]=p}return n}function v(e,t){if(e instanceof k||t){t=t||e.constructor;let r=I(e,t);for(let n in r)r[n]=v(r[n]);return r}return Array.isArray(e)||e instanceof f?e.map(r=>v(r)):e}function R(e,t){let r=t.schema,n=t.mappings||{},o=t.serializers||{},i=r.properties||{},s={};for(let p in e){let g=Object.keys(n).find(y=>n[y].name===p)||p,d=i[g];if(d){let y=e[g];if(d.type==="array"){let O=Array.isArray(d.items)?d.items[0]:d.items;if(O.$ref){let J=h.get(O.$ref);if(J){s[g]=y.map($=>R($,J));continue}}}else if(d.$ref){let O=h.get(d.$ref);if(O){s[g]=R(y,O);continue}}let N=o[p]?.unserialize;N?s[g]=N(y):s[g]=y}}return new t(s)}var S,k=class extends x{constructor(t){super();M(this,S,[]);if(t){let n=this.getSchema().properties||{};for(let s in n){let p=n[s];"default"in p&&!(s in t)&&(t[s]=p.default)}let o=this.constructor,i=o.validator;if(i(I(t,o,!1)))for(let s in t)this[s]=t[s];else throw new Error(`Invalid init properties. | ||
${i.errors?.map(s=>JSON.stringify(s)).join(` | ||
`)}`)}}isNew(){return!this.hasChanges()}getDisplayFields(){return[]}getId(){}getModelType(){return this.constructor.type}getSchema(){return this.constructor.schema}getPropertyFormat(t){let o=(this.getSchema().properties||{})[t];if(!!o)return o.format}set(t,r=!1){let n=this.toJSON(),o=!1;for(let i in t){let s=t[i];(s!==this[i]||r)&&(o=!0,this[i]=s)}o&&(a(this,S).push(n),this.trigger("change"))}hasChanges(){return!!a(this,S).length}validate(){return this.constructor.validator(this.toJSON())}getErrors(){let t=this.constructor.validator;return t(this.toJSON())?[]:t.errors||[]}toJSON(){return v(this)}};S=new WeakMap,k.mappings={},k.serializers={};var Mt={serialize:e=>e?e.toISOString():void 0,unserialize:e=>e?new Date(e):void 0};export{f as Collection,Mt as DateSerializer,at as Default,x as Emitter,ct as Enum,ft as Form,st as Format,pt as Items,k as Model,ot as ReadOnly,lt as Ref,h as Registry,it as Required,rt as Schema,ut as Serialize,nt as Type,F as collectReferences,K as getConstructor,T as registerConstructor,C as schemaManager,v as serialize,R as unserialize}; | ||
var P=(e,t,r)=>{if(!t.has(e))throw TypeError("Cannot "+r)};var s=(e,t,r)=>(P(e,t,"read from private field"),r?r.call(e):t.get(e)),O=(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)},A=(e,t,r,n)=>(P(e,t,"write to private field"),n?n.call(e,r):t.set(e,r),r);import $ from"ajv";import j from"ajv-formats";import z from"ajv-keywords/dist/definitions/instanceof.js";var C=new $({keywords:[{keyword:"__esModule"},z(),{keyword:"formatOptions",valid:!0},{keyword:"form",metaSchema:{type:"object",properties:{type:{type:"string"}}}}],formats:{text:e=>typeof e=="string",html:e=>typeof e=="string",code:e=>typeof e=="string"},allowUnionTypes:!0,strictTypes:!1});j(C);function v(e,t){z.CONSTRUCTORS[e]=t}function K(e){return z.CONSTRUCTORS[e]}function F(e,t=[]){return Array.isArray(e)?e.forEach(r=>F(r,t)):e.oneOf?F(e.oneOf,t):e.allOf?F(e.allOf,t):e.$ref&&t.push(e.$ref),t}var h=class{static register(t,r){if(this.registry.has(t))throw new Error(`Model "${t}" has already been registered.`);v(t,r),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()}};h.registry=new Map;var l,S=class{constructor(){O(this,l,{})}on(t,r){s(this,l)[t]=s(this,l)[t]||[],s(this,l)[t].push(r)}off(t,r){let n=s(this,l)[t];if(!n)return;let o=n.indexOf(r);o!==-1&&s(this,l)[t].splice(o,1)}trigger(t,r){let n=s(this,l)[t];!n||n.forEach(o=>o(r))}};l=new WeakMap;var c,b=class extends S{constructor(t=[]){super();O(this,c,void 0);A(this,c,t)}get length(){return s(this,c).length}add(...t){return t=t.filter(r=>!this.has(r)),s(this,c).push(...t),this.trigger("change",{added:t,removed:[],length:this.length}),this}insert(t,...r){return r.filter(n=>this.has(n)).forEach(n=>{s(this,c).splice(this.indexOf(n),1)}),s(this,c).splice(t,0,...r),this.trigger("change",{added:r,removed:[],length:this.length}),this}remove(...t){return t=t.filter(r=>this.has(r)),t.forEach(r=>{s(this,c).splice(this.indexOf(r),1)}),this.trigger("change",{added:[],removed:t,length:this.length}),this}indexOf(t){return s(this,c).indexOf(t)}item(t){return s(this,c)[t]||null}itemById(t){let r=s(this,c);for(let n=0,o=r.length;n<o;n++)if(r[n].getId()===t)return r[n];return null}has(t){return this.indexOf(t)!==-1}forEach(t){return s(this,c).forEach(t)}some(t){return s(this,c).some(t)}find(t){return s(this,c).find(t)}map(t){return s(this,c).map(t)}entries(){return s(this,c).entries()}filter(t){let r=this.constructor;return new r(s(this,c).filter(t))}reduce(t,r){return s(this,c).reduce(t,r)}get(){return s(this,c).slice(0)}clone(){return new b(this.get())}toJSON(){return this.get().map(t=>t.toJSON())}*[Symbol.iterator](){for(let t of s(this,c))yield t}},u=b;c=new WeakMap,u.className="Collection";v(u.className,u);function E(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 f(e,t){let r=E(e),n=r.properties=r.properties||{};return n[t]=n[t]||{}}function U(e,t,r,n,o){let i=e.mappings?{...e.mappings}:{};return e.mappings=i,i[t]={name:r,type:n,serialize:o},i}function nt(e){return function(t){let r=E(t);r.$id=e,r.type="object",t.type=e,t.validator=C.addSchema(r,e).compile(r),h.register(e,t)}}function ot(e){return function(t,r){let n=t.constructor,o=f(n,r);o.type=e}}function it(e){return function(t,r){let n=t.constructor,o=f(n,r);o.anyOf=e.map(i=>typeof i=="string"?{type:i}:i)}}function st(){return function(e,t){let r=e.constructor,n=f(r,t);n.readOnly=!0}}function at(){return function(e,t){let r=e.constructor,n=E(r);(n.required=n.required||[]).push(t)}}function ct(e,t={}){return function(r,n){let o=r.constructor,i=f(o,n);i.format=e,i.formatOptions=t}}function pt(e){return function(t,r){let n=t.constructor,o=f(n,r);o.default=e}}function lt(e){return function(t,r){let n=t.constructor,o=f(n,r);o.type="string",o.enum=Array.isArray(e)?e:Object.values(e)}}function ft(e){return function(t,r){let n=t.constructor,o=f(n,r);o.instanceof=u.className;let i=Array.isArray(e)?e:[e];i.length===1?o.items={$ref:i[0].type}:o.items={allOf:i.map(a=>({$ref:a.type}))}}}function ut(e,t,r="string",n=o=>o){return function(o,i){let a=o.constructor,p=f(a,i);p.instanceof=e.type,t&&U(a,i,t,r,n)}}function dt(e){return function(t,r){let n=t.constructor,o=f(n,r),i=o.form=o.form||{};Object.assign(i,e)}}function mt(e,t){return function(r,n){let o=r.constructor,i=o.serializers?{...o.serializers}:{};o.serializers=i,i[n]={serialize:e,unserialize:t}}}function N(e,t,r=!0){let n={},o=t.mappings||{},i=t.serializers||{};for(let a in e){let p=e[a],g=i[a]?.serialize;r&&o[a]?n[o[a].name]=k(o[a].serialize(p)):g?n[a]=g(p):n[a]=p}return n}function k(e,t){if(e instanceof w||t){t=t||e.constructor;let r=N(e,t);for(let n in r)r[n]=k(r[n]);return r}return Array.isArray(e)||e instanceof u?e.map(r=>k(r)):e}function D(e,t,r){let n=t.schema,o=t.mappings||{},i=t.serializers||{},a=n.properties||{},p={};for(let g in e){let y=Object.keys(o).find(M=>o[M].name===g)||g,d=a[y];if(d){let M=e[y];if(d.type==="array"){let x=Array.isArray(d.items)?d.items[0]:d.items;if(x.$ref){let J=h.get(x.$ref);if(J){p[y]=M.map(R=>D(R,J,r));continue}}}else if(d.$ref){let x=h.get(d.$ref);if(x){p[y]=D(M,x,r);continue}}let I=i[g]?.unserialize;I?p[y]=I(M):p[y]=M}}return new t(p,r)}var T,w=class extends S{constructor(t,r){super();O(this,T,[]);if(t){let o=this.getSchema().properties||{};for(let i in o){let a=o[i];"default"in a&&!(i in t)&&(t[i]=a.default)}if(!r?.skipValidation){let i=this.constructor,a=i.validator;if(!a(N(t,i,!1)))throw new Error(`Invalid init properties. | ||
${a.errors?.map(p=>JSON.stringify(p)).join(` | ||
`)}`)}for(let i in t)this[i]=t[i]}}isNew(){return!this.hasChanges()}getId(){}getDisplayName(){return this.getId()}getDisplayFields(){return[]}getModelType(){return this.constructor.type}getSchema(){return this.constructor.schema}getPropertyFormat(t){let o=(this.getSchema().properties||{})[t];if(!!o)return o.format}set(t,r=!1){let n=this.toJSON(),o=!1;for(let i in t){let a=t[i];(a!==this[i]||r)&&(o=!0,this[i]=a)}o&&(s(this,T).push(n),this.trigger("change"))}hasChanges(){return!!s(this,T).length}validate(){let t=this.constructor;return t.validator(N(this,t))}getErrors(){let r=this.constructor.validator;return this.validate()?[]:r.errors||[]}toJSON(){return k(this)}};T=new WeakMap,w.mappings={},w.serializers={};var xt={serialize:e=>e?e.toISOString():void 0,unserialize:e=>e?new Date(e):void 0};export{it as AnyOf,u as Collection,xt as DateSerializer,pt as Default,S as Emitter,lt as Enum,dt as Form,ct as Format,ft as Items,w as Model,st as ReadOnly,ut as Ref,h as Registry,at as Required,nt as Schema,mt as Serialize,ot as Type,F as collectReferences,K as getConstructor,v as registerConstructor,C as schemaManager,k as serialize,D as unserialize}; | ||
//# sourceMappingURL=backbone.js.map |
{ | ||
"name": "@chialab/backbone", | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"description": "Models, Collections and Form helpers for JS apps.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
import type { JSONSchema } from '../Lib/Schema'; | ||
import type { Model, Serializer } from '../Models/Model'; | ||
declare type PlainTypes = 'number' | 'string' | 'boolean' | 'object' | 'array' | 'null'; | ||
/** | ||
@@ -12,4 +13,9 @@ * Decorate a class as JSON schema model. | ||
*/ | ||
export declare function Type(type: 'number' | 'string' | 'boolean' | 'object' | 'array' | 'null' | JSONSchema): (target: Model, propertyName: string) => void; | ||
export declare function Type(type: PlainTypes | PlainTypes[]): (target: Model, propertyName: string) => void; | ||
/** | ||
* Define multiple types for . | ||
* @param type The type of the field. | ||
*/ | ||
export declare function AnyOf(types: (PlainTypes | JSONSchema)[]): (target: Model, propertyName: string) => void; | ||
/** | ||
* Flag a class field as readonly in the schema. | ||
@@ -16,0 +22,0 @@ */ |
import type { JSONSchema, ValidateFunction } from '../Lib/Schema'; | ||
import { Emitter } from './Emitter'; | ||
export interface ModelInitOptions { | ||
skipValidation?: boolean; | ||
} | ||
export declare function serialize(object: any, model?: typeof Model): any; | ||
export declare function unserialize(inputData: any, BaseModel: typeof Model): any; | ||
export declare function unserialize(inputData: any, model: typeof Model, options?: ModelInitOptions): any; | ||
export interface Props { | ||
@@ -48,6 +51,7 @@ [key: string]: unknown; | ||
[key: string]: unknown; | ||
}); | ||
}, options?: ModelInitOptions); | ||
isNew(): boolean; | ||
getId(): number | string | void; | ||
getDisplayName(): string; | ||
getDisplayFields(): string[]; | ||
getId(): number | string | void; | ||
getModelType(): string; | ||
@@ -54,0 +58,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
91623
311