@homedev/fields 1.2.23 → 1.2.25

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
@@ -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?: (section: FieldProcessorSection, context: FieldSectionProcessorContext) => Promise<void | boolean | string> | void | boolean | string;
83
- onSectionConfig?: (section: FieldProcessorSection, context: FieldSectionProcessorContext) => Promise<boolean | void> | boolean | void;
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
- `)X++;M.push({start:$,end:X,name:D,body:Q})}let _=[],E=0;for(let{start:y,end:D,name:Q,body:$}of M){if(y>E)_.push(P.slice(E,y));E=D;let X={},C=$,k=/(^|\r?\n)[ \t]*---[ \t]*(\r?\n|$)/m.exec($);if(k){let O=$.slice(0,k.index),G=k.index+k[0].length;C=$.slice(G);let j=O.trim();if(j.length>0)try{X=Bun.YAML.parse(j)??{}}catch(S){throw Error(`Failed to parse YAML config for section ${Q}: ${S.message}`)}}else if(/^\r?\n/.test(C))C=C.replace(/^\r?\n/,"");if(/[ \t]*\r?\n$/.test(C))C=C.replace(/[\t]*\r?\n$/,"");let J=`__FIELD_SECTION_${W.length.toString()}__`;W.push({name:Q,config:X,content:C,placeholder:J}),_.push(J)}if(E<P.length)_.push(P.slice(E));return{sections:W,content:_.join("")}},t=async(P,F,f,W)=>{let M=F?.onSection?I0(P):null;if(!M)return[P,null];let _={content:M.content,root:W,path:f,options:F};for(let E of M.sections){let y=await I(f,E.name,W,F);if(y.action===U.ReplaceItem)E.name=y.by;if(Object.keys(E.config).length>0){if(await F.onSectionConfig?.(E,_)!==!1)await B(E.config,F)}let D=await F.onSection(E,_);if(E.trim)E.content=E.content.trim();if(E.indent)E.content=E.content.split(`
3
- `).map(($)=>" ".repeat(E.indent)+$).join(`
4
- `);let Q="";if(D===!0||E.show)Q=E.content;else if(typeof D==="string")Q=D;_.content=_.content.replace(E.placeholder,Q)}return[_.content,M]};var N0=(P)=>{return P!==null&&typeof P==="object"&&"action"in P},H0=/([\w\d.-_/]+):(.+)/,b0=async(P,F,f,W)=>{let M=await(async()=>{let _=H0.exec(P);if(!_)return await L(F,P);let[E,y,D]=_,Q=b(W.sources,y),$=e(D,Q,F);return await Q.fn({args:$.parsedArgs,rawArgs:$.rawArgs,tags:$.tags,key:y,options:W,root:F,path:f,value:null})})();if(M===null||M===void 0)return"";if(typeof M==="object")return v(M);return M},I=async(P,F,f,W)=>{let[M,_]=await t(F,W,P,f),E=await c(M,(y)=>b0(y,f,P,W));if(N0(E))return E;if(E===M){if(_?.sections.length)return Z.ReplaceItem(E);return Z.Explore()}if(typeof E==="string"){let y=await I(P,E,f,W);if(y.action!==U.Explore)return y}return Z.ReplaceItem(E)};var B=async(P,F)=>{return F??={},await g(P,async(f)=>{let W=[...f.path,f.key].join(".");if(F?.filters?.length&&F.filters.some((y)=>y.test(W)))return Z.NoExplore();let M=f.path.length===0,_=f.parents.length>0?f.parents.toReversed():[];if(M){if(_.length>0)throw Error("Root object should not have a parent");_=F?.root?.parents?[F.root.parent,...F.root.parents.toReversed()]:[]}let E={...P,...F.root,this:f.parent,parent:_.length>0?_[0]:null,parents:_};try{if(f.key.startsWith("$"))return Z.NoExplore();let y=Z.Explore();if(typeof f.value==="string")switch(y=await I(W,f.value,E,F),y.action){case U.Explore:break;case U.ReplaceItem:f.value=y.by;break;default:return y}if(n(f,F))y=await p(f,W,E,F);return y}catch(y){throw Error(`${y.message}
5
- (${W})`)}}),P};var s=(P,F)=>F?{fn:(f)=>P(...f.args),types:F}:(f)=>P(...f.args),kP=(P,F)=>Object.fromEntries(P.map((f)=>[f.name,s(f,F?.types?.[f.name])])),OP=(P,F)=>Object.fromEntries((F?.keys??Object.keys(P)).map((f)=>[f,s(P[f],F?.types?.[f])]));export{B as processFields,OP as makeFieldProcessorMap,kP as makeFieldProcessorList,s as makeFieldProcessor,b as getProcessor,e as getArgs};
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,p=new WeakMap,X0=(P)=>{var f=p.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 p.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]=()=>_})},e={};G0(e,{splitWithQuotes:()=>C0,splitNested:()=>K0});i.exports=X0(e);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 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 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 c=async(P,f)=>{let y=new WeakSet,_=[],M=[],W=async(F,$,k)=>{let U=F===null,J=U?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($),!U)_.push(F),M.push(k);let j=$;while(await D(j,F,k))j=k[F];if(!U)M.pop(),_.pop()}return J},D=async(F,$,k)=>{let U=Object.keys(F),J=U.length;for(let j=0;j<J;j++){let O=U[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:Y0,defineProperty:u,getOwnPropertyNames:S0}=Object,I0=Object.prototype.hasOwnProperty,H0=(P,f,y)=>{y=P!=null?L0(Y0(P)):{};let _=f||!P||!P.__esModule?u(y,"default",{value:P,enumerable:!0}):y;for(let M of S0(P))if(!I0.call(_,M))u(_,M,{get:()=>P[M],enumerable:!0});return _},b0=(P,f)=>()=>(f||P((f={exports:{}}).exports,f),f.exports),T0=b0((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:()=>U}),f.exports=F(k);var U=(j,O,C="(",Q=")")=>{let K=0,E="",L=[];for(let X of j){if(E+=X,X===C)K++;if(X===Q)K--;if(K===0&&X===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=(X)=>{if(X+K>Q)return!1;for(let G=0;G<K;G++)if(j[X+G]!==O[G])return!1;return!0};while(E<Q){while(E<Q&&j[E]===" ")E++;if(E>=Q)break;let X="",G=j[E]==='"'||j[E]==="'";if(G){let Y=j[E++];while(E<Q&&j[E]!==Y)X+=j[E++];if(E<Q)E++}else{while(E<Q&&!L(E)){let Y=j[E];if(Y==='"'||Y==="'"){X+=Y,E++;while(E<Q&&j[E]!==Y)X+=j[E++];if(E<Q)X+=j[E++]}else X+=j[E++]}X=X.trim()}if(X)C.push({value:X,quoted:G});if(L(E))E+=K}return C}}),N0=H0(T0(),1),B0=/^\[([^=\]]*)([=]*)([^\]]*)\]$/,r0=/^([^[\]]*)\[([^\]]*)]$/,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},z0=(P,f,y,_)=>{let M=m(f,y,_);if(M)return H(P,M,_);let W=B0.exec(f);if(W){let[,F,$,k]=W,U=m(F.trim(),y,_,!0),J=m(k.trim(),y,_,!0);if(Array.isArray(P)&&($==="="||$==="==")&&U)return P.find((j)=>j?.[U]==J)}let D=r0.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=(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)},$=N0.splitNested(W,y?.separator??".","{","}"),k=void 0;if($.length>0&&$[$.length-1].startsWith("?="))k=$.pop()?.substring(2);$.reverse();let U=F(P,$);if(U===void 0)return k??y?.defaultValue;return U};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)},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 n(f,(F)=>H(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 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}},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,$)=>N(f.options,F,y,$,W));return{rawArgs:W,parsedArgs:D,tags:M}};var m0=(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=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 m0(F)?F:Z.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 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[U,J]=W,j=W.index,O=j+U.length,C=P[O]===`
3
+ `;if(C)O++;let Q=j+F,K=O+F,E={},L=J,X=/(^|\r?\n)[ \t]*---[ \t]*(\r?\n|$)/m.exec(J);if(X){let z=J.slice(0,X.index),M0=X.index+X[0].length;L=J.slice(M0);let V=d(z).trim();if(V.length>0)try{E=Bun.YAML.parse(V)??{}}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=d(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 r(G.config,{...f,root:_})}let Y=k.section.config.type?f.sections?.[k.section.config.type]:void 0,b;if(Y)b=await Y(k);else if(f.onSection)b=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 I="";if(b===!0||G.show)I=G.content;else if(typeof b==="string")I=b;if(C&&I!==""&&!I.endsWith(`
6
+ `))I+=`
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)+I+D.slice(K),F+=I.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}),d0=(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(d0($))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 H(f,P);let[D,F,$]=W,k=B(_.sources,F),U=a($,k,f);return await k.fn({args:U.parsedArgs,rawArgs:U.rawArgs,tags:U.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!==S.Explore)return W;return Z.ReplaceItem(M)}return Z.ReplaceItem(M)};var r=async(P,f)=>{return f??={},await c(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 $=y.value,k=!1;while(!0){let[U,J]=await f0($,f,_,D);if(k=k||J,F=await R(_,U,D,f),F.action===S.ReplaceItem&&typeof F.by==="string"){$=F.by;continue}if(F.action===S.Explore&&J&&U!==$)F=Z.ReplaceItem(U);break}if(F.action===S.Explore&&$!==y.value)F=Z.ReplaceItem($);switch(F.action){case S.Explore:break;case S.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),YP=(P,f)=>Object.fromEntries(P.map((y)=>[y.name,W0(y,f?.types?.[y.name])])),SP=(P,f)=>Object.fromEntries((f?.keys??Object.keys(P)).map((y)=>[y,W0(P[y],f?.types?.[y])]));export{r as processFields,SP as makeFieldProcessorMap,YP as makeFieldProcessorList,W0 as makeFieldProcessor,B as getProcessor,a as getArgs};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homedev/fields",
3
- "version": "1.2.23",
3
+ "version": "1.2.25",
4
4
  "description": "field utilities",
5
5
  "author": "julzor",
6
6
  "license": "ISC",