@homedev/objector 1.2.38 → 1.2.40

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
@@ -339,7 +339,7 @@ export declare class Objector {
339
339
  loadObject<T = any>(data: T, container?: any, options?: LoadObjectOptions): Promise<T>;
340
340
  load<T = any>(fileName: string, cwd?: string, container?: any, noProcess?: boolean, ld?: LoadContext): Promise<T | null>;
341
341
  writeOutputs(options?: WriteOutputsOption, root?: any): Promise<void>;
342
- loadSubDocument(name: string, root?: any): Promise<any>;
342
+ loadSubDocument(name: string, root?: any, clone?: boolean): Promise<any>;
343
343
  }
344
344
 
345
345
  declare interface ObjectorConfig {
package/dist/index.js CHANGED
@@ -10,4 +10,4 @@ ${o}}`}else return String(e)},B=(e)=>{if(!e)return e;return e.replaceAll("\\n",`
10
10
  `).replaceAll("\\$","$").replaceAll("\\t","\t")};var Tr=()=>({substring:{types:{0:"ref",1:"number",2:"number"},fn:(e)=>e.args[0].substring(e.args[1],e.args[2])},repeat:{types:{0:"ref",1:"number"},fn:({args:[e,r]})=>e.repeat(r)},uuid:(e)=>crypto.randomUUID(),pad:{types:{0:"ref",1:"number",2:"string"},fn:({args:[e,r],tags:t})=>{let o=t.join?.[0]??`
11
11
  `,n=t.padChar?.[0]??" ";return d.padBlock(e,r??2,o,n)}},formatAs:{types:{1:["json","yaml","terraform"]},fn:({args:[e,r],tags:t})=>{switch(r){case"json":return JSON.stringify(e,null,2);case"yaml":return Bun.YAML.stringify(e,null,2);case"terraform":{let o=!!(t?.sort?.length||t?.sortKeys?.length);return F(e,0,{sortKeys:o})}}throw Error("Unsupported format: "+String(r))}},...Ir.makeFieldProcessorMap({pascal:d.pascal,camel:d.camel,capital:d.capital,fromYaml:Bun.YAML.parse,fromJson:JSON.parse,toYaml:(e,r)=>Bun.YAML.stringify(e,null,r??2),toJson:(e,r)=>JSON.stringify(e,null,r!==0?r??2:void 0),concat:(...e)=>e.join(""),len:(e)=>e.length,reverse:(e)=>e.split("").reverse().join(""),trim:(e)=>e.trim(),ltrim:(e)=>e.trimStart(),rtrim:(e)=>e.trimEnd(),lower:(e)=>e.toLowerCase(),upper:(e)=>e.toUpperCase(),snake:(e,r)=>d.changeCase(d.snake(e,"_"),r),kebab:(e,r)=>d.changeCase(d.snake(e,"-"),r),encode:(e,r)=>Buffer.from(e).toString(r??"base64"),decode:(e,r)=>Buffer.from(e,r??"base64").toString()})});var Kr=m(v(),1);var Gr={};C(Gr,{toObjectWithKey:()=>_r,toObject:()=>q,toList:()=>j,sortBy:()=>Sr,nonNullMap:()=>Ur,defined:()=>Jr,asyncMap:()=>Br});var j=(e,r="name")=>Object.entries(e).map(([t,o])=>({[r]:t,...o})),_r=(e,r="name")=>Object.fromEntries(e.map((t)=>[t[r],t])),q=(e,r="name")=>Object.fromEntries(e.map((t)=>{let{[r]:o,...n}=t;return[o,n]})),Ur=(e,r)=>e?.filter((t)=>t!==null&&t!==void 0).map(r)??[],Jr=(e,r="Value is undefined")=>{if(e===void 0)throw Error(r);return e},Sr=(e,r,t=(o,n)=>o-n)=>e.slice().sort((o,n)=>t(r(o),r(n))),Br=async(e,r)=>Promise.all(e.map(r));var ve=m(b(),1),Yr=m(S(),1);var Hr=()=>({...Kr.makeFieldProcessorMap({merge:(...e)=>e.flatMap((r)=>r)}),join:({args:[e],root:r,tags:t})=>{let o=t.key?.[0],n=t.separator?.[0]??t.sep?.[0],s=t.finalize?.length>0,i=B(n);if(typeof e==="string"){if(s&&e.length&&i)return e+i;return e}if(!Array.isArray(e))throw Error("Object is not a list");let a=((o?.length)?e.map((y)=>ve.select({...r,...y},o)):e).join(i),f=t.pad?.[0]??"0",c=t.padChar?.[0]??" ",p=s&&a.length&&i?a+i:a;return f?Yr.padBlock(p,parseInt(f),`
12
12
  `,c):p},list:({args:e})=>j(e[0],e[1]),object:({args:e})=>q(e[0],e[1]),range:({args:[e,r,t]})=>{let o=parseInt(e),n=parseInt(r),s=parseInt(t)||1;if(isNaN(o))throw Error("Invalid range: start value '"+String(e)+"' is not a number");if(isNaN(n))throw Error("Invalid range: end value '"+String(r)+"' is not a number");if(s===0)throw Error("Invalid range: step cannot be zero");if((n-o)/s<0)throw Error("Invalid range: step "+String(s)+" has wrong direction for range "+String(o)+" to "+String(n));return Array.from({length:Math.floor((n-o)/s)+1},(i,a)=>o+a*s)},filter:{types:(e,r,t)=>{switch(e){case 0:return"array";case 1:return Object.keys(A)}return U[t[1].value]?.(e-2,r,t.slice(2))},fn:({args:[e,r,t],root:o,tags:n})=>{if(e===null||e===void 0)throw Error("Filter source cannot be null or undefined");if(!Array.isArray(e))throw Error("Filter source must be an array");let s=n.key?.[0],i=A[r];if(s){let f=Array(e.length);for(let p=0;p<e.length;p++)try{f[p]=ve.select({...o,...e[p]},s)}catch{f[p]=Symbol("invalid")}let c=[];for(let p=0;p<e.length;p++){let y=f[p];if(typeof y!=="symbol"&&t===void 0?i(y):i(y,t))c.push(e[p])}return c}let a=[];for(let f of e)if(t===void 0?i(f):i(f,t))a.push(f);return a}}});var Qr=m(v(),1);var G=m(b(),1),xr=m(S(),1);var Zr=()=>({each:{processArgs:!1,fn:async({rawArgs:[e,r,...t],root:o,options:n,tags:s})=>{let i=async(g)=>g.quoted?B(g.value):await G.select(o,g.value),a=await G.select(o,e.value),f=await i(r),c=s.key?.[0],p=Array.isArray(a)?a:j(a),y=[],P=await Promise.all(t.map(async(g)=>await i(g))),O={...o,item:void 0,index:0,params:P,instance:void 0,parent:o.this,tags:s},Y={...n,root:O};for(let g=0;g<p.length;g++){let N=h(f),L={instance:N},ee=typeof f==="string"?L:N;if(O.item=p[g],O.index=g,O.instance=N,s?.root?.[0])Object.assign(O,await G.select(O,s.root[0]));if(await Qr.processFields(ee,Y),c!==void 0)y.push(await G.select(L.instance,c));else y.push(L.instance)}if(s.join?.length){if(y.length===0)return"";let g=B(s.join[0]),N=s.pad?.[0]??"0",L=s.padChar?.[0]??" ",ee=s.finalize?.length&&g?g:"",he=`${y.join(g)}${ee}`;return N?xr.padBlock(he,parseInt(N),`
13
- `,L):he}return y}}});var Xr=m(b(),1),K=async(e,r,t)=>e.rawArgs[r].quoted?e.rawArgs[r].value:await Xr.select(e.root,e.rawArgs[r].value,{defaultValue:t}),Vr=()=>({switch:{processArgs:!1,fn:async(e)=>{let r=await K(e,0,!1),t=await K(e,1);if(t[r]===void 0){if(e.rawArgs.length>2)return K(e,2);throw Error(`Unhandled switch case: ${r}`)}return t[r]}}});var Fr=m(v(),1),qr=()=>Fr.makeFieldProcessorMap({log:console.log});var zr=m(v(),1);var et=()=>({if:{types:{0:"boolean"},fn:(e)=>e.args[0]?e.args[1]:e.args[2]},check:{types:{0:Object.keys(A)},fn:(e)=>A[e.args[0]](...e.args.slice(1))},...zr.makeFieldProcessorMap(A,{types:U})});var rt=m(w(),1);var tt=(e)=>({default:{processArgs:!1,fn:async(r)=>{for(let t=0;t<r.rawArgs.length;t++)if(r.rawArgs[t]?.value!==void 0){let n=await K(r,t,null);if(n!==null)return n}if(r.tags.nullable?.length)return rt.NavigateResult.DeleteItem();if(r.tags.fail?.length)throw Error(`No valid value found for default (${r.rawArgs.map((t)=>t.value).join(", ")})`);return""}},subDocument:async(r)=>e.loadSubDocument(r.args[0],r.options.root)});var D=m(w(),1),ot=()=>({convert:({args:[e,r,t]})=>{switch(r){case"string":return String(e);case"number":{let o=Number(e);if(!isNaN(o))return D.NavigateResult.ReplaceItem(o);break}case"boolean":if(e==="true"||e===!0||e===1||e==="1"||e==="yes"||e==="on")return D.NavigateResult.ReplaceItem(!0);else if(e==="false"||e===!1||e===0||e==="0"||e==="no"||e==="off")return D.NavigateResult.ReplaceItem(!1);break;default:throw Error(`Unsupported type for convert: ${r}`)}if(t!==void 0)return D.NavigateResult.ReplaceItem(t);throw Error(`Failed to convert value to ${r}`)},isType:({args:[e,r]})=>{let o=(()=>{switch(r){case"string":return typeof e==="string";case"number":return typeof e==="number";case"boolean":return typeof e==="boolean";case"object":return e!==null&&typeof e==="object"&&!Array.isArray(e);case"array":return Array.isArray(e);case"null":return e===null;case"undefined":return e===void 0;case"function":return typeof e==="function";default:throw Error(`Unknown type for isType: ${r}`)}})();return D.NavigateResult.ReplaceItem(o)},typeOf:({args:[e]})=>{if(e===null)return"null";else if(Array.isArray(e))return"array";else return typeof e}});var nt=(e)=>e.keys(tr(e)).keys(nr(e)).keys(yr()).keys(Or()).keys(dr()).keys(vr()).keys(kr(e)).keys(Pr(e)).sources(Cr(e)).sources(Er()).sources(Tr()).sources(ot()).sources(Hr()).sources(Zr()).sources(Vr()).sources(qr()).sources(et()).sources(tt(e));var l={};C(l,{writeOutputs:()=>V,writeOutput:()=>ye,writeFormat:()=>ce,toObjectWithKey:()=>_r,toObject:()=>q,toList:()=>j,sortBy:()=>Sr,nonNullMap:()=>Ur,mergeAll:()=>_,merge:()=>M,locate:()=>E,loadFormat:()=>Z,importList:()=>ge,importGlob:()=>qe,globMap:()=>X,exists:()=>T,defined:()=>Jr,deepClone:()=>h,conditionPredicates:()=>A,asyncMap:()=>Br});u(l,m(v(),1));u(l,m(w(),1));u(l,m(b(),1));u(l,m(H(),1));u(l,m(S(),1));var gs=l;class to{currentConfig={};includeDirectories=[];includes=[];outputs=[];vars={};cache={};fields={sources:{},keys:{}};objectMetadata={};subDocuments={};currentContext=null;constructor(){this.use(nt)}use(e){return e(this),this}config(e){return Object.assign(this.currentConfig,e),this}getConfig(){return this.currentConfig}includeDirectory(...e){return this.includeDirectories.push(...e),this}getIncludeDirectories(){return this.includeDirectories}include(...e){return this.includes.push(...e),this}keys(e){return Object.assign(this.fields.keys,{...this.fields.keys,...e}),this}sources(e){return Object.assign(this.fields.sources,{...this.fields.sources,...e}),this}variables(e){return this.vars={...this.vars,...e},this}getOutputs(){return this.outputs}output(e){return this.outputs.push(e),this}metadata(e,r){return this.objectMetadata[e]=r,this}getMetadata(e){return this.objectMetadata[e]}getAllMetadata(){return this.objectMetadata}subDocument(e,r){return this.subDocuments[e]=r,this}getSubDocument(e){return this.subDocuments[e]}getCurrentContext(){return this.currentContext}filter(e){return this.fields.filters??=[],this.fields.filters?.push(e),this}async docIncludes(e,r){let t=[];if(e.content.includes)t.push(...e.content.includes),delete e.content.includes;t.push(...this.includes),this.includes.length=0;let o=[];for(let n of t)if(n.includes("*")){let s=new Bun.Glob(n);for await(let i of s.scan({cwd:e.folderPath,absolute:!0}))if(!this.currentContext?.included.includes(i))o.push(i)}else if(n.endsWith("/")){let s=z.isAbsolute(n)?n:z.join(e.folderPath,n);this.includeDirectories.push(s)}else o.push(n);for(let n of o)M(await this.load(n,e.folderPath,{parent:e.content,...r},!0),e.content)}async loadObject(e,r,t){return await st.processFields(e,{...this.fields,globalContext:{file:t?.fullPath??"@internal"},root:{...r,...this.fields.root,...this.vars,context:this.currentContext,$document:{root:{content:e},fileName:t?.fileName??"@internal",folder:t?.folderPath??"@internal",fullPath:t?.fullPath??"@internal"}}}),e}async load(e,r,t,o,n){let s=r?[r]:[];if(n)this.currentContext=n;this.currentContext??={included:[],document:null},s.push(...this.includeDirectories.map((y)=>z.isAbsolute(y)?y:z.join(r??process.cwd(),y)));let i=await Z(e,{dirs:s});if(this.currentContext.included.push(i.fullPath),!i.content)return null;let a=i.fullPath,f=this.cache[a]??i.content,c=h(f);i.content=c,await this.docIncludes(i,t);let p;if(o!==!0)p=await this.loadObject(i.content,t,{fileName:i.fullPath,folderPath:i.folderPath,fullPath:i.fullPath});else p=i.content;return this.currentContext.document=p,this.cache[a]??=f,p}async writeOutputs(e,r){for(let t of Object.entries(this.subDocuments))if(t[0].startsWith("output."))this.output(await this.loadSubDocument(t[0],r));await V(this.getOutputs(),e)}async loadSubDocument(e,r){r??=this.currentContext?.document;let t=this.getSubDocument(e);if(t===void 0)throw Error(`Sub document ${e} not found`);let o=await this.loadObject(h(t),r);if(r)r[e]=o;return o}}export{gs as default,to as Objector};
13
+ `,L):he}return y}}});var Xr=m(b(),1),K=async(e,r,t)=>e.rawArgs[r].quoted?e.rawArgs[r].value:await Xr.select(e.root,e.rawArgs[r].value,{defaultValue:t}),Vr=()=>({switch:{processArgs:!1,fn:async(e)=>{let r=await K(e,0,!1),t=await K(e,1);if(t[r]===void 0){if(e.rawArgs.length>2)return K(e,2);throw Error(`Unhandled switch case: ${r}`)}return t[r]}}});var Fr=m(v(),1),qr=()=>Fr.makeFieldProcessorMap({log:console.log});var zr=m(v(),1);var et=()=>({if:{types:{0:"boolean"},fn:(e)=>e.args[0]?e.args[1]:e.args[2]},check:{types:{0:Object.keys(A)},fn:(e)=>A[e.args[0]](...e.args.slice(1))},...zr.makeFieldProcessorMap(A,{types:U})});var rt=m(w(),1);var tt=(e)=>({default:{processArgs:!1,fn:async(r)=>{for(let t=0;t<r.rawArgs.length;t++)if(r.rawArgs[t]?.value!==void 0){let n=await K(r,t,null);if(n!==null)return n}if(r.tags.nullable?.length)return rt.NavigateResult.DeleteItem();if(r.tags.fail?.length)throw Error(`No valid value found for default (${r.rawArgs.map((t)=>t.value).join(", ")})`);return""}},subDocument:async(r)=>e.loadSubDocument(r.args[0],r.options.root)});var D=m(w(),1),ot=()=>({convert:({args:[e,r,t]})=>{switch(r){case"string":return String(e);case"number":{let o=Number(e);if(!isNaN(o))return D.NavigateResult.ReplaceItem(o);break}case"boolean":if(e==="true"||e===!0||e===1||e==="1"||e==="yes"||e==="on")return D.NavigateResult.ReplaceItem(!0);else if(e==="false"||e===!1||e===0||e==="0"||e==="no"||e==="off")return D.NavigateResult.ReplaceItem(!1);break;default:throw Error(`Unsupported type for convert: ${r}`)}if(t!==void 0)return D.NavigateResult.ReplaceItem(t);throw Error(`Failed to convert value to ${r}`)},isType:({args:[e,r]})=>{let o=(()=>{switch(r){case"string":return typeof e==="string";case"number":return typeof e==="number";case"boolean":return typeof e==="boolean";case"object":return e!==null&&typeof e==="object"&&!Array.isArray(e);case"array":return Array.isArray(e);case"null":return e===null;case"undefined":return e===void 0;case"function":return typeof e==="function";default:throw Error(`Unknown type for isType: ${r}`)}})();return D.NavigateResult.ReplaceItem(o)},typeOf:({args:[e]})=>{if(e===null)return"null";else if(Array.isArray(e))return"array";else return typeof e}});var nt=(e)=>e.keys(tr(e)).keys(nr(e)).keys(yr()).keys(Or()).keys(dr()).keys(vr()).keys(kr(e)).keys(Pr(e)).sources(Cr(e)).sources(Er()).sources(Tr()).sources(ot()).sources(Hr()).sources(Zr()).sources(Vr()).sources(qr()).sources(et()).sources(tt(e));var l={};C(l,{writeOutputs:()=>V,writeOutput:()=>ye,writeFormat:()=>ce,toObjectWithKey:()=>_r,toObject:()=>q,toList:()=>j,sortBy:()=>Sr,nonNullMap:()=>Ur,mergeAll:()=>_,merge:()=>M,locate:()=>E,loadFormat:()=>Z,importList:()=>ge,importGlob:()=>qe,globMap:()=>X,exists:()=>T,defined:()=>Jr,deepClone:()=>h,conditionPredicates:()=>A,asyncMap:()=>Br});u(l,m(v(),1));u(l,m(w(),1));u(l,m(b(),1));u(l,m(H(),1));u(l,m(S(),1));var gs=l;class to{currentConfig={};includeDirectories=[];includes=[];outputs=[];vars={};cache={};fields={sources:{},keys:{}};objectMetadata={};subDocuments={};currentContext=null;constructor(){this.use(nt)}use(e){return e(this),this}config(e){return Object.assign(this.currentConfig,e),this}getConfig(){return this.currentConfig}includeDirectory(...e){return this.includeDirectories.push(...e),this}getIncludeDirectories(){return this.includeDirectories}include(...e){return this.includes.push(...e),this}keys(e){return Object.assign(this.fields.keys,{...this.fields.keys,...e}),this}sources(e){return Object.assign(this.fields.sources,{...this.fields.sources,...e}),this}variables(e){return this.vars={...this.vars,...e},this}getOutputs(){return this.outputs}output(e){return this.outputs.push(e),this}metadata(e,r){return this.objectMetadata[e]=r,this}getMetadata(e){return this.objectMetadata[e]}getAllMetadata(){return this.objectMetadata}subDocument(e,r){return this.subDocuments[e]=r,this}getSubDocument(e){return this.subDocuments[e]}getCurrentContext(){return this.currentContext}filter(e){return this.fields.filters??=[],this.fields.filters?.push(e),this}async docIncludes(e,r){let t=[];if(e.content.includes)t.push(...e.content.includes),delete e.content.includes;t.push(...this.includes),this.includes.length=0;let o=[];for(let n of t)if(n.includes("*")){let s=new Bun.Glob(n);for await(let i of s.scan({cwd:e.folderPath,absolute:!0}))if(!this.currentContext?.included.includes(i))o.push(i)}else if(n.endsWith("/")){let s=z.isAbsolute(n)?n:z.join(e.folderPath,n);this.includeDirectories.push(s)}else o.push(n);for(let n of o)M(await this.load(n,e.folderPath,{parent:e.content,...r},!0),e.content)}async loadObject(e,r,t){return await st.processFields(e,{...this.fields,globalContext:{file:t?.fullPath??"@internal"},root:{...r,...this.fields.root,...this.vars,context:this.currentContext,$document:{root:{content:e},fileName:t?.fileName??"@internal",folder:t?.folderPath??"@internal",fullPath:t?.fullPath??"@internal"}}}),e}async load(e,r,t,o,n){let s=r?[r]:[];if(n)this.currentContext=n;this.currentContext??={included:[],document:null},s.push(...this.includeDirectories.map((y)=>z.isAbsolute(y)?y:z.join(r??process.cwd(),y)));let i=await Z(e,{dirs:s});if(this.currentContext.included.push(i.fullPath),!i.content)return null;let a=i.fullPath,f=this.cache[a]??i.content,c=h(f);i.content=c,await this.docIncludes(i,t);let p;if(o!==!0)p=await this.loadObject(i.content,t,{fileName:i.fullPath,folderPath:i.folderPath,fullPath:i.fullPath});else p=i.content;return this.currentContext.document=p,this.cache[a]??=f,p}async writeOutputs(e,r){for(let t of Object.entries(this.subDocuments))if(t[0].startsWith("output."))this.output(await this.loadSubDocument(t[0],r));await V(this.getOutputs(),e)}async loadSubDocument(e,r,t){r??=this.currentContext?.document;let o=this.getSubDocument(e);if(o===void 0)throw Error(`Sub document ${e} not found`);let n=await this.loadObject(t?h(o):o,r);if(r)r[e]=n;return n}}export{gs as default,to as Objector};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homedev/objector",
3
- "version": "1.2.38",
3
+ "version": "1.2.40",
4
4
  "description": "object extensions for YAML/JSON",
5
5
  "author": "julzor",
6
6
  "license": "ISC",