UNPKG

corello

Version:

Main Kernel

2 lines (1 loc) 4.68 kB
(function(s,l){typeof exports=="object"&&typeof module<"u"?l(exports):typeof define=="function"&&define.amd?define(["exports"],l):(s=typeof globalThis<"u"?globalThis:s||self,l(s.corello={}))})(this,function(s){"use strict";var B=Object.defineProperty;var U=(s,l,y)=>l in s?B(s,l,{enumerable:!0,configurable:!0,writable:!0,value:y}):s[l]=y;var p=(s,l,y)=>U(s,typeof l!="symbol"?l+"":l,y);function l(n,t){let e=n;for(;e;){if(e===t)return!0;e=Object.getPrototypeOf(e)}return!1}const y=n=>typeof n=="function",g=n=>Object.getOwnPropertyNames(n).map(t=>({prop:t,descriptor:Object.getOwnPropertyDescriptor(n,t)})),w=({instance:n,methodPredicate:t,withObjProps:e})=>{const r={},o=Object.getOwnPropertyNames(Object.getPrototypeOf({})),f=u=>{g(u).forEach(i=>{!e&&o.includes(i.prop)||r[i.prop]!==void 0||(t&&y(u[i.prop])?t(i.prop)&&(r[i.prop]=i):r[i.prop]=i)}),u!==Object.prototype&&f(Object.getPrototypeOf(u))};return f(n),Object.freeze(r)},D=n=>n instanceof Date,d=n=>typeof n=="function"&&/^class\s/.test(Function.prototype.toString.call(n)),E=n=>n!==Object(n),x=Symbol("?"),v=()=>{if("crypto"in window&&y(crypto.randomUUID))return crypto.randomUUID();{let n=new Date().getTime();return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){const e=(n+Math.random()*16)%16|0;return n=Math.floor(n/16),(t==="x"?e:e&3|8).toString(16)})}},O=Symbol(),m=Symbol();function S(n){if(!l(n,P))throw new Error(`${n.name} needs to have DtoBase in its inhertiance chain.`);const t=n.name||"";return class extends n{constructor(...e){super(e==null?void 0:e[0]);const r=(e==null?void 0:e[0])||{};!this[x]&&Object.defineProperty(this,x,{enumerable:!0,configurable:!1,get:function(){return Object.freeze({name:n.name,instance:this})}}),!this[m]&&Object.defineProperty(this,m,{enumerable:!1,configurable:!1,value:function(o){const f=w({instance:this}),u=(i,h,c,b,...j)=>{if(Array.isArray(h)){if(!Array.isArray(c))throw new Error(`Expected Array for [${i}] but the received json is not an Array!`);return c.map((a,T)=>u(i,a,a,b,T))}else if(D(h))if(b){const a=b();return d(a)||a===Date?new a(c,...j):a(c,...j)}else return c?new Date(c):new Date;else if(b){const a=b();return d(a)||a===Date?new a(c,...j):a(c,...j)}else return E(h)?c:(Object.getPrototypeOf(h)===Object.prototype&&console.warn(`Missing a possible factory to serialize ${t?t+".":""}${i}, will treat [${JSON.stringify(h)}] as plain object`),h.constructor?new h.constructor(c):c)};Object.entries(f).filter(([i,h])=>{var c;return!((c=h.descriptor)!=null&&c.get)}).forEach(([i,h])=>{var c;o[i]!==void 0&&(this[i]=u(i,this[i],o[i],(c=this[O])==null?void 0:c[i]))})}}),this[m](r)}toJSON(){const e=Object.assign({},this),r=o=>{Object.entries(Object.getOwnPropertyDescriptors(o)).filter(([f,u])=>y(u.get)).map(([f,u])=>{if(u&&f[0]!=="_")try{const i=this[f];e[f]=i}catch(i){console.error(`Error calling getter ${f}`,i)}}),o!==Object.prototype&&r(Object.getPrototypeOf(o))};return r(this),e}toString(){return`[DTO:${t}]`}}}function $(n){return function(t,e){!t[O]&&(t[O]={}),t[O][e]=n}}function _(n){return function(t,e){if(d(n()))throw new Error(`Factory must return an instance, update your factory for property ${e} to return NEW instnace`);!t[O]&&(t[O]={}),t[O][e]=()=>n}}class P{constructor(t){}from(t){return this[m](t)}}class F{constructor(t,e,r="Event"){p(this,"type");p(this,"info");this.type=`${r}@${t}`,this.info=e}}class A{constructor(){p(this,"id",v());p(this,"listeners",{});p(this,"target",null);p(this,"_isOn",!0)}once(t,e){this.validateEvent(t),this.on(t,e,{once:!0})}get isOn(){return this._isOn}turnOn(){this._isOn=!0}turnOff(){this._isOn=!1}on(t,e,r={}){this.validateEvent(t);const{type:o}=t;o in this.listeners||(this.listeners[o]=[]),this.listeners[o].push({callback:e,options:r})}off(t,e){this.validateEvent(t);const{type:r}=t;if(!(r in this.listeners))return;const o=this.listeners[r];if(e){for(let f=0,u=o.length;f<u;f++)if(o[f].callback===e){o.splice(f,1);return}}else o.length=0}dispatch(t){if(this.validateEvent(t),!!this._isOn){if(!(t.type in this.listeners))return!0;this.listeners[t.type].forEach((e,r)=>{e.callback.call(this,t),e.options.once&&this.listeners[t.type].splice(r,1,void 0)}),this.listeners[t.type]=this.listeners[t.type].filter(e=>!!e)}}dispose(){Object.keys(this.listeners).forEach(t=>{this.listeners[t].length=0,delete this.listeners[t]})}}const N=(n,t)=>{if(!(n instanceof t)&&!l(t,n.constructor))throw new Error("To dispatch events use the class Event")};s.Class=$,s.DispatcherBase=A,s.Dto=S,s.DtoBase=P,s.Event=F,s.Factory=_,s.extract=w,s.hasParentClass=l,s.isClass=d,s.isDate=D,s.isFn=y,s.isPrimitive=E,s.uid=v,s.validate=N,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});