@homedev/fields 1.2.25 → 1.2.27
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 +1 -0
- package/dist/index.js +8 -8
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -88,6 +88,7 @@ export declare interface ProcessFieldsOptions {
|
|
|
88
88
|
globalContext?: any;
|
|
89
89
|
onSection?: FieldProcessorSectionFunc;
|
|
90
90
|
onSectionConfig?: FieldProcessorSectionConfigFunc;
|
|
91
|
+
configParser?: (configText: string) => any;
|
|
91
92
|
}
|
|
92
93
|
|
|
93
94
|
declare type SourceFunc = (...args: any[]) => any;
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
var
|
|
2
|
-
`)};var
|
|
3
|
-
`;if(
|
|
4
|
-
`).map((z)=>" ".repeat(
|
|
5
|
-
`);let I="";if(b===!0||
|
|
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
6
|
`))I+=`
|
|
7
|
-
`;let
|
|
8
|
-
`,
|
|
9
|
-
(${_})`)}}),P};var
|
|
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};
|