@homedev/fields 1.2.23 → 1.2.24
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 +17 -10
- package/dist/index.js +9 -5
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -31,7 +31,6 @@ export declare interface FieldProcessorOptions {
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
export declare interface FieldProcessorSection {
|
|
34
|
-
name: string;
|
|
35
34
|
config: Record<string, any>;
|
|
36
35
|
content: string;
|
|
37
36
|
trim?: boolean;
|
|
@@ -39,6 +38,20 @@ export declare interface FieldProcessorSection {
|
|
|
39
38
|
show?: boolean;
|
|
40
39
|
}
|
|
41
40
|
|
|
41
|
+
export declare type FieldProcessorSectionConfigFunc = (context: FieldProcessorSectionContext) => Promise<boolean | void> | boolean | void;
|
|
42
|
+
|
|
43
|
+
export declare interface FieldProcessorSectionContext {
|
|
44
|
+
section: FieldProcessorSection;
|
|
45
|
+
content: string;
|
|
46
|
+
path: string;
|
|
47
|
+
root: Record<string, any>;
|
|
48
|
+
options: ProcessFieldsOptions;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export declare type FieldProcessorSectionFunc = (context: FieldProcessorSectionContext) => Promise<void | boolean | string> | void | boolean | string;
|
|
52
|
+
|
|
53
|
+
export declare type FieldProcessorSectionMap = Record<string, FieldProcessorSectionFunc>;
|
|
54
|
+
|
|
42
55
|
export declare type FieldProcessorTypeChecker = (n: number, value: any, args: any[]) => any;
|
|
43
56
|
|
|
44
57
|
export declare type FieldProcessorTypeCheckers<T = unknown> = Record<keyof T, FieldProcessorTypeChecker>;
|
|
@@ -47,13 +60,6 @@ export declare type FieldProcessorTypedFunc = FieldProcessorFunc | (FieldProcess
|
|
|
47
60
|
fn: FieldProcessorFunc;
|
|
48
61
|
});
|
|
49
62
|
|
|
50
|
-
export declare interface FieldSectionProcessorContext {
|
|
51
|
-
content: string;
|
|
52
|
-
path: string;
|
|
53
|
-
root: Record<string, any>;
|
|
54
|
-
options: ProcessFieldsOptions;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
63
|
export declare const makeFieldProcessor: (fn: SourceFunc, types?: FieldProcessorTypeChecker) => FieldProcessorTypedFunc;
|
|
58
64
|
|
|
59
65
|
export declare const makeFieldProcessorList: <T = unknown>(source: SourceFunc[], options?: {
|
|
@@ -76,11 +82,12 @@ export declare const processFields: (obj: any, options?: ProcessFieldsOptions) =
|
|
|
76
82
|
export declare interface ProcessFieldsOptions {
|
|
77
83
|
sources?: FieldProcessorMap;
|
|
78
84
|
keys?: FieldProcessorMap;
|
|
85
|
+
sections?: FieldProcessorSectionMap;
|
|
79
86
|
filters?: RegExp[];
|
|
80
87
|
root?: Record<any, any>;
|
|
81
88
|
globalContext?: any;
|
|
82
|
-
onSection?:
|
|
83
|
-
onSectionConfig?:
|
|
89
|
+
onSection?: FieldProcessorSectionFunc;
|
|
90
|
+
onSectionConfig?: FieldProcessorSectionConfigFunc;
|
|
84
91
|
}
|
|
85
92
|
|
|
86
93
|
declare type SourceFunc = (...args: any[]) => any;
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
var o=Object.create;var{getPrototypeOf:a,defineProperty:R,getOwnPropertyNames:P0}=Object;var f0=Object.prototype.hasOwnProperty;var F0=(P,F,f)=>{f=P!=null?o(a(P)):{};let W=F||!P||!P.__esModule?R(f,"default",{value:P,enumerable:!0}):f;for(let M of P0(P))if(!f0.call(W,M))R(W,M,{get:()=>P[M],enumerable:!0});return W};var y0=(P,F)=>()=>(F||P((F={exports:{}}).exports,F),F.exports);var x=y0((z0,h)=>{var{defineProperty:z,getOwnPropertyNames:W0,getOwnPropertyDescriptor:_0}=Object,E0=Object.prototype.hasOwnProperty,q=new WeakMap,M0=(P)=>{var F=q.get(P),f;if(F)return F;if(F=z({},"__esModule",{value:!0}),P&&typeof P==="object"||typeof P==="function")W0(P).map((W)=>!E0.call(F,W)&&z(F,W,{get:()=>P[W],enumerable:!(f=_0(P,W))||f.enumerable}));return q.set(P,F),F},D0=(P,F)=>{for(var f in F)z(P,f,{get:F[f],enumerable:!0,configurable:!0,set:(W)=>F[f]=()=>W})},m={};D0(m,{splitWithQuotes:()=>Q0,splitNested:()=>C0});h.exports=M0(m);var C0=(P,F,f="(",W=")")=>{let M=0,_="",E=[];for(let y of P){if(_+=y,y===f)M++;if(y===W)M--;if(M===0&&y===F)E.push(_.slice(0,-1)),_=""}if(_)E.push(_);return E},Q0=(P,F=",")=>{let f=[],W=P.length,M=F.length,_=0,E=(y)=>{if(y+M>W)return!1;for(let D=0;D<M;D++)if(P[y+D]!==F[D])return!1;return!0};while(_<W){while(_<W&&P[_]===" ")_++;if(_>=W)break;let y="",D=P[_]==='"'||P[_]==="'";if(D){let Q=P[_++];while(_<W&&P[_]!==Q)y+=P[_++];if(_<W)_++}else{while(_<W&&!E(_)){let Q=P[_];if(Q==='"'||Q==="'"){y+=Q,_++;while(_<W&&P[_]!==Q)y+=P[_++];if(_<W)y+=P[_++]}else y+=P[_++]}y=y.trim()}if(y)f.push({value:y,quoted:D});if(E(_))_+=M}return f}});var i=F0(x(),1);var U;((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"})(U||={});class Z{action;by;skipSiblings;reexplore;constructor(P,F,f,W){this.action=P,this.by=F,this.skipSiblings=f,this.reexplore=W}static _explore=new Z(0);static _noExplore=new Z(2);static _skipSiblings=new Z(1);static _deleteParent=new Z(4);static ReplaceParent(P,F){return new Z(3,P,void 0,F)}static SkipSiblings(){return Z._skipSiblings}static Explore(){return Z._explore}static NoExplore(){return Z._noExplore}static DeleteParent(){return Z._deleteParent}static MergeParent(P){return new Z(5,P)}static ReplaceItem(P,F){return new Z(6,P,F)}static DeleteItem(P){return new Z(7,void 0,P)}}var g=async(P,F)=>{let f=new WeakSet,W=[],M=[],_=async(y,D,Q)=>{let $=y===null,X=$?Z.Explore():await F({key:y,value:D,path:W,parent:Q,parents:M});if(D&&typeof D==="object"&&X.action===0){if(f.has(D))return X;if(f.add(D),!$)W.push(y),M.push(Q);let C=D;while(await E(C,y,Q))C=Q[y];if(!$)M.pop(),W.pop()}return X},E=async(y,D,Q)=>{let $=Object.keys(y),X=$.length;for(let C=0;C<X;C++){let k=$[C],J=y[k],O=await _(k,J,y),G=O.action;if(G===0||G===2)continue;if(G===6){if(y[k]=O.by,O.skipSiblings)return;continue}if(G===7){if(delete y[k],O.skipSiblings)return;continue}if(G===1)return;if(G===3){if(D===null)throw Error("Cannot replace root object");if(Q[D]=O.by,O.reexplore)return!0;return}if(G===4){if(D===null)throw Error("Cannot delete root object");delete Q[D];return}if(G===5)delete y[k],Object.assign(y,O.by)}};await _(null,P,null)};var j0=Object.create,{getPrototypeOf:k0,defineProperty:r,getOwnPropertyNames:O0}=Object,$0=Object.prototype.hasOwnProperty,X0=(P,F,f)=>{f=P!=null?j0(k0(P)):{};let W=F||!P||!P.__esModule?r(f,"default",{value:P,enumerable:!0}):f;for(let M of O0(P))if(!$0.call(W,M))r(W,M,{get:()=>P[M],enumerable:!0});return W},Z0=(P,F)=>()=>(F||P((F={exports:{}}).exports,F),F.exports),J0=Z0((P,F)=>{var{defineProperty:f,getOwnPropertyNames:W,getOwnPropertyDescriptor:M}=Object,_=Object.prototype.hasOwnProperty,E=new WeakMap,y=(C)=>{var k=E.get(C),J;if(k)return k;if(k=f({},"__esModule",{value:!0}),C&&typeof C==="object"||typeof C==="function")W(C).map((O)=>!_.call(k,O)&&f(k,O,{get:()=>C[O],enumerable:!(J=M(C,O))||J.enumerable}));return E.set(C,k),k},D=(C,k)=>{for(var J in k)f(C,J,{get:k[J],enumerable:!0,configurable:!0,set:(O)=>k[J]=()=>O})},Q={};D(Q,{splitWithQuotes:()=>X,splitNested:()=>$}),F.exports=y(Q);var $=(C,k,J="(",O=")")=>{let G=0,j="",S=[];for(let K of C){if(j+=K,K===J)G++;if(K===O)G--;if(G===0&&K===k)S.push(j.slice(0,-1)),j=""}if(j)S.push(j);return S},X=(C,k=",")=>{let J=[],O=C.length,G=k.length,j=0,S=(K)=>{if(K+G>O)return!1;for(let Y=0;Y<G;Y++)if(C[K+Y]!==k[Y])return!1;return!0};while(j<O){while(j<O&&C[j]===" ")j++;if(j>=O)break;let K="",Y=C[j]==='"'||C[j]==="'";if(Y){let T=C[j++];while(j<O&&C[j]!==T)K+=C[j++];if(j<O)j++}else{while(j<O&&!S(j)){let T=C[j];if(T==='"'||T==="'"){K+=T,j++;while(j<O&&C[j]!==T)K+=C[j++];if(j<O)K+=C[j++]}else K+=C[j++]}K=K.trim()}if(K)J.push({value:K,quoted:Y});if(S(j))j+=G}return J}}),G0=X0(J0(),1),K0=/^\[([^=\]]*)([=]*)([^\]]*)\]$/,S0=/^([^[\]]*)\[([^\]]*)]$/,A=(P,F,f,W)=>{if(P.startsWith("{")&&P.endsWith("}")){let M=P.substring(1,P.length-1);return L(F,M,f)?.toString()}return W?P:void 0},U0=(P,F,f,W)=>{let M=A(F,f,W);if(M)return L(P,M,W);let _=K0.exec(F);if(_){let[,y,D,Q]=_,$=A(y.trim(),f,W,!0),X=A(Q.trim(),f,W,!0);if(Array.isArray(P)&&(D==="="||D==="==")&&$)return P.find((C)=>C?.[$]==X)}let E=S0.exec(F);if(E){let[,y,D]=E;return P?.[y]?.[D]}return P?.[F]},L=(P,F,f)=>{let W=void 0,M=void 0,_=F??"",E=(X)=>{if(!X)return[X,!1];return X.endsWith("?")?[X.substring(0,X.length-1),!0]:[X,!1]},y=(X,C)=>{let[k,J]=E(C.pop());if(!k){if(f?.set!==void 0&&M!==void 0&&W!==void 0)W[M]=f.set;return X}let O=U0(X,k,P,f);if(O===void 0){if(J||f?.optional||f?.defaultValue!==void 0)return;throw Error(`Unknown path element: "${k}" in "${_}"`)}return W=X,M=k,y(O,C)},D=G0.splitNested(_,f?.separator??".","{","}"),Q=void 0;if(D.length>0&&D[D.length-1].startsWith("?="))Q=D.pop()?.substring(2);D.reverse();let $=y(P,D);if($===void 0)return Q??f?.defaultValue;return $};var V=(P,F)=>{if(typeof P.value==="string"){if(P.quoted)return P.value;if(P.value.startsWith("[")&&P.value.endsWith("]")){let f=P.value.slice(1,-1);if(f.trim()==="")return[];return f.split(";").map((W)=>{let M=W.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)},N=(P,F,f,W,M,_)=>{let E=W?.(P)??P,y=M(E);if(y[0])return y[1];if(!F){let D=L(f,P),Q=M(D);if(Q[0])return Q[1]}throw Error(_)},H=(P,F,f,W,M)=>{let _=typeof P.types==="function"?P.types(W,F.value,M):P.types?.[W];if(!_||_==="ref")return V(F,(y)=>L(f,y));let E=V(F,()=>F.value);if(Array.isArray(_)){if(!_.includes(E))throw Error(`Argument ${W.toString()} must be one of [${_.join(", ")}]: ${E}`);return E}if(typeof _==="string"){if(_.endsWith("?")){let y=_.slice(0,-1);if(F.value==="null")return Z.ReplaceItem(null);if(F.value==="undefined")return Z.ReplaceItem(void 0);if(F.value==="")return"";_=y}switch(_){case"any":return E;case"array":return N(E,F.quoted,f,null,(y)=>[Array.isArray(y),y],`Argument ${W.toString()} must be an array: ${E}`);case"object":return N(E,F.quoted,f,null,(y)=>[typeof y==="object"&&y!==null,y],`Argument ${W.toString()} must be an object: ${E}`);case"number":return N(E,F.quoted,f,(y)=>Number(y),(y)=>[!isNaN(y),y],`Argument ${W.toString()} must be a number: ${E}`);case"boolean":return N(E,F.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 ${W.toString()} must be a boolean: ${E}`);case"string":return E.toString();default:throw Error(`Unknown type for argument ${W.toString()}: ${_}`)}}return _(E,f,W,F.quoted)};var b=(P,F)=>{let f=P?.[F];if(f===void 0)throw Error(`Unhandled field processor type: ${F}`);return typeof f==="function"?{options:{},fn:f}:{options:f,fn:f.fn}},l=(P)=>!P.quoted&&P.value.startsWith("."),Y0=(P)=>{let F={};for(let f of P){if(!l(f))continue;let W=f.value.indexOf("="),M=W>-1?f.value.slice(1,W):f.value.slice(1),_=W>-1?f.value.slice(W+1):"";if(_.length>=2){let E=_[0],y=_[_.length-1];if(E==='"'&&y==='"'||E==="'"&&y==="'")_=_.slice(1,-1)}if(!F[M])F[M]=[];F[M].push(_)}return F},e=(P,F,f)=>{let W=i.splitWithQuotes(P??""),M=Y0(W),_=W.filter((y)=>!l(y)),E=F.options.processArgs===!1?[]:_.map((y,D)=>H(F.options,y,f,D,_));return{rawArgs:_,parsedArgs:E,tags:M}};var L0=(P)=>{return P!==null&&typeof P==="object"&&"action"in P},n=(P,F)=>F.keys!==void 0&&P.key.startsWith(".")&&F.keys[P.key.substring(1)]!==void 0,p=async(P,F,f,W)=>{let{key:M,parent:_}=P,E=b(W.keys,M.substring(1));if(E.options.processArgs!==!1&&(typeof E.options.types==="function"||E.options.types?.[0]!==void 0))P.value=H(E.options,{value:P.value,quoted:!1},f,0,[]);if(typeof P.value!=="string")await B(P.value,{...W,root:{...f,parent:P.parent,parents:P.parents},filters:[...W.filters??[],...E.options.filters??[]]});let y=await E.fn({path:F,root:f,parent:_,key:M,options:W,value:P.value,args:[],rawArgs:[],tags:{}});return L0(y)?y:Z.ReplaceParent(y)};var d=(P)=>{let F=[],f=0;while(f<P.length)if(P[f]==="$"&&P[f+1]==="("&&(f===0||P[f-1]!=="\\")){let W=f;f+=2;let M=1;for(;f<P.length;f++)if(P[f]==="(")M++;else if(P[f]===")"){if(M--,M===0){F.push([W,f]);break}}if(M!==0)throw Error(`Unmatched opening $( at position ${W.toString()}`);f++}else f++;return F};var w=Symbol.for("@homedev/fields:OverrideResult"),v=(P)=>({[w]:!0,value:P}),T0=(P)=>{return P!==null&&typeof P==="object"&&w in P&&P[w]===!0},u=async(P,F,f)=>{for(let W=F.length-1;W>=0;W--){let[M,_]=F[W],E=P.slice(M+2,_),y=await u(E,d(E),f);if(typeof y==="object")return y;let D=await f(y,P);if(T0(D))return D.value;P=P.slice(0,M)+D+P.slice(_+1)}return P},c=async(P,F)=>u(P,d(P),F);var I0=(P)=>{let F=/%{\s*section\s+([\w\d_-]+)\s*}([\s\S]*?)%{\s*endsection\s+\1\s*}/g,f,W=[],M=[];while((f=F.exec(P))!==null){let[y,D,Q]=f,$=f.index,X=$+y.length;if(P[X]===`
|
|
2
|
-
`)
|
|
3
|
-
|
|
4
|
-
`)
|
|
5
|
-
|
|
1
|
+
var $0=Object.create;var{getPrototypeOf:j0,defineProperty:l,getOwnPropertyNames:k0}=Object;var E0=Object.prototype.hasOwnProperty;var Q0=(P,f,y)=>{y=P!=null?$0(j0(P)):{};let _=f||!P||!P.__esModule?l(y,"default",{value:P,enumerable:!0}):y;for(let M of k0(P))if(!E0.call(_,M))l(_,M,{get:()=>P[M],enumerable:!0});return _};var O0=(P,f)=>()=>(f||P((f={exports:{}}).exports,f),f.exports);var v=O0((g0,i)=>{var{defineProperty:w,getOwnPropertyNames:Z0,getOwnPropertyDescriptor:J0}=Object,U0=Object.prototype.hasOwnProperty,V=new WeakMap,X0=(P)=>{var f=V.get(P),y;if(f)return f;if(f=w({},"__esModule",{value:!0}),P&&typeof P==="object"||typeof P==="function")Z0(P).map((_)=>!U0.call(f,_)&&w(f,_,{get:()=>P[_],enumerable:!(y=J0(P,_))||y.enumerable}));return V.set(P,f),f},G0=(P,f)=>{for(var y in f)w(P,y,{get:f[y],enumerable:!0,configurable:!0,set:(_)=>f[y]=()=>_})},p={};G0(p,{splitWithQuotes:()=>C0,splitNested:()=>K0});i.exports=X0(p);var K0=(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},C0=(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 s=Q0(v(),1);var r;((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"})(r||={});class Z{action;by;skipSiblings;reexplore;constructor(P,f,y,_){this.action=P,this.by=f,this.skipSiblings=y,this.reexplore=_}static _explore=new Z(0);static _noExplore=new Z(2);static _skipSiblings=new Z(1);static _deleteParent=new Z(4);static ReplaceParent(P,f){return new Z(3,P,void 0,f)}static SkipSiblings(){return Z._skipSiblings}static Explore(){return Z._explore}static NoExplore(){return Z._noExplore}static DeleteParent(){return Z._deleteParent}static MergeParent(P){return new Z(5,P)}static ReplaceItem(P,f){return new Z(6,P,f)}static DeleteItem(P){return new Z(7,void 0,P)}}var u=async(P,f)=>{let y=new WeakSet,_=[],M=[],W=async(F,$,k)=>{let X=F===null,J=X?Z.Explore():await f({key:F,value:$,path:_,parent:k,parents:M});if($&&typeof $==="object"&&J.action===0){if(y.has($))return J;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 J},D=async(F,$,k)=>{let X=Object.keys(F),J=X.length;for(let j=0;j<J;j++){let O=X[j],C=F[O],Q=await W(O,C,F),K=Q.action;if(K===0||K===2)continue;if(K===6){if(F[O]=Q.by,Q.skipSiblings)return;continue}if(K===7){if(delete F[O],Q.skipSiblings)return;continue}if(K===1)return;if(K===3){if($===null)throw Error("Cannot replace root object");if(k[$]=Q.by,Q.reexplore)return!0;return}if(K===4){if($===null)throw Error("Cannot delete root object");delete k[$];return}if(K===5)delete F[O],Object.assign(F,Q.by)}};await W(null,P,null)};var L0=Object.create,{getPrototypeOf:S0,defineProperty:c,getOwnPropertyNames:Y0}=Object,r0=Object.prototype.hasOwnProperty,I0=(P,f,y)=>{y=P!=null?L0(S0(P)):{};let _=f||!P||!P.__esModule?c(y,"default",{value:P,enumerable:!0}):y;for(let M of Y0(P))if(!r0.call(_,M))c(_,M,{get:()=>P[M],enumerable:!0});return _},H0=(P,f)=>()=>(f||P((f={exports:{}}).exports,f),f.exports),N0=H0((P,f)=>{var{defineProperty:y,getOwnPropertyNames:_,getOwnPropertyDescriptor:M}=Object,W=Object.prototype.hasOwnProperty,D=new WeakMap,F=(j)=>{var O=D.get(j),C;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:!(C=M(j,Q))||C.enumerable}));return D.set(j,O),O},$=(j,O)=>{for(var C in O)y(j,C,{get:O[C],enumerable:!0,configurable:!0,set:(Q)=>O[C]=()=>Q})},k={};$(k,{splitWithQuotes:()=>J,splitNested:()=>X}),f.exports=F(k);var X=(j,O,C="(",Q=")")=>{let K=0,E="",L=[];for(let U of j){if(E+=U,U===C)K++;if(U===Q)K--;if(K===0&&U===O)L.push(E.slice(0,-1)),E=""}if(E)L.push(E);return L},J=(j,O=",")=>{let C=[],Q=j.length,K=O.length,E=0,L=(U)=>{if(U+K>Q)return!1;for(let G=0;G<K;G++)if(j[U+G]!==O[G])return!1;return!0};while(E<Q){while(E<Q&&j[E]===" ")E++;if(E>=Q)break;let U="",G=j[E]==='"'||j[E]==="'";if(G){let S=j[E++];while(E<Q&&j[E]!==S)U+=j[E++];if(E<Q)E++}else{while(E<Q&&!L(E)){let S=j[E];if(S==='"'||S==="'"){U+=S,E++;while(E<Q&&j[E]!==S)U+=j[E++];if(E<Q)U+=j[E++]}else U+=j[E++]}U=U.trim()}if(U)C.push({value:U,quoted:G});if(L(E))E+=K}return C}}),b0=I0(N0(),1),T0=/^\[([^=\]]*)([=]*)([^\]]*)\]$/,B0=/^([^[\]]*)\[([^\]]*)]$/,d=(P,f,y,_)=>{if(P.startsWith("{")&&P.endsWith("}")){let M=P.substring(1,P.length-1);return I(f,M,y)?.toString()}return _?P:void 0},z0=(P,f,y,_)=>{let M=d(f,y,_);if(M)return I(P,M,_);let W=T0.exec(f);if(W){let[,F,$,k]=W,X=d(F.trim(),y,_,!0),J=d(k.trim(),y,_,!0);if(Array.isArray(P)&&($==="="||$==="==")&&X)return P.find((j)=>j?.[X]==J)}let D=B0.exec(f);if(D){let[,F,$]=D;return P?.[F]?.[$]}return P?.[f]},I=(P,f,y)=>{let _=void 0,M=void 0,W=f??"",D=(J)=>{if(!J)return[J,!1];return J.endsWith("?")?[J.substring(0,J.length-1),!0]:[J,!1]},F=(J,j)=>{let[O,C]=D(j.pop());if(!O){if(y?.set!==void 0&&M!==void 0&&_!==void 0)_[M]=y.set;return J}let Q=z0(J,O,P,y);if(Q===void 0){if(C||y?.optional||y?.defaultValue!==void 0)return;throw Error(`Unknown path element: "${O}" in "${W}"`)}return _=J,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 n=(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)},N=(P,f,y,_,M,W)=>{let D=_?.(P)??P,F=M(D);if(F[0])return F[1];if(!f){let $=I(y,P),k=M($);if(k[0])return k[1]}throw Error(W)},b=(P,f,y,_,M)=>{let W=typeof P.types==="function"?P.types(_,f.value,M):P.types?.[_];if(!W||W==="ref")return n(f,(F)=>I(y,F));let D=n(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 Z.ReplaceItem(null);if(f.value==="undefined")return Z.ReplaceItem(void 0);if(f.value==="")return"";W=F}switch(W){case"any":return D;case"array":return N(D,f.quoted,y,null,(F)=>[Array.isArray(F),F],`Argument ${_.toString()} must be an array: ${D}`);case"object":return N(D,f.quoted,y,null,(F)=>[typeof F==="object"&&F!==null,F],`Argument ${_.toString()} must be an object: ${D}`);case"number":return N(D,f.quoted,y,(F)=>Number(F),(F)=>[!isNaN(F),F],`Argument ${_.toString()} must be a number: ${D}`);case"boolean":return N(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 T=(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}},t=(P)=>!P.quoted&&P.value.startsWith("."),w0=(P)=>{let f={};for(let y of P){if(!t(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},a=(P,f,y)=>{let _=s.splitWithQuotes(P??""),M=w0(_),W=_.filter((F)=>!t(F)),D=f.options.processArgs===!1?[]:W.map((F,$)=>b(f.options,F,y,$,W));return{rawArgs:W,parsedArgs:D,tags:M}};var d0=(P)=>{return P!==null&&typeof P==="object"&&"action"in P},o=(P,f)=>f.keys!==void 0&&P.key.startsWith(".")&&f.keys[P.key.substring(1)]!==void 0,P0=async(P,f,y,_)=>{let{key:M,parent:W}=P,D=T(_.keys,M.substring(1));if(D.options.processArgs!==!1&&(typeof D.options.types==="function"||D.options.types?.[0]!==void 0))P.value=b(D.options,{value:P.value,quoted:!1},y,0,[]);if(typeof P.value!=="string")await B(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:Z.ReplaceParent(F)};var m=(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 f0=async(P,f,y,_)=>{if(!f?.onSection&&!f?.onSectionConfig&&!f?.sections)return[P,!1];let M=/%{\s*section\s*}%([\s\S]*?)%{\s*endsection\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,J]=W,j=W.index,O=j+X.length,C=P[O]===`
|
|
3
|
+
`;if(C)O++;let Q=j+F,K=O+F,E={},L=J,U=/(^|\r?\n)[ \t]*---[ \t]*(\r?\n|$)/m.exec(J);if(U){let z=J.slice(0,U.index),M0=U.index+U[0].length;L=J.slice(M0);let e=m(z).trim();if(e.length>0)try{E=Bun.YAML.parse(e)??{}}catch(D0){throw Error(`Failed to parse YAML config for section: ${D0.message}`)}}else if(/^\r?\n/.test(L))L=L.replace(/^\r?\n/,"");L=m(L),L=L.replace(/(?:\r?\n[ \t]*)+$/,""),L=L.replace(/[ \t]+$/,"");let G={config:E,content:L};if(k.content=D,k.section=G,Object.keys(G.config).length>0){if(await f.onSectionConfig?.(k)!==!1)await B(G.config,{...f,root:_})}let S=k.section.config.type?f.sections?.[k.section.config.type]:void 0,H;if(S)H=await S(k);else if(f.onSection)H=await f.onSection(k);if(G.trim)G.content=G.content.trim();if(G.indent)G.content=G.content.split(`
|
|
4
|
+
`).map((z)=>" ".repeat(G.indent)+z).join(`
|
|
5
|
+
`);let Y="";if(H===!0||G.show)Y=G.content;else if(typeof H==="string")Y=H;if(C&&Y!==""&&!Y.endsWith(`
|
|
6
|
+
`))Y+=`
|
|
7
|
+
`;let h=D.lastIndexOf(`
|
|
8
|
+
`,Q-1),x=h===-1?0:h+1,g=D.slice(x,Q).trim().length===0?x:Q;D=D.slice(0,g)+Y+D.slice(K),F+=Y.length-(K-g),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"),y0=(P)=>({[q]:!0,value:P}),m0=(P)=>{return P!==null&&typeof P==="object"&&q in P&&P[q]===!0},F0=async(P,f,y)=>{for(let _=f.length-1;_>=0;_--){let[M,W]=f[_],D=P.slice(M+2,W),F=await F0(D,A(D),y);if(typeof F==="object")return F;let $=await y(F,P);if(m0($))return $.value;P=P.slice(0,M)+$+P.slice(W+1)}return P},_0=async(P,f)=>F0(P,A(P),f);var A0=(P)=>{return P!==null&&typeof P==="object"&&"action"in P},q0=/([\w\d.-_/]+):(.+)/,R0=async(P,f,y,_)=>{let M=await(async()=>{let W=q0.exec(P);if(!W)return await I(f,P);let[D,F,$]=W,k=T(_.sources,F),X=a($,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 y0(M);return M},R=async(P,f,y,_)=>{let M=await _0(f,(W)=>R0(W,y,P,_));if(A0(M))return M;if(M===f)return Z.Explore();if(typeof M==="string"){let W=await R(P,M,y,_);if(W.action!==r.Explore)return W}return Z.ReplaceItem(M)};var B=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 Z.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 Z.NoExplore();let F=Z.Explore();if(typeof y.value==="string"){let[$,k]=await f0(y.value,f,_,D);if(F=await R(_,$,D,f),F.action===r.Explore&&k&&$!==y.value)F=Z.ReplaceItem($);switch(F.action){case r.Explore:break;case r.ReplaceItem:y.value=F.by;break;default:return F}}if(o(y,f))F=await P0(y,_,D,f);return F}catch(F){throw Error(`${F.message}
|
|
9
|
+
(${_})`)}}),P};var W0=(P,f)=>f?{fn:(y)=>P(...y.args),types:f}:(y)=>P(...y.args),SP=(P,f)=>Object.fromEntries(P.map((y)=>[y.name,W0(y,f?.types?.[y.name])])),YP=(P,f)=>Object.fromEntries((f?.keys??Object.keys(P)).map((y)=>[y,W0(P[y],f?.types?.[y])]));export{B as processFields,YP as makeFieldProcessorMap,SP as makeFieldProcessorList,W0 as makeFieldProcessor,T as getProcessor,a as getArgs};
|