UNPKG

@ayka/strukt

Version:

A lightweight TypeScript library that simplifies the creation of structured data objects and classes. It provides a type-safe and flexible way to define classes with custom properties and initialization logic.

2 lines 4.22 kB
"use strict";var x=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var s=(r,t)=>x(r,"name",{value:t,configurable:!0});var d=(r,t)=>{for(var e in t)x(r,e,{get:t[e],enumerable:!0})},F=(r,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of z(t))!D.call(r,a)&&a!==e&&x(r,a,{get:()=>t[a],enumerable:!(o=_(t,a))||o.enumerable});return r};var V=r=>F(x({},"__esModule",{value:!0}),r);var G={};d(G,{Basic:()=>u,Error:()=>j,Extended:()=>p,Lib:()=>g,Strukt:()=>C,StruktBase:()=>T,error:()=>B,init:()=>b,initBasic:()=>P,isBasicStrukt:()=>E,isErrorStrukt:()=>$,isStrukt:()=>S,klass:()=>R,lazy:()=>M,makeConstructor:()=>I,promiseObject:()=>A,redefineAsAccessors:()=>f,selectKeys:()=>k,staticError:()=>O});module.exports=V(G);var g={};d(g,{klass:()=>R,lazy:()=>M,makeConstructor:()=>I,promiseObject:()=>A,redefineAsAccessors:()=>f,selectKeys:()=>k});var R=s(()=>class{},"klass"),k=s((r,t)=>{let e={};for(let o of t)e[o]=r[o];return e},"selectKeys"),I=s(r=>(...t)=>Reflect.construct(r,t),"makeConstructor"),w=s((r,t,e)=>{Object.defineProperty(r,t,{get(){return e}})},"defineGetter"),f=s((r,t)=>{for(let e of t){let o=r[e];w(r,e,o),Object.defineProperty(r,e,{set(a){w(r,e,a)}})}return r},"redefineAsAccessors"),M=s(r=>{let{useValue:t=!0,configurable:e=!0,writable:o=!0,enumerable:a=!0}=r??{};return(n,c,i)=>{if(i.get==null)return;let K=i.get;i.get=s(function(){let l=K.call(this);return Object.defineProperty(this,c,t?{value:l,writable:o,enumerable:a,configurable:e}:{get(){return l},set(v){l=v},configurable:e,enumerable:a}),l},"lazy")}},"lazy"),A=s(async r=>{let t=Object.entries(r).map(([o,a])=>a.then(n=>[o,n])),e=await Promise.all(t);return Object.fromEntries(e)},"promiseObject");var C={};d(C,{init:()=>b,initBasic:()=>P,isBasicStrukt:()=>E,isStrukt:()=>S});var T={};d(T,{BasicStrukt:()=>u,ExtendedStrukt:()=>p,construct:()=>h});var h=s(r=>{let t={...r.data};return Reflect.deleteProperty(t,"__proto__"),Object.assign(r.target,t),f(r.target,r.hidden),r.target},"construct"),u=class{static{s(this,"BasicStrukt")}constructor(t){h({target:this,data:t.data,hidden:t.hidden})}},p=class extends u{static{s(this,"ExtendedStrukt")}#t;#r;constructor(t){super(t),this.#t=Object.keys(t.data),this.#r=[...t.hidden]}get $$argsType(){}get $$args1Type(){}get $$dataType(){}$dataKeys(){return[...this.#t]}$data(){let t={};return this.#t.forEach(e=>{t[e]=Reflect.get(this,e)}),t}$selectKeys(t){return k(this,t)}$clone(){let t=h({target:{},data:this,hidden:this.#r});return Object.setPrototypeOf(t,Object.getPrototypeOf(this)),t}$update(t){let e=h({target:{},data:{...this,...t},hidden:this.#r});return Object.setPrototypeOf(e,Object.getPrototypeOf(this)),e}$patch(t){let e=t(this.$clone());return this.$update(e)}$create(...t){return new this.constructor(...t)}};var b=s(r=>{let t=r.constructor,e=r.hidden??[];class o extends p{static{s(this,"Strukt")}constructor(...n){let c=t(...n);super({data:c,hidden:e})}}return o},"init"),P=s(r=>{let t=r.constructor,e=r.hidden??[];class o extends u{static{s(this,"Strukt")}constructor(...n){let c=t(...n);super({data:c,hidden:e})}}return o},"initBasic"),S=s(r=>r instanceof p,"isStrukt"),E=s(r=>r instanceof u,"isBasicStrukt");var j={};d(j,{ErrorStruktBase:()=>y,init:()=>B,isErrorStrukt:()=>$,staticError:()=>O});var y=class extends Error{static{s(this,"ErrorStruktBase")}message;meta;constructor(t,e){let o={...e},a=o.message??t;super(a,o.cause==null?void 0:{cause:o.cause}),this.message=a,this.name=this.constructor.name,delete o.cause,delete o.message,this.meta=o}},O=s(r=>{let t=r?.message??"";class e extends y{static{s(this,"StaticStruktError")}constructor(a,n){let c=typeof a=="string"?n:a,i=typeof a=="string"?a:t;super(i,c)}}return e},"staticError"),B=s(r=>{let t=r.constructor;class e extends y{static{s(this,"StruktError")}data;constructor(a,n){let{data:c,message:i}=t(a);super(i??"",n),this.data=c}}return e},"init"),$=s(r=>r instanceof y,"isErrorStrukt");0&&(module.exports={Basic,Error,Extended,Lib,Strukt,StruktBase,error,init,initBasic,isBasicStrukt,isErrorStrukt,isStrukt,klass,lazy,makeConstructor,promiseObject,redefineAsAccessors,selectKeys,staticError}); //# sourceMappingURL=main.cjs.map