@homedev/fields 1.2.27 → 1.2.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { NavigateResult } from '@homedev/navigate';
1
+ import { NavigateResult } from '@homedev/framework';
2
2
 
3
3
  export declare interface FieldContext {
4
4
  path: string;
package/dist/index.js CHANGED
@@ -1,9 +1,10 @@
1
- var j0=Object.create;var{getPrototypeOf:k0,defineProperty:p,getOwnPropertyNames:E0}=Object;var Q0=Object.prototype.hasOwnProperty;var O0=(P,f,y)=>{y=P!=null?j0(k0(P)):{};let _=f||!P||!P.__esModule?p(y,"default",{value:P,enumerable:!0}):y;for(let M of E0(P))if(!Q0.call(_,M))p(_,M,{get:()=>P[M],enumerable:!0});return _};var Z0=(P,f)=>()=>(f||P((f={exports:{}}).exports,f),f.exports);var c=Z0((V0,v)=>{var{defineProperty:w,getOwnPropertyNames:J0,getOwnPropertyDescriptor:U0}=Object,X0=Object.prototype.hasOwnProperty,e=new WeakMap,G0=(P)=>{var f=e.get(P),y;if(f)return f;if(f=w({},"__esModule",{value:!0}),P&&typeof P==="object"||typeof P==="function")J0(P).map((_)=>!X0.call(f,_)&&w(f,_,{get:()=>P[_],enumerable:!(y=U0(P,_))||y.enumerable}));return e.set(P,f),f},K0=(P,f)=>{for(var y in f)w(P,y,{get:f[y],enumerable:!0,configurable:!0,set:(_)=>f[y]=()=>_})},i={};K0(i,{splitWithQuotes:()=>L0,splitNested:()=>C0});v.exports=G0(i);var C0=(P,f,y="(",_=")")=>{let M=0,W="",D=[];for(let F of P){if(W+=F,F===y)M++;if(F===_)M--;if(M===0&&F===f)D.push(W.slice(0,-1)),W=""}if(W)D.push(W);return D},L0=(P,f=",")=>{let y=[],_=P.length,M=f.length,W=0,D=(F)=>{if(F+M>_)return!1;for(let $=0;$<M;$++)if(P[F+$]!==f[$])return!1;return!0};while(W<_){while(W<_&&P[W]===" ")W++;if(W>=_)break;let F="",$=P[W]==='"'||P[W]==="'";if($){let k=P[W++];while(W<_&&P[W]!==k)F+=P[W++];if(W<_)W++}else{while(W<_&&!D(W)){let k=P[W];if(k==='"'||k==="'"){F+=k,W++;while(W<_&&P[W]!==k)F+=P[W++];if(W<_)F+=P[W++]}else F+=P[W++]}F=F.trim()}if(F)y.push({value:F,quoted:$});if(D(W))W+=M}return y}});var t=O0(c(),1);var S;((P)=>{P[P.Explore=0]="Explore",P[P.SkipSiblings=1]="SkipSiblings",P[P.NoExplore=2]="NoExplore",P[P.ReplaceParent=3]="ReplaceParent",P[P.DeleteParent=4]="DeleteParent",P[P.MergeParent=5]="MergeParent",P[P.ReplaceItem=6]="ReplaceItem",P[P.DeleteItem=7]="DeleteItem"})(S||={});class J{action;by;skipSiblings;reexplore;constructor(P,f,y,_){this.action=P,this.by=f,this.skipSiblings=y,this.reexplore=_}static _explore=new J(0);static _noExplore=new J(2);static _skipSiblings=new J(1);static _deleteParent=new J(4);static ReplaceParent(P,f){return new J(3,P,void 0,f)}static SkipSiblings(){return J._skipSiblings}static Explore(){return J._explore}static NoExplore(){return J._noExplore}static DeleteParent(){return J._deleteParent}static MergeParent(P){return new J(5,P)}static ReplaceItem(P,f){return new J(6,P,f)}static DeleteItem(P){return new J(7,void 0,P)}}var u=async(P,f)=>{let y=new WeakSet,_=[],M=[],W=async(F,$,k)=>{let X=F===null,U=X?J.Explore():await f({key:F,value:$,path:_,parent:k,parents:M});if($&&typeof $==="object"&&U.action===0){if(y.has($))return U;if(y.add($),!X)_.push(F),M.push(k);let j=$;while(await D(j,F,k))j=k[F];if(!X)M.pop(),_.pop()}return U},D=async(F,$,k)=>{let X=Object.keys(F),U=X.length;for(let j=0;j<U;j++){let O=X[j],K=F[O],Q=await W(O,K,F),G=Q.action;if(G===0||G===2)continue;if(G===6){if(F[O]=Q.by,Q.skipSiblings)return;continue}if(G===7){if(delete F[O],Q.skipSiblings)return;continue}if(G===1)return;if(G===3){if($===null)throw Error("Cannot replace root object");if(k[$]=Q.by,Q.reexplore)return!0;return}if(G===4){if($===null)throw Error("Cannot delete root object");delete k[$];return}if(G===5)delete F[O],Object.assign(F,Q.by)}};await W(null,P,null)};var Y0=Object.create,{getPrototypeOf:S0,defineProperty:n,getOwnPropertyNames:I0}=Object,H0=Object.prototype.hasOwnProperty,b0=(P,f,y)=>{y=P!=null?Y0(S0(P)):{};let _=f||!P||!P.__esModule?n(y,"default",{value:P,enumerable:!0}):y;for(let M of I0(P))if(!H0.call(_,M))n(_,M,{get:()=>P[M],enumerable:!0});return _},T0=(P,f)=>()=>(f||P((f={exports:{}}).exports,f),f.exports),N0=T0((P,f)=>{var{defineProperty:y,getOwnPropertyNames:_,getOwnPropertyDescriptor:M}=Object,W=Object.prototype.hasOwnProperty,D=new WeakMap,F=(j)=>{var O=D.get(j),K;if(O)return O;if(O=y({},"__esModule",{value:!0}),j&&typeof j==="object"||typeof j==="function")_(j).map((Q)=>!W.call(O,Q)&&y(O,Q,{get:()=>j[Q],enumerable:!(K=M(j,Q))||K.enumerable}));return D.set(j,O),O},$=(j,O)=>{for(var K in O)y(j,K,{get:O[K],enumerable:!0,configurable:!0,set:(Q)=>O[K]=()=>Q})},k={};$(k,{splitWithQuotes:()=>U,splitNested:()=>X}),f.exports=F(k);var X=(j,O,K="(",Q=")")=>{let G=0,E="",Y=[];for(let Z of j){if(E+=Z,Z===K)G++;if(Z===Q)G--;if(G===0&&Z===O)Y.push(E.slice(0,-1)),E=""}if(E)Y.push(E);return Y},U=(j,O=",")=>{let K=[],Q=j.length,G=O.length,E=0,Y=(Z)=>{if(Z+G>Q)return!1;for(let L=0;L<G;L++)if(j[Z+L]!==O[L])return!1;return!0};while(E<Q){while(E<Q&&j[E]===" ")E++;if(E>=Q)break;let Z="",L=j[E]==='"'||j[E]==="'";if(L){let C=j[E++];while(E<Q&&j[E]!==C)Z+=j[E++];if(E<Q)E++}else{while(E<Q&&!Y(E)){let C=j[E];if(C==='"'||C==="'"){Z+=C,E++;while(E<Q&&j[E]!==C)Z+=j[E++];if(E<Q)Z+=j[E++]}else Z+=j[E++]}Z=Z.trim()}if(Z)K.push({value:Z,quoted:L});if(Y(E))E+=G}return K}}),B0=b0(N0(),1),r0=/^\[([^=\]]*)([=]*)([^\]]*)\]$/,z0=/^([^[\]]*)\[([^\]]*)]$/,m=(P,f,y,_)=>{if(P.startsWith("{")&&P.endsWith("}")){let M=P.substring(1,P.length-1);return H(f,M,y)?.toString()}return _?P:void 0},w0=(P,f,y,_)=>{let M=m(f,y,_);if(M)return H(P,M,_);let W=r0.exec(f);if(W){let[,F,$,k]=W,X=m(F.trim(),y,_,!0),U=m(k.trim(),y,_,!0);if(Array.isArray(P)&&($==="="||$==="==")&&X)return P.find((j)=>j?.[X]==U)}let D=z0.exec(f);if(D){let[,F,$]=D;return P?.[F]?.[$]}return P?.[f]},H=(P,f,y)=>{let _=void 0,M=void 0,W=f??"",D=(U)=>{if(!U)return[U,!1];return U.endsWith("?")?[U.substring(0,U.length-1),!0]:[U,!1]},F=(U,j)=>{let[O,K]=D(j.pop());if(!O){if(y?.set!==void 0&&M!==void 0&&_!==void 0)_[M]=y.set;return U}let Q=w0(U,O,P,y);if(Q===void 0){if(K||y?.optional||y?.defaultValue!==void 0)return;throw Error(`Unknown path element: "${O}" in "${W}"`)}return _=U,M=O,F(Q,j)},$=B0.splitNested(W,y?.separator??".","{","}"),k=void 0;if($.length>0&&$[$.length-1].startsWith("?="))k=$.pop()?.substring(2);$.reverse();let X=F(P,$);if(X===void 0)return k??y?.defaultValue;return X};var s=(P,f)=>{if(typeof P.value==="string"){if(P.quoted)return P.value;if(P.value.startsWith("[")&&P.value.endsWith("]")){let y=P.value.slice(1,-1);if(y.trim()==="")return[];return y.split(";").map((_)=>{let M=_.trim();if(!isNaN(+M))return+M;return M})}if(P.value.startsWith("{")&&P.value.endsWith("}")){if(P.value.slice(1,-1).trim()==="")return{}}if(P.value==="null")return null;if(P.value==="undefined")return;if(P.value.startsWith("@"))return P.value.slice(1);if(!isNaN(+P.value))return+P.value}return f(P.value)},T=(P,f,y,_,M,W)=>{let D=_?.(P)??P,F=M(D);if(F[0])return F[1];if(!f){let $=H(y,P),k=M($);if(k[0])return k[1]}throw Error(W)},N=(P,f,y,_,M)=>{let W=typeof P.types==="function"?P.types(_,f.value,M):P.types?.[_];if(!W||W==="ref")return s(f,(F)=>H(y,F));let D=s(f,()=>f.value);if(Array.isArray(W)){if(!W.includes(D))throw Error(`Argument ${_.toString()} must be one of [${W.join(", ")}]: ${D}`);return D}if(typeof W==="string"){if(W.endsWith("?")){let F=W.slice(0,-1);if(f.value==="null")return J.ReplaceItem(null);if(f.value==="undefined")return J.ReplaceItem(void 0);if(f.value==="")return"";W=F}switch(W){case"any":return D;case"array":return T(D,f.quoted,y,null,(F)=>[Array.isArray(F),F],`Argument ${_.toString()} must be an array: ${D}`);case"object":return T(D,f.quoted,y,null,(F)=>[typeof F==="object"&&F!==null,F],`Argument ${_.toString()} must be an object: ${D}`);case"number":return T(D,f.quoted,y,(F)=>Number(F),(F)=>[!isNaN(F),F],`Argument ${_.toString()} must be a number: ${D}`);case"boolean":return T(D,f.quoted,y,null,(F)=>{if([!0,"true","1",1,"yes","on"].includes(F))return[!0,!0];if([!1,"false","0",0,"no","off"].includes(F))return[!0,!1];if([null,"null","undefined",void 0,""].includes(F))return[!0,!1];if(Array.isArray(F))return[!0,F.length>0];return[!1,!1]},`Argument ${_.toString()} must be a boolean: ${D}`);case"string":return D.toString();default:throw Error(`Unknown type for argument ${_.toString()}: ${W}`)}}return W(D,y,_,f.quoted)};var B=(P,f)=>{let y=P?.[f];if(y===void 0)throw Error(`Unhandled field processor type: ${f}`);return typeof y==="function"?{options:{},fn:y}:{options:y,fn:y.fn}},a=(P)=>!P.quoted&&P.value.startsWith("."),m0=(P)=>{let f={};for(let y of P){if(!a(y))continue;let _=y.value.indexOf("="),M=_>-1?y.value.slice(1,_):y.value.slice(1),W=_>-1?y.value.slice(_+1):"";if(W.length>=2){let D=W[0],F=W[W.length-1];if(D==='"'&&F==='"'||D==="'"&&F==="'")W=W.slice(1,-1)}if(!f[M])f[M]=[];f[M].push(W)}return f},o=(P,f,y)=>{let _=t.splitWithQuotes(P??""),M=m0(_),W=_.filter((F)=>!a(F)),D=f.options.processArgs===!1?[]:W.map((F,$)=>N(f.options,F,y,$,W));return{rawArgs:W,parsedArgs:D,tags:M}};var d0=(P)=>{return P!==null&&typeof P==="object"&&"action"in P},P0=(P,f)=>f.keys!==void 0&&P.key.startsWith(".")&&f.keys[P.key.substring(1)]!==void 0,f0=async(P,f,y,_)=>{let{key:M,parent:W}=P,D=B(_.keys,M.substring(1));if(D.options.processArgs!==!1&&(typeof D.options.types==="function"||D.options.types?.[0]!==void 0))P.value=N(D.options,{value:P.value,quoted:!1},y,0,[]);if(typeof P.value!=="string")await r(P.value,{..._,root:{...y,parent:P.parent,parents:P.parents},filters:[..._.filters??[],...D.options.filters??[]]});let F=await D.fn({path:f,root:y,parent:W,key:M,options:_,value:P.value,args:[],rawArgs:[],tags:{}});return d0(F)?F:J.ReplaceParent(F)};var d=(P)=>{let f=P.split(/\r?\n/),y=f.filter((M)=>M.trim().length>0).map((M)=>/^[ \t]*/.exec(M)?.[0].length??0),_=y.length>0?Math.min(...y):0;if(_===0)return P;return f.map((M)=>M.slice(_)).join(`
2
- `)};var y0=async(P,f,y,_)=>{if(!f?.onSection&&!f?.onSectionConfig&&!f?.sections)return[P,!1];let M=/<@\s*section(?:\s*:\s*([\w-]+))?\s*@>([\s\S]*?)<@\s*\/\s*section\s*@>/g,W,D=P,F=0,$=!1,k={content:D,root:_,path:y,options:f,section:{}};while((W=M.exec(P))!==null){$=!0;let[X,U,j]=W,O=W.index,K=O+X.length,Q=P[K]===`
3
- `;if(Q)K++;let G=O+F,E=K+F,Y={},Z=j,L=/(^|\r?\n)[ \t]*---[ \t]*(\r?\n|$)/m.exec(j);if(L){let z=j.slice(0,L.index),D0=L.index+L[0].length;Z=j.slice(D0);let l=d(z).trim();if(l.length>0)try{Y=f?.configParser?.(l)??{}}catch($0){throw Error(`Failed to parse YAML config for section: ${$0.message}`)}}else if(/^\r?\n/.test(Z))Z=Z.replace(/^\r?\n/,"");Z=d(Z),Z=Z.replace(/(?:\r?\n[ \t]*)+$/,""),Z=Z.replace(/[ \t]+$/,"");let C={config:U?{...Y,type:U}:Y,content:Z};if(k.content=D,k.section=C,Object.keys(C.config).length>0){if(await f.onSectionConfig?.(k)!==!1)await r(C.config,{...f,root:_})}let h=k.section.config.type?f.sections?.[k.section.config.type]:void 0,b;if(h)b=await h(k);else if(f.onSection)b=await f.onSection(k);if(C.trim)C.content=C.content.trim();if(C.indent)C.content=C.content.split(`
4
- `).map((z)=>" ".repeat(C.indent)+z).join(`
5
- `);let I="";if(b===!0||C.show)I=C.content;else if(typeof b==="string")I=b;if(Q&&I!==""&&!I.endsWith(`
6
- `))I+=`
7
- `;let x=D.lastIndexOf(`
8
- `,G-1),g=x===-1?0:x+1,V=D.slice(g,G).trim().length===0?g:G;D=D.slice(0,V)+I+D.slice(E),F+=I.length-(E-V),k.content=D}return[D,$]};var A=(P)=>{let f=[],y=0;while(y<P.length)if(P[y]==="$"&&P[y+1]==="("&&(y===0||P[y-1]!=="\\")){let _=y;y+=2;let M=1;for(;y<P.length;y++)if(P[y]==="(")M++;else if(P[y]===")"){if(M--,M===0){f.push([_,y]);break}}if(M!==0)throw Error(`Unmatched opening $( at position ${_.toString()}`);y++}else y++;return f};var q=Symbol.for("@homedev/fields:OverrideResult"),F0=(P)=>({[q]:!0,value:P}),A0=(P)=>{return P!==null&&typeof P==="object"&&q in P&&P[q]===!0},_0=async(P,f,y)=>{for(let _=f.length-1;_>=0;_--){let[M,W]=f[_],D=P.slice(M+2,W),F=await _0(D,A(D),y);if(typeof F==="object")return F;let $=await y(F,P);if(A0($))return $.value;P=P.slice(0,M)+$+P.slice(W+1)}return P},W0=async(P,f)=>_0(P,A(P),f);var q0=(P)=>{return P!==null&&typeof P==="object"&&"action"in P},R0=/([\w\d.-_/]+):(.+)/,h0=async(P,f,y,_)=>{let M=await(async()=>{let W=R0.exec(P);if(!W)return await H(f,P);let[D,F,$]=W,k=B(_.sources,F),X=o($,k,f);return await k.fn({args:X.parsedArgs,rawArgs:X.rawArgs,tags:X.tags,key:F,options:_,root:f,path:y,value:null})})();if(M===null||M===void 0)return"";if(typeof M==="object")return F0(M);return M},R=async(P,f,y,_)=>{let M=await W0(f,(W)=>h0(W,y,P,_));if(q0(M))return M;if(M===f)return J.Explore();if(typeof M==="string"){let W=await R(P,M,y,_);if(W.action!==S.Explore)return W;return J.ReplaceItem(M)}return J.ReplaceItem(M)};var r=async(P,f)=>{return f??={},await u(P,async(y)=>{let _=[...y.path,y.key].join(".");if(f?.filters?.length&&f.filters.some((F)=>F.test(_)))return J.NoExplore();let M=y.path.length===0,W=y.parents.length>0?y.parents.toReversed():[];if(M){if(W.length>0)throw Error("Root object should not have a parent");W=f?.root?.parents?[f.root.parent,...f.root.parents.toReversed()]:[]}let D={...P,...f.root,this:y.parent,parent:W.length>0?W[0]:null,parents:W};try{if(y.key.startsWith("$"))return J.NoExplore();let F=J.Explore();if(typeof y.value==="string"){let $=y.value,k=!1;while(!0){let[X,U]=await y0($,f,_,D);if(k=k||U,F=await R(_,X,D,f),F.action===S.ReplaceItem&&typeof F.by==="string"){$=F.by;continue}if(F.action===S.Explore&&U&&X!==$)F=J.ReplaceItem(X);break}if(F.action===S.Explore&&$!==y.value)F=J.ReplaceItem($);switch(F.action){case S.Explore:break;case S.ReplaceItem:y.value=F.by;break;default:return F}}if(P0(y,f))F=await f0(y,_,D,f);return F}catch(F){throw Error(`${F.message}
9
- (${_})`)}}),P};var M0=(P,f)=>f?{fn:(y)=>P(...y.args),types:f}:(y)=>P(...y.args),SP=(P,f)=>Object.fromEntries(P.map((y)=>[y.name,M0(y,f?.types?.[y.name])])),IP=(P,f)=>Object.fromEntries((f?.keys??Object.keys(P)).map((y)=>[y,M0(P[y],f?.types?.[y])]));export{r as processFields,IP as makeFieldProcessorMap,SP as makeFieldProcessorList,M0 as makeFieldProcessor,B as getProcessor,o as getArgs};
1
+ Array.prototype.findOrFail=function(r,i,f){let e=this.find(r,f);if(e===void 0)throw Error(i??"Element not found");return e};Array.flat=function(r){if(Array.isArray(r))return r.flat(2);else return[r]};Array.prototype.forEachAsync=async function(r){for(let i=0;i<this.length;i++)await r(this[i],i,this)};Array.prototype.groupBy=function(r){return this.reduce((i,f)=>{let e=r(f);if(!i[e])i[e]=[];return i[e].push(f),i},{})};Array.prototype.createInstances=function(r,...i){return this.map((f,e)=>new r(...i,f,e))};Array.prototype.mapAsync=async function(r){return Promise.all(this.map(r))};Array.prototype.nonNullMap=function(r){let i=[];for(let f=0;f<this.length;f++){let e=r(this[f],f,this);if(e!=null)i.push(e)}return i};Array.prototype.nonNullMapAsync=async function(r){let i=[];for(let f=0;f<this.length;f++){let e=await r(this[f],f,this);if(e!=null)i.push(e)}return i};Array.prototype.mergeAll=function(r){let i={};for(let f of this.toReversed())Object.merge(f,i,r);return i};Array.prototype.toObject=function(r,i){return Object.fromEntries(this.map((f)=>{let e=r(f),t=i?i(f):f;return[e,t]}))};Array.prototype.toObjectWithKey=function(r){return Object.fromEntries(this.map((i)=>[String(i[r]),i]))};Array.prototype.selectFor=function(r,i,f,e){let t=this.find(r,e);if(t===void 0)throw Error(f??"Element not found");return i(t)};Array.prototype.sortBy=function(r,i=!0){return this.slice().sort((f,e)=>{let t=r(f),h=r(e);if(t<h)return i?-1:1;if(t>h)return i?1:-1;return 0})};Array.prototype.unique=function(){return Array.from(new Set(this))};Array.prototype.uniqueBy=function(r){let i=new Set;return this.filter((f)=>{let e=r(f);if(i.has(e))return!1;else return i.add(e),!0})};var k;((r)=>{r[r.Explore=0]="Explore",r[r.SkipSiblings=1]="SkipSiblings",r[r.NoExplore=2]="NoExplore",r[r.ReplaceParent=3]="ReplaceParent",r[r.DeleteParent=4]="DeleteParent",r[r.MergeParent=5]="MergeParent",r[r.ReplaceItem=6]="ReplaceItem",r[r.DeleteItem=7]="DeleteItem"})(k||={});class E{action;by;skipSiblings;reexplore;constructor(r,i,f,e){this.action=r,this.by=i,this.skipSiblings=f,this.reexplore=e}static _explore=new E(0);static _noExplore=new E(2);static _skipSiblings=new E(1);static _deleteParent=new E(4);static ReplaceParent(r,i){return new E(3,r,void 0,i)}static SkipSiblings(){return E._skipSiblings}static Explore(){return E._explore}static NoExplore(){return E._noExplore}static DeleteParent(){return E._deleteParent}static MergeParent(r){return new E(5,r)}static ReplaceItem(r,i){return new E(6,r,i)}static DeleteItem(r){return new E(7,void 0,r)}}Object.navigate=async(r,i)=>{let f=new WeakSet,e=[],t=[],h=async(y,n,O)=>{let P=y===null,j=P?E.Explore():await i({key:y,value:n,path:e,parent:O,parents:t});if(n&&typeof n==="object"&&j.action===0){if(f.has(n))return j;if(f.add(n),!P)e.push(y),t.push(O);let F=n;while(await b(F,y,O))F=O[y];if(!P)t.pop(),e.pop()}return j},b=async(y,n,O)=>{let P=Object.keys(y),j=P.length;for(let F=0;F<j;F++){let d=P[F],$=y[d],S=await h(d,$,y),m=S.action;if(m===0||m===2)continue;if(m===6){if(y[d]=S.by,S.skipSiblings)return;continue}if(m===7){if(delete y[d],S.skipSiblings)return;continue}if(m===1)return;if(m===3){if(n===null)throw Error("Cannot replace root object");if(O[n]=S.by,S.reexplore)return!0;return}if(m===4){if(n===null)throw Error("Cannot delete root object");delete O[n];return}if(m===5)delete y[d],Object.assign(y,S.by)}};await h(null,r,null)};Object.find=async function(r,i){let f=[];return await Object.navigate(r,(e)=>{if(i([...e.path,e.key].join("."),e.value))f.push(e.value);return E.Explore()}),f};Object.merge=(r,i,f)=>{for(let e of Object.keys(i)){if(r[e]===void 0)continue;let t=i[e],h=r[e];if(typeof h!==typeof t||Array.isArray(h)!==Array.isArray(t)){let y=f?.mismatch?.(e,h,t,r,i);if(y!==void 0){i[e]=y;continue}throw Error(`Type mismatch: ${e}`)}let b=f?.mode?.(e,h,t,r,i)??i[".merge"]??"merge";switch(b){case"source":i[e]=h;continue;case"target":continue;case"skip":delete i[e],delete r[e];continue;case"merge":break;default:throw Error(`Unknown merge mode: ${b}`)}if(typeof h==="object")if(Array.isArray(h))t.push(...h);else{if(f?.navigate?.(e,h,t,r,i)===!1)continue;Object.merge(h,t,f)}else{if(h===t)continue;let y=f?.conflict?.(e,h,t,r,i);i[e]=y===void 0?t:y}}for(let e of Object.keys(r))if(i[e]===void 0)i[e]=r[e]};var v=/^\[([^=\]]*)([=]*)([^\]]*)\]$/,a=/^([^[\]]*)\[([^\]]*)]$/,l=(r,i,f,e)=>{if(r.startsWith("{")&&r.endsWith("}")){let t=r.substring(1,r.length-1);return Object.select(i,t,f)?.toString()}return e?r:void 0},rr=(r,i,f,e)=>{let t=l(i,f,e);if(t)return Object.select(r,t,e);let h=v.exec(i);if(h){let[,y,n,O]=h,P=l(y.trim(),f,e,!0),j=l(O.trim(),f,e,!0);if(Array.isArray(r)&&(n==="="||n==="==")&&P)return r.find((F)=>F?.[P]==j)}let b=a.exec(i);if(b){let[,y,n]=b;return r?.[y]?.[n]}return r?.[i]};Object.select=(r,i,f)=>{let e=void 0,t=void 0,h=i??"",b=(j)=>{if(!j)return[j,!1];return j.endsWith("?")?[j.substring(0,j.length-1),!0]:[j,!1]},y=(j,F)=>{let[d,$]=b(F.pop());if(!d){if(f?.set!==void 0&&t!==void 0&&e!==void 0)e[t]=f.set;return j}let S=rr(j,d,r,f);if(S===void 0){if($||f?.optional||f?.defaultValue!==void 0)return;throw Error(`Unknown path element: "${d}" in "${h}"`)}return e=j,t=d,y(S,F)},n=h.splitNested(f?.separator??".","{","}"),O=void 0;if(n.length>0&&n[n.length-1].startsWith("?="))O=n.pop()?.substring(2);n.reverse();let P=y(r,n);if(P===void 0)return O??f?.defaultValue;return P};Object.deepClone=function(r){if(typeof structuredClone<"u")try{return structuredClone(r)}catch{}return JSON.parse(JSON.stringify(r))};Object.toList=function(r,i){return Object.entries(r).map(([f,e])=>({[i]:f,...e}))};Object.mapEntries=function(r,i){let f=Object.entries(r).map(([e,t])=>i(e,t));return Object.fromEntries(f)};global.AsyncFunction=Object.getPrototypeOf(async function(){}).constructor;global.defined=(r,i="Value is undefined")=>{if(r===void 0||r===null)throw Error(i);return r};String.prototype.tokenize=function(r,i=/\${([^}]+)}/g){return this.replace(i,(f,e)=>{let t=r[e];if(t===null||t===void 0)return"";if(typeof t==="string")return t;if(typeof t==="number"||typeof t==="boolean")return String(t);return String(t)})};String.prototype.toPascal=function(){return this.replace(/((?:[_ ]+)\w|^\w)/g,(r,i)=>i.toUpperCase()).replace(/[_ ]/g,"")};String.prototype.capitalize=function(){return this.replace(/((?:[ ]+)\w|^\w)/g,(r,i)=>i.toUpperCase())};String.prototype.toCamel=function(){return this.toPascal().replace(/((?:[ ]+\w)|^\w)/g,(r,i)=>i.toLowerCase())};String.prototype.toSnake=function(){return this.replace(/([a-z])([A-Z])/g,"$1_$2").replace(/\s+/g,"_")};String.prototype.toKebab=function(){return this.replace(/([a-z])([A-Z])/g,"$1-$2").replace(/\s+/g,"-")};String.prototype.changeCase=function(r){switch(r){case"upper":return this.toUpperCase();case"lower":return this.toLowerCase();case"pascal":return this.toPascal();case"camel":return this.toCamel();case"snake":return this.toSnake();case"kebab":return this.toKebab();default:throw Error(`Unsupported case type: ${r}`)}};String.prototype.splitWithQuotes=function(r=","){let i=[],f=this.length,e=r.length,t=0,h=(b)=>{if(b+e>f)return!1;for(let y=0;y<e;y++)if(this[b+y]!==r[y])return!1;return!0};while(t<f){while(t<f&&this[t]===" ")t++;if(t>=f)break;let b="",y=this[t]==='"'||this[t]==="'";if(y){let n=this[t++];while(t<f&&this[t]!==n)b+=this[t++];if(t<f)t++}else{while(t<f&&!h(t)){let n=this[t];if(n==='"'||n==="'"){b+=n,t++;while(t<f&&this[t]!==n)b+=this[t++];if(t<f)b+=this[t++]}else b+=this[t++]}b=b.trim()}if(b)i.push({value:b,quoted:y});if(h(t))t+=e}return i};String.prototype.splitNested=function(r,i,f){let e=0,t="",h=[];for(let b of this){if(t+=b,b===i)e++;if(b===f)e--;if(e===0&&b===r)h.push(t.slice(0,-1)),t=""}if(t)h.push(t);return h};String.prototype.padBlock=function(r,i=`
2
+ `,f=" "){let e=f.repeat(r);return this.split(i).map((t)=>e+t).join(i)};String.prototype.stripIndent=function(){let r=this.split(/\r?\n/),i=r.filter((e)=>e.trim().length>0).map((e)=>/^[ \t]*/.exec(e)?.[0].length??0),f=i.length>0?Math.min(...i):0;if(f===0)return this;return r.map((e)=>e.slice(f)).join(`
3
+ `)};var c=(r,i)=>{if(typeof r.value==="string"){if(r.quoted)return r.value;if(r.value.startsWith("[")&&r.value.endsWith("]")){let f=r.value.slice(1,-1);if(f.trim()==="")return[];return f.split(";").map((e)=>{let t=e.trim();if(!isNaN(+t))return+t;return t})}if(r.value.startsWith("{")&&r.value.endsWith("}")){if(r.value.slice(1,-1).trim()==="")return{}}if(r.value==="null")return null;if(r.value==="undefined")return;if(r.value.startsWith("@"))return r.value.slice(1);if(!isNaN(+r.value))return+r.value}return i(r.value)},_=(r,i,f,e,t,h)=>{let b=e?.(r)??r,y=t(b);if(y[0])return y[1];if(!i){let n=Object.select(f,r),O=t(n);if(O[0])return O[1]}throw Error(h)},z=(r,i,f,e,t)=>{let h=typeof r.types==="function"?r.types(e,i.value,t):r.types?.[e];if(!h||h==="ref")return c(i,(y)=>Object.select(f,y));let b=c(i,()=>i.value);if(Array.isArray(h)){if(!h.includes(b))throw Error(`Argument ${e.toString()} must be one of [${h.join(", ")}]: ${b}`);return b}if(typeof h==="string"){if(h.endsWith("?")){let y=h.slice(0,-1);if(i.value==="null")return E.ReplaceItem(null);if(i.value==="undefined")return E.ReplaceItem(void 0);if(i.value==="")return"";h=y}switch(h){case"any":return b;case"array":return _(b,i.quoted,f,null,(y)=>[Array.isArray(y),y],`Argument ${e.toString()} must be an array: ${b}`);case"object":return _(b,i.quoted,f,null,(y)=>[typeof y==="object"&&y!==null,y],`Argument ${e.toString()} must be an object: ${b}`);case"number":return _(b,i.quoted,f,(y)=>Number(y),(y)=>[!isNaN(y),y],`Argument ${e.toString()} must be a number: ${b}`);case"boolean":return _(b,i.quoted,f,null,(y)=>{if([!0,"true","1",1,"yes","on"].includes(y))return[!0,!0];if([!1,"false","0",0,"no","off"].includes(y))return[!0,!1];if([null,"null","undefined",void 0,""].includes(y))return[!0,!1];if(Array.isArray(y))return[!0,y.length>0];return[!1,!1]},`Argument ${e.toString()} must be a boolean: ${b}`);case"string":return b.toString();default:throw Error(`Unknown type for argument ${e.toString()}: ${h}`)}}return h(b,f,e,i.quoted)};var G=(r,i)=>{let f=r?.[i];if(f===void 0)throw Error(`Unhandled field processor type: ${i}`);return typeof f==="function"?{options:{},fn:f}:{options:f,fn:f.fn}},q=(r)=>!r.quoted&&r.value.startsWith("."),ir=(r)=>{let i={};for(let f of r){if(!q(f))continue;let e=f.value.indexOf("="),t=e>-1?f.value.slice(1,e):f.value.slice(1),h=e>-1?f.value.slice(e+1):"";if(h.length>=2){let b=h[0],y=h[h.length-1];if(b==='"'&&y==='"'||b==="'"&&y==="'")h=h.slice(1,-1)}if(!i[t])i[t]=[];i[t].push(h)}return i},N=(r,i,f)=>{let e=(r??"").splitWithQuotes(),t=ir(e),h=e.filter((y)=>!q(y)),b=i.options.processArgs===!1?[]:h.map((y,n)=>z(i.options,y,f,n,h));return{rawArgs:h,parsedArgs:b,tags:t}};var fr=(r)=>{return r!==null&&typeof r==="object"&&"action"in r},w=(r,i)=>i.keys!==void 0&&r.key.startsWith(".")&&i.keys[r.key.substring(1)]!==void 0,p=async(r,i,f,e)=>{let{key:t,parent:h}=r,b=G(e.keys,t.substring(1));if(b.options.processArgs!==!1&&(typeof b.options.types==="function"||b.options.types?.[0]!==void 0))r.value=z(b.options,{value:r.value,quoted:!1},f,0,[]);if(typeof r.value!=="string")await J(r.value,{...e,root:{...f,parent:r.parent,parents:r.parents},filters:[...e.filters??[],...b.options.filters??[]]});let y=await b.fn({path:i,root:f,parent:h,key:t,options:e,value:r.value,args:[],rawArgs:[],tags:{}});return fr(y)?y:E.ReplaceParent(y)};var R=async(r,i,f,e)=>{if(!i?.onSection&&!i?.onSectionConfig&&!i?.sections)return[r,!1];let t=/<@\s*section(?:\s*:\s*([\w-]+))?\s*@>([\s\S]*?)<@\s*\/\s*section\s*@>/g,h,b=r,y=0,n=!1,O={content:b,root:e,path:f,options:i,section:{}};while((h=t.exec(r))!==null){n=!0;let[P,j,F]=h,d=h.index,$=d+P.length,S=r[$]===`
4
+ `;if(S)$++;let m=d+y,Q=$+y,M={},U=F,W=/(^|\r?\n)[ \t]*---[ \t]*(\r?\n|$)/m.exec(F);if(W){let Z=F.slice(0,W.index),o=W.index+W[0].length;U=F.slice(o);let Y=Z.stripIndent().trim();if(Y.length>0)try{M=i?.configParser?.(Y)??{}}catch(s){throw Error(`Failed to parse YAML config for section: ${s.message}`)}}else if(/^\r?\n/.test(U))U=U.replace(/^\r?\n/,"");U=U.stripIndent().replace(/(?:\r?\n[ \t]*)+$/,"").replace(/[ \t]+$/,"");let A={config:j?{...M,type:j}:M,content:U};if(O.content=b,O.section=A,Object.keys(A.config).length>0){if(await i.onSectionConfig?.(O)!==!1)await J(A.config,{...i,root:e})}let B=O.section.config.type?i.sections?.[O.section.config.type]:void 0,C;if(B)C=await B(O);else if(i.onSection)C=await i.onSection(O);if(A.trim)A.content=A.content.trim();if(A.indent)A.content=A.content.split(`
5
+ `).map((Z)=>" ".repeat(A.indent)+Z).join(`
6
+ `);let D="";if(C===!0||A.show)D=A.content;else if(typeof C==="string")D=C;if(S&&D!==""&&!D.endsWith(`
7
+ `))D+=`
8
+ `;let X=b.lastIndexOf(`
9
+ `,m-1),g=X===-1?0:X+1,H=b.slice(g,m).trim().length===0?g:m;b=b.slice(0,H)+D+b.slice(Q),y+=D.length-(Q-H),O.content=b}return[b,n]};var I=(r)=>{let i=[],f=0;while(f<r.length)if(r[f]==="$"&&r[f+1]==="("&&(f===0||r[f-1]!=="\\")){let e=f;f+=2;let t=1;for(;f<r.length;f++)if(r[f]==="(")t++;else if(r[f]===")"){if(t--,t===0){i.push([e,f]);break}}if(t!==0)throw Error(`Unmatched opening $( at position ${e.toString()}`);f++}else f++;return i};var K=Symbol.for("@homedev/fields:OverrideResult"),T=(r)=>({[K]:!0,value:r}),er=(r)=>{return r!==null&&typeof r==="object"&&K in r&&r[K]===!0},u=async(r,i,f)=>{for(let e=i.length-1;e>=0;e--){let[t,h]=i[e],b=r.slice(t+2,h),y=await u(b,I(b),f);if(typeof y==="object")return y;let n=await f(y,r);if(er(n))return n.value;r=r.slice(0,t)+n+r.slice(h+1)}return r},x=async(r,i)=>u(r,I(r),i);var tr=(r)=>{return r!==null&&typeof r==="object"&&"action"in r},yr=/([\w\d.-_/]+):(.+)/,hr=async(r,i,f,e)=>{let t=await(async()=>{let h=yr.exec(r);if(!h)return await Object.select(i,r);let[b,y,n]=h,O=G(e.sources,y),P=N(n,O,i);return await O.fn({args:P.parsedArgs,rawArgs:P.rawArgs,tags:P.tags,key:y,options:e,root:i,path:f,value:null})})();if(t===null||t===void 0)return"";if(typeof t==="object")return T(t);return t},L=async(r,i,f,e)=>{let t=await x(i,(h)=>hr(h,f,r,e));if(tr(t))return t;if(t===i)return E.Explore();if(typeof t==="string"){let h=await L(r,t,f,e);if(h.action!==k.Explore)return h;return E.ReplaceItem(t)}return E.ReplaceItem(t)};var J=async(r,i)=>{return i??={},await Object.navigate(r,async(f)=>{let e=[...f.path,f.key].join(".");if(i?.filters?.length&&i.filters.some((y)=>y.test(e)))return E.NoExplore();let t=f.path.length===0,h=f.parents.length>0?f.parents.toReversed():[];if(t){if(h.length>0)throw Error("Root object should not have a parent");h=i?.root?.parents?[i.root.parent,...i.root.parents.toReversed()]:[]}let b={...r,...i.root,this:f.parent,parent:h.length>0?h[0]:null,parents:h};try{if(f.key.startsWith("$"))return E.NoExplore();let y=E.Explore();if(typeof f.value==="string"){let n=f.value,O=!1;while(!0){let[P,j]=await R(n,i,e,b);if(O=O||j,y=await L(e,P,b,i),y.action===k.ReplaceItem&&typeof y.by==="string"){n=y.by;continue}if(y.action===k.Explore&&j&&P!==n)y=E.ReplaceItem(P);break}if(y.action===k.Explore&&n!==f.value)y=E.ReplaceItem(n);switch(y.action){case k.Explore:break;case k.ReplaceItem:f.value=y.by;break;default:return y}}if(w(f,i))y=await p(f,e,b,i);return y}catch(y){throw Error(`${y.message}
10
+ (${e})`)}}),r};var V=(r,i)=>i?{fn:(f)=>r(...f.args),types:i}:(f)=>r(...f.args),Hr=(r,i)=>Object.fromEntries(r.map((f)=>[f.name,V(f,i?.types?.[f.name])])),Yr=(r,i)=>Object.fromEntries((i?.keys??Object.keys(r)).map((f)=>[f,V(r[f],i?.types?.[f])]));export{J as processFields,Yr as makeFieldProcessorMap,Hr as makeFieldProcessorList,V as makeFieldProcessor,G as getProcessor,N as getArgs};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homedev/fields",
3
- "version": "1.2.27",
3
+ "version": "1.2.28",
4
4
  "description": "field utilities",
5
5
  "author": "julzor",
6
6
  "license": "ISC",