-
Notifications
You must be signed in to change notification settings - Fork 1
/
modelz.js
2 lines (2 loc) · 3.25 KB
/
modelz.js
1
2
!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).modelz=t()}(this,(function(){"use strict";function e(){}function t(e){return e}function r(e){return{}.toString.call(e)}function n(e){return"[object Array]"==r(e)}function o(e){return"function"==typeof e}function i(e){return"string"==typeof e}const u={construct:t,getCacheKey:e,enumerable:!0,required:!1},a={castString:!0,parseNumbers:!0,onChangeListener:()=>e,extraProperties:!1,embedPlainData:!0,preInit:t,postInit:t,types:{}};function c({cacheKey:t}){return n(t)?e=>t.map((t=>e[t]instanceof Date?e[t].getTime():e[t])).join("|<3|"):o(t)?t:e}return function(s){const{castString:f,debug:l,embedPlainData:d,extraProperties:p,onChangeListener:g,parseNumbers:y,postInit:b,preInit:h,types:m}={...a,...s};function P(e,r){const n={string(e){if(i(e))return e;if(f)return""+e;throw Error(`Expect a string for "${r}", got "${e}"`)},number(e){if("number"==typeof e)return e;if(i(e)&&y)return parseFloat(e);throw Error(`Expect a number for "${r}", got "${e}"`)},boolean:e=>!!e,array:e=>[...e],object:e=>({...e}),date:e=>new Date(e),identity:t,...m};if(o(e))return e;if(null==n[e])throw Error(`Try to use unknown type "${e}" as type for "${r}"`);return n[e]}function j(e,t){if(null!==(u=e)&&"[object Object]"===r(u)){if(o(e.construct))return e;if(i(e.type))return{construct:P(e.type,t),...e};if(o(e.get))return{getCacheKey:c(e),get:e.get,set:e.set,enumerable:e.enumerable||!1}}var u;if(n(e)&&2===e.length){const[r,n]=e;return{construct:P(r,t),required:n}}if(n(e)&&3===e.length){const[r,n,o]=e;return{construct:P(r,t),required:n,default:o}}if(o(e))return{construct:e};if(i(e))try{return{construct:P(e,t)}}catch(e){}try{return{construct:P(typeof e,t),required:!0,default:e}}catch(t){throw new Error(`No proper config handler found for config:\n${JSON.stringify(e)}`)}}return function(t,r){const n=(r={embedPlainData:d,extraProperties:p,preInit:h,postInit:b,onChangeListener:g,...r}).name?r.name:"instance",i={modelName:n};return function(a={}){if(l&&l.extend("create")(`constructing ${n} with data %O`,a),a._schema===i)return a;const c={};let s=e,f={};if(r.extraProperties)f={...a};else if(l){const e=Object.keys(t),r=Object.keys(a).filter((t=>!e.includes(t)));r.length&&l.extend("warn")(`The properties %o are not defined on target ${n} and will therefore be dropped.`,r)}r.embedPlainData&&Object.defineProperty(f,"_data",{get:()=>c,enumerable:!1}),Object.defineProperty(f,"_isInitialized",{get:()=>!0,enumerable:!1}),Object.defineProperty(f,"_schema",{get:()=>i,enumerable:!1}),f=r.preInit(f),s=r.onChangeListener(f);for(const e in t){const r={...u,...j(t[e],e)};if(Object.defineProperty(f,e,{enumerable:r.enumerable,get:function(){if(r.get){const t=r.getCacheKey(f);return c.hasOwnProperty(e)&&null!=t&&t===c[e].key||(c[e]={key:t,value:r.get(f)}),c[e].value}return f._data[e]},set:function(t){const n=f[e];o(r.set)?r.set(f,t):r.required||null!=t?c[e]=r.construct(t,f,r):c[e]=t=null,s(e,t,n)}}),null!=a[e])f[e]=a[e];else if(r.hasOwnProperty("default"))o(r.default)?f[e]=r.default(a):f[e]=r.default;else{if(r.required)throw Error("No value set for "+e);r.get||(f[e]=null)}}return f=r.postInit(f),p||Object.seal(f),f}}}}));
//# sourceMappingURL=modelz.js.map