@homedev/objector 0.0.12 → 0.0.13
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.js +1 -1
- package/dist/objector.d.ts +1 -16
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import v from"path";import D from"fs/promises";import X from"path";import r from"yaml";var h=async(T)=>{try{return await D.access(T,D.constants.R_OK),!0}catch{return!1}},G=async(T,F)=>{let P=await(async()=>{let $=[""];if(!X.isAbsolute(T)&&F?.dirs)$.push(...F.dirs);for(let W of $){let U=X.join(W,T);if(await h(U))return X.resolve(U)}throw new Error(`File not found: "${T}"`)})(),O=await D.readFile(P),C=X.dirname(P),M={"[.](yml|yaml)$":r.parse,"[.]json$":JSON.parse,...F?.parsers},E=Object.entries(M).find(($)=>P.match($[0]));if(!E)throw new Error(`Unhandled file type: ${T}`);return{content:E[1](O.toString()),fullPath:P,folderPath:C}};var L=async(T,F)=>{for(let j of T){let P=F?.root?v.join(F.root,j):j,O=await G(P,F?.loadOptions);if(F?.loaded)await F.loaded(O)}};var Z=(T,F,j)=>{for(let P of Object.keys(F)){if(T[P]===void 0)continue;let O=F[P],C=T[P];if(typeof C!==typeof O||Array.isArray(C)!==Array.isArray(O)){let E=j?.mismatch?.(P,C,O,T,F);if(E!==void 0){F[P]=E;continue}throw new Error(`Type mismatch: ${P}`)}let M=j?.mode?.(P,C,O,T,F)??"merge";switch(M){case"source":F[P]=C;continue;case"target":continue;case"skip":delete F[P],delete T[P];continue;case"merge":break;default:throw new Error(`Unknown merge mode: ${M}`)}if(typeof C==="object")if(Array.isArray(C))O.push(...C);else{if(j?.navigate?.(P,C,O,T,F)===!1)continue;Z(C,O,j)}else{if(C===O)continue;let E=j?.conflict?.(P,C,O,T,F);F[P]=E===void 0?O:E}}for(let P of Object.keys(T))if(F[P]===void 0)F[P]=T[P]};var Y;((C)=>{C[C.Continue=0]="Continue";C[C.Skip=1]="Skip";C[C.ReplaceItem=2]="ReplaceItem";C[C.ReplaceParent=3]="ReplaceParent";C[C.DeleteParent=4]="DeleteParent"})(Y||={});class I{action;by;constructor(T,F){this.action=T;this.by=F}static ReplaceParent(T){return new I(3,T)}static ReplaceItem(T){return new I(2,T)}static Skip(){return new I(1)}static Continue(){return new I(0)}static DeleteParent(){return new I(4)}}var S=async(T,F)=>{let j=async(O)=>{for(let[C,M]of Object.entries(O.value)){let E=await P({key:C,value:M,path:O.path,parent:O.value});switch(E.action){case 3:if(!O.key)throw new Error("Cannot replace root");O.parent[O.key]=E.by;return;case 2:O.value[C]=E.by;break;case 1:return;case 4:delete O.parent[O.key];return}}},P=async(O)=>{let C=O.key?await F(O):{action:0};if(C.action===0){if(O.value&&typeof O.value==="object"){if(O.key)O.path.push(O.key);if(await j(O),O.key)O.path.pop()}}return C};await P({key:null,value:T,path:[],parent:null})};var z=async(T,F,j,P)=>{for(let[O,C]of P?.keys??[]){let M=O.exec(T.key);if(M){let[,E,...w]=M;if(C[E]){let $={args:[T.value,...w],path:F,value:T.value,root:j,parent:T.parent,key:T.key,origin:P?.origin,options:P},W=typeof C[E]==="function"?await C[E]($):C[E];if(W===null)return I.DeleteParent();if(W===void 0)return I.Continue();return I.ReplaceParent(W)}}}};var K=async(T,F,j,P)=>{let O,C=[],M=F;for(let[E,w]of P?.sources??[]){if(O)break;M=M.replaceAll(E,($,W,...U)=>{if(O)return $;let B=w[W];if(typeof B!=="object"&&typeof B!=="function")return B??$;if(typeof B==="object")return O=B,$;let Q=B({args:U,path:T,value:M,root:j,key:W,options:P});if(typeof Q==="object")return O=Q,$;return Q instanceof Promise?`ð[${C.push(Q)}]ñ`:Q})}if(O){let E=await O;if(E===null)return I.DeleteParent();if(E)return I.ReplaceItem(E)}if(C.length){let E=await Promise.all(C);M=M.replaceAll(/\xf0\[(\d+)]\xf1/g,(w,$)=>E[+$-1])}return M===F?I.Continue():I.ReplaceItem(M)};var _=async(T,F)=>S(T,async(j)=>{let P=[...j.path,j.key].join(".");if(F?.filters){if(!F.filters.some((C)=>C.test(P)))return I.Continue()}let O=async(C)=>{let M=await K(P,C,F?.root??T,F);if(M.action===2&&typeof M.by==="string"){let E=await O(M.by);if(E.by)return E}return M};return await z(j,P,F?.root??T,F)??(typeof j.value==="string"?await O(j.value):I.Continue())});import N from"yaml";var R=(T,F="name")=>Object.entries(T).map(([j,P])=>({[F]:j,...P})),wF=(T,F="name")=>Object.fromEntries(T.map((j)=>[j[F],j])),A=(T,F="name")=>Object.fromEntries(T.map((j)=>{let{[F]:P,...O}=j;return[P,O]}));var y=(T,F,j="(",P=")")=>{let O=0,C="",M=[];for(let E of T){if(C+=E,E===j)O++;if(E===P)O--;if(O===0&&E===F)M.push(C.slice(0,-1)),C=""}if(C)M.push(C);return M};var d=(T,F,j,P)=>{if(F.startsWith("{")){let M=F.substring(1,F.length-1),E=q(j,M,P);return q(T,E,P)}let O=/^\[([^=\]]*)([=]*)([^\]]*)\]$/.exec(F);if(O){let[M,E,w,$]=O;if(Array.isArray(T))switch(w){case"==":case"=":return T.find((W)=>W[E.trim()]==$.trim())}}let C=/^([^[\]]*)\[([^\]]*)]/.exec(F);if(C)return T[C[1]][C[2]];return T[F]},q=(T,F,j)=>{let P=void 0,O=void 0;F??="";let C=(E,w)=>{let $=w.pop();if($){let W=d(E,$,T,j);if(!W)throw new Error(`Unknown path element: "${$}" in "${F}"`);return P=E,O=$,C(W,w)}if(j?.set&&O)P[O]=j?.set;return E},M=C(T,y(F,j?.separator??".","{","}").reverse());switch(j?.format){case"list":return R(M,j?.key);case"object":return A(M,j?.key);case"json":return JSON.stringify(M);case"yaml":return N.stringify(M);default:return M}};var H=(T,F)=>{let j=/^(?<path>[^@>]*)?(?:@(?<from>[^>]*))?(?:>(?<format>[^|]*)(?:\|(?<key>.*))?)?$/.exec(T)?.groups;if(!j)throw new Error(`Invalid reference: "${T}"`);let{from:P,path:O,format:C,key:M}=j;if(!P)return q(F.root,O,{format:C,key:M});if(F.async===!1){if(!F.load)throw new Error("No load function defined");return q(F.load(P),O,{format:C,key:M})}else return(F.load??G)(P).then((E)=>q(E,O,{format:C,key:M}))};var SF=async(T,F)=>{let j=[];return await S(T,(P)=>{if(F([...P.path,P.key].join("."),P.value))j.push(P.value);return I.Continue()}),j};import b from"fs/promises";import k from"path";import m from"yaml";import g from"fs/promises";import J from"path";var f=(T)=>{return{include:(F)=>T.load(F.args[0]),file:(F)=>g.readFile(F.args[0]).then((j)=>j.toString()),env:(F)=>process.env[F.args[0]],ref:(F)=>H(F.args[0],{root:F.root,load:(j)=>T.load(j)})}},V=(T)=>{return{load:async(F)=>{for(let{file:j,alias:P}of Array.isArray(F.value)?F.value:[F.value]){if(!j)throw new Error("File reference required");await T.load(j,P??j,F.origin&&J.dirname(F.origin))}delete F.parent[F.key]},include:(F)=>T.load(F.value,void 0,F.origin&&J.dirname(F.origin)),from:async(F)=>{let j=await H(F.value,{root:F.root,load:(P)=>T.load(P,void 0,F.origin&&J.dirname(F.origin))});if(F.parent.content){if(Array.isArray(F.parent.content)&&Array.isArray(j))return[...j,...F.parent.content];return T.merge(j,F.parent.content),F.parent.content}return j},concat:async(F)=>{let j=[];for(let P of Array.isArray(F.value)?F.value:[F.value])j.push(...await H(P,{root:F.root,load:(O)=>T.load(O,void 0,F.origin&&J.dirname(F.origin))}));return j.filter((P)=>P!==void 0)},extends:async(F)=>{for(let j of Array.isArray(F.value)?F.value:[F.value])T.merge(await H(j,{root:F.root,load:(P)=>T.load(P,void 0,F.origin&&J.dirname(F.origin))}),F.parent);delete F.parent[F.key]},alias:(F)=>{T.alias(F.value,F.parent),delete F.parent[F.key]},if:(F)=>{if(!F.value)return null;delete F.parent[F.key]},output:(F)=>{delete F.parent[F.key];for(let j of Array.isArray(F.value)?F.value:[F.value])T.output(j,F.parent)},each:async(F)=>{delete F.parent[F.key];let j=Array.isArray(F.value)?F.value:await H(F.value,{root:F.root,load:(O)=>T.load(O,void 0,F.origin&&J.dirname(F.origin))}),P=[];if(!Array.isArray(j))throw new Error("source must be a list");for(let O=0;O<j.length;O++){let C=j[O],M=JSON.parse(JSON.stringify(F.parent)),E=[[/^\.(.*)/,{item:($)=>q(C,$.value)}]],w=[[/\$\((\w*)(?::([^)]*))?\)/g,{index:O,...C}]];F.options?.keys&&E.push(...F.options.keys),F.options?.sources&&w.push(...F.options.sources),console.log(M),await _(M,{...F.options,sources:w,keys:E,root:{...F.root,item:C,this:M}}),P.push(M)}return P}}};class p{options={load:{dirs:[]},merge:{},include:{},fields:{},includeKey:"includes",mergeKey:".merge"};cache={};includeDirectory(...T){return this.options.load.dirs??=[],this.options.load.dirs.push(...T),this}updateOrCreate(T,F,j){this.options.fields[F]??=[];let P=this.options.fields[F],O=P.find((C)=>C[0].source===T.source);if(!O)P.push([T,j]);else O[1]={...O[1],...j};return this}keys(T,F){return this.updateOrCreate(F??/^\.(.*)/,"keys",T)}sources(T,F){return this.updateOrCreate(F??/\$\(([^:)]*)(?::([^)]*))?\)/g,"sources",T)}output(T,F){return this.options.outputs??={},this.options.outputs[T]=F,this}defaultSources(T){return this.sources(f(this),T)}defaultKeys(T){return this.keys(V(this),T)}defaultProcessors(){return this.defaultKeys().defaultSources()}alias(T,F){return this.cache[T]=F,this}getAlias(T){return this.cache[T]}getOutputs(){return this.options.outputs??{}}async load(T,F,j){if(this.cache[T])return this.cache[T];let P=j?[j]:[];this.options.load.dirs&&P.push(...this.options.load.dirs);let O=await G(T,{...this.options.load,dirs:P});if(!O.content)return null;if(F)this.cache[F]=O.content;if(this.options.includeKey&&O.content[this.options.includeKey])await L(O.content[this.options.includeKey],{root:O.folderPath,loaded:(C)=>{if(this.options.mergeKey){if(C.content)Z(C.content,O.content,{mode:(M,E,w)=>w[this.options.mergeKey],...this.options.merge})}},loadOptions:this.options.load,...this.options.include}),delete O.content[this.options.includeKey];return await _(O.content,{...this.options.fields,origin:O.fullPath}),O.content}merge(T,F){Z(T,F,{mode:(j,P,O)=>O[this.options.mergeKey],...this.options.merge})}async writeOutputs(T){for(let[F,j]of Object.entries(this.getOutputs())){let P=k.join(T??".",F);try{switch(k.extname(F)){case".yml":case".yaml":await b.writeFile(P,m.stringify(j));break;case".json":await b.writeFile(P,JSON.stringify(j));break;default:await b.writeFile(P,j)}}catch{throw new Error(`Failed to write output: ${P}`)}}}}export{wF as toObjectWithKey,A as toObject,R as toList,H as select,L as processIncludes,_ as processFields,S as navigate,Z as merge,G as load,SF as find,p as Objector,I as NavigateResult,Y as NavigateAction};
|
package/dist/objector.d.ts
CHANGED
|
@@ -161,6 +161,7 @@ export declare interface ProcessFieldsOptions {
|
|
|
161
161
|
keys?: FieldProcessorMatcher[];
|
|
162
162
|
filters?: RegExp[];
|
|
163
163
|
origin?: string;
|
|
164
|
+
root?: any;
|
|
164
165
|
}
|
|
165
166
|
|
|
166
167
|
/**
|
|
@@ -176,22 +177,6 @@ export declare const select: (at: string, options: {
|
|
|
176
177
|
async?: boolean;
|
|
177
178
|
}) => any;
|
|
178
179
|
|
|
179
|
-
export declare interface SelectOptions {
|
|
180
|
-
set?: any;
|
|
181
|
-
format?: string;
|
|
182
|
-
key?: string;
|
|
183
|
-
separator?: string;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* @public
|
|
188
|
-
* @param from
|
|
189
|
-
* @param path
|
|
190
|
-
* @param set
|
|
191
|
-
* @returns
|
|
192
|
-
*/
|
|
193
|
-
export declare const selectPath: <T = any>(from: any, path: string, options?: SelectOptions) => T;
|
|
194
|
-
|
|
195
180
|
export declare const toList: <T>(from: any, key?: string) => T[];
|
|
196
181
|
|
|
197
182
|
export declare const toObject: <T>(from: any[], key?: string) => T;
|