@chialab/backbone
Advanced tools
Comparing version 2.5.2 to 2.5.3
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)),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,n)=>(P(e,t,"write to private field"),n?n.call(e,r):t.set(e,r),r);import F from"ajv";import b from"ajv-formats";var x=new F({keywords:[{keyword:"__esModule"},{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});b(x);function z(e,t=[]){return Array.isArray(e)?e.forEach(r=>z(r,t)):e.oneOf?z(e.oneOf,t):e.allOf?z(e.allOf,t):e.$ref&&t.push(e.$ref),t}var S=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 has(t){return this.registry.has(t)}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()}};S.registry=new Map;function A(e){return function(t){let r=t.setupSchema();r.$id=e,r.type="object",t.validator=x.addSchema(r,e).compile(r),S.register(e,t)}}function V(e){return function(t,r){t.constructor.setupProperty(r,{type:e})}}function j(e){return function(t,r){t.constructor.setupProperty(r,{anyOf:e.map(o=>typeof o=="string"?{type:o}:o)})}}function q(){return function(e,t){e.constructor.setupProperty(t,{readOnly:!0})}}function R(){return function(e,t){let n=e.constructor.setupSchema();(n.required=n.required||[]).push(t)}}function H(e,t={}){return function(r,n){r.constructor.setupProperty(n,{format:e,formatOptions:t})}}function L(e){return function(t,r){t.constructor.setupProperty(r,{default:e})}}function K(e){return function(t,r){t.constructor.setupProperty(r,{type:"string",enum:Array.isArray(e)?e:Object.values(e)})}}function B(e){return function(t,r){let n=t.constructor;e=e.filter(o=>o.schema),n.setupProperty(r,{type:"array",items:e.length>1?{allOf:e.map(o=>({$ref:o.schema.$id}))}:e.length===1?{$ref:e[0].schema.$id}:{}})}}function _(e){return function(t,r){let n=t.constructor,o=Array.isArray(e)?{oneOf:e.map(i=>typeof i=="string"?{type:i}:{$ref:i.schema.$id})}:{$ref:e.schema.$id};n.setupProperty(r,o)}}function G(e){return function(t,r){let n=t.constructor,o=(Array.isArray(e)?e:[e]).filter(i=>i.schema);n.setupProperty(r,{type:"array",uniqueItems:!0,items:o.length>1?{allOf:o.map(i=>({$ref:i.schema.$id}))}:o.length===1?{$ref:o[0].schema.$id}:{}})}}function Q(e){return function(t,r){t.constructor.setupProperty(r,{form:e})}}function W(e,t){return function(r,n){r.constructor.setupSerializer(n,{serialize:e,unserialize:t})}}var p,m=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 n=s(this,p)[t];if(!n)return;let o=n.indexOf(r);o!==-1&&s(this,p)[t].splice(o,1)}trigger(t,r){let n=s(this,p)[t];!n||n.forEach(o=>o.call(this,r))}};p=new WeakMap;var c,T=class extends m{constructor(t=[]){super();d(this,c,void 0);k(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 T(this.get())}toJSON(){return this.get().map(t=>t.toJSON())}*[Symbol.iterator](){for(let t of s(this,c))yield t}},O=T;c=new WeakMap,O.className="Collection";function N(e,t,r,n=[],o=!1){let i={},u=t.schema,a=t.serializers||{};for(let l in e){if(u&&r&&!r(l,u.properties?.[l],u,e,t,n))continue;let h=e[l],y=a[l]?.serialize;y&&!o?i[l]=y(h,e,i,r,[...n,{model:t,object:e,key:l}]):i[l]=h}return i}function g(e,t,r,n=[],o=!1){if(e instanceof w||t){t=t||e.constructor;let i=N(e,t,r,n,o);for(let u in i)i[u]=g(i[u],void 0,r,[...n,{model:t,object:e,key:u}],o);return i}return Array.isArray(e)||e instanceof O?e.map(i=>g(i,void 0,r,n,o)):e}function nt(e,t,r={},n=[]){let o=t.schema,i=t.serializers||{},u=o.properties||{},a={};for(let l in e)if(u[l]){let y=i[l]?.unserialize,v=e[l];y?a[l]=y(v,e,r,n):a[l]=v}return new t(a,r)}var M,w=class extends m{constructor(t,r={}){super();d(this,M,[]);let n=this.constructor,o=n.setupSerializers();if(t){let u=this.getSchema().properties||{};for(let a in u){let l=u[a];if(a in t){let h=o[a]?.unserialize;t[a]=h?h(t[a],t,r):t[a]}else"default"in l&&(typeof l.default=="function"?t[a]=l.default(this,t):t[a]=l.default)}if(!r.skipValidation&&!r.editingMode){let a=n.validator;if(a&&!a(g(t,n)))throw new Error(`Invalid init properties. | ||
${a.errors?.map(l=>JSON.stringify(l)).join(` | ||
`)}`)}for(let a in t)this[a]=t[a]}}static setupSchema(){return Object.prototype.hasOwnProperty.call(this,"schema")?this.schema:this.schema=this.schema?JSON.parse(JSON.stringify(this.schema)):{}}static setupSerializers(){return Object.prototype.hasOwnProperty.call(this,"serializers")?this.serializers:this.serializers={...this.serializers||{}}}static setupProperty(t,r){let n=this.setupSchema(),o=n.properties=n.properties||{},i=o[t]=o[t]||{};return Object.assign(i,r),i}static setupSerializer(t,r){let n=this.setupSerializers();n[t]=r}isNew(){return!this.hasChanges()}getId(){}getDisplayName(){return this.getId()}getDisplayFields(){return[]}getSchema(){return this.constructor.schema||{}}getModelType(){return this.getSchema().$id}getPropertyFormat(t){let o=(this.getSchema().properties||{})[t];if(!!o)return o.format}get(t){return this[t]}set(t,r=!1){let n=this.toJSON(),o=!1;for(let i in t){let u=t[i];(u!==this[i]||r)&&(o=!0,this[i]=u)}o&&(s(this,M).push(n),this.trigger("change"))}hasChanges(){return!!s(this,M).length}validate(){let t=this.constructor;return t.validator(g(this,t,void 0,void 0,!0))}getErrors(){let r=this.constructor.validator;return this.validate()?[]:r.errors||[]}toJSON(t){let r=this.constructor;return g(this,r,t)}};M=new WeakMap;var it={serialize:e=>e&&!isNaN(e.getTime())?e.toISOString():void 0,unserialize:e=>e?new Date(e):void 0};export{j as AnyOf,O as Collection,it as DateSerializer,L as Default,m as Emitter,K as Enum,Q as Form,H as Format,G as HasMany,_ as HasOne,B as Items,w as Model,q as ReadOnly,S as Registry,R as Required,A as Schema,W as Serialize,V as Type,z as collectReferences,x as schemaManager,g as serialize,nt as unserialize}; | ||
`)}`)}for(let a in t)this[a]=t[a]}}static setupSchema(){return Object.prototype.hasOwnProperty.call(this,"schema")?this.schema:this.schema=this.schema?JSON.parse(JSON.stringify(this.schema)):{}}static setupSerializers(){return Object.prototype.hasOwnProperty.call(this,"serializers")?this.serializers:this.serializers={...this.serializers||{}}}static setupProperty(t,r){let n=this.setupSchema(),o=n.properties=n.properties||{},i=o[t]=o[t]||{};return Object.assign(i,r),i}static setupSerializer(t,r){let n=this.setupSerializers();n[t]=r}isNew(){return!this.hasChanges()}getId(){}getDisplayName(){return this.getId()}getDisplayFields(){return[]}getSchema(){return this.constructor.schema||{}}getModelType(){return this.getSchema().$id}getPropertyFormat(t){let o=(this.getSchema().properties||{})[t];if(!!o)return o.format}get(t){return this[t]}set(t,r=!1){let n=this.toJSON(),o=!1;for(let i in t){let u=t[i];(u!==this[i]||r)&&(o=!0,this[i]=u)}o&&(s(this,M).push(n),this.trigger("change"))}hasChanges(){return!!s(this,M).length}validate(){let t=this.constructor;return t.validator(g(this,t,void 0,void 0,!0))}getErrors(){let r=this.constructor.validator;return this.validate()?[]:r.errors||[]}toJSON(t,r=[]){let n=this.constructor;return g(this,n,t,r)}};M=new WeakMap;var it={serialize:e=>e&&!isNaN(e.getTime())?e.toISOString():void 0,unserialize:e=>e?new Date(e):void 0};export{j as AnyOf,O as Collection,it as DateSerializer,L as Default,m as Emitter,K as Enum,Q as Form,H as Format,G as HasMany,_ as HasOne,B as Items,w as Model,q as ReadOnly,S as Registry,R as Required,A as Schema,W as Serialize,V as Type,z as collectReferences,x as schemaManager,g as serialize,nt as unserialize}; | ||
//# sourceMappingURL=backbone.js.map |
{ | ||
"name": "@chialab/backbone", | ||
"version": "2.5.2", | ||
"version": "2.5.3", | ||
"description": "Models, Collections and Form helpers for JS apps.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -86,3 +86,3 @@ import type { JSONSchema, ValidateFunction } from '../Lib/Schema'; | ||
getErrors(): import("ajv").ErrorObject<string, Record<string, any>, unknown>[]; | ||
toJSON(filter?: SerializeFilter): T; | ||
toJSON(filter?: SerializeFilter, path?: SerializePath[]): T; | ||
} |
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
95530