@homedev/objector 0.0.1

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/auto.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { MergeOptions, IncludeOptions, LoadOptions, ProcessFieldsOptions } from '.';
2
+ export interface AutoLoadOptions {
3
+ includeKey?: string;
4
+ mergeKey?: string;
5
+ merge?: MergeOptions;
6
+ load?: LoadOptions;
7
+ include?: IncludeOptions;
8
+ fields?: ProcessFieldsOptions;
9
+ }
10
+ export declare const autoLoad: (fileName: string, options?: AutoLoadOptions) => Promise<import("./load").DataFile<any>>;
package/dist/find.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const find: (from: any, cb: (path: string, value: string) => any) => Promise<any[]>;
@@ -0,0 +1,7 @@
1
+ import { DataFile, LoadOptions } from './load';
2
+ export interface IncludeOptions {
3
+ root?: string;
4
+ loaded?: (data: DataFile) => Promise<void> | void;
5
+ loadOptions?: LoadOptions;
6
+ }
7
+ export declare const processIncludes: (includes: string[], options?: IncludeOptions) => Promise<void>;
@@ -0,0 +1,8 @@
1
+ export * from './load';
2
+ export * from './include';
3
+ export * from './merge';
4
+ export * from './auto';
5
+ export * from './navigate';
6
+ export * from './processFields';
7
+ export * from './select';
8
+ export * from './find';
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ import m from"fs/promises";import d from"path";import P from"yaml";var f=async(a,n)=>{let t=await(async()=>{let l=[""];if(!d.isAbsolute(a)&&n?.dirs)l.push(...n.dirs);for(let w of l){let g=d.join(w,a);if(await m.exists(g))return d.resolve(g)}throw new Error(`File not found: "${a}"`)})(),e=await m.readFile(t),i=d.dirname(t),o={"[.](yml|yaml)$":P.parse,"[.]json$":JSON.parse,...n?.parsers},s=Object.entries(o).find((l)=>t.match(l[0]));if(!s)throw new Error(`Unhandled file type: ${a}`);return{content:s[1](e.toString()),fullPath:t,folderPath:i}};import O from"path";var v=async(a,n)=>{for(let r of a){let t=n?.root?O.join(n.root,r):r,e=await f(t,n?.loadOptions);if(n?.loaded)await n.loaded(e)}};var y=(a,n,r)=>{for(let t of Object.keys(n)){if(a[t]===void 0)continue;let e=n[t],i=a[t];if(typeof i!==typeof e||Array.isArray(i)!==Array.isArray(e)){let s=r?.mismatch?.(t,i,e);if(s!==void 0){n[t]=s;continue}throw new Error(`Type mismatch: ${t}`)}let o=r?.mode?.(t,i,e)??"merge";switch(o){case"source":n[t]=i;continue;case"target":continue;case"skip":delete n[t],delete a[t];continue;case"merge":break;default:throw new Error(`Unknown merge mode: ${o}`)}if(typeof i==="object")if(Array.isArray(i))e.push(...i);else{if(r?.navigate?.(t,i,e)===!1)continue;y(i,e,r)}else{if(i===e)continue;let s=r?.conflict?.(t,i,e);n[t]=s===void 0?i:s}}for(let t of Object.keys(a))if(n[t]===void 0)n[t]=a[t]};var D=async(a,n)=>{let r=await f(a,n?.load),t=n?.includeKey??"includes";return await v(r.content[t]??[],{root:r.folderPath,loaded:(e)=>{y(e.content,r.content,{mode:(i,o,s)=>s[n?.mergeKey??".merge"],...n?.merge})},loadOptions:n?.load,...n?.include}),delete r.content[t],await R(r.content,n?.fields),r};var x;((e)=>{e[e.Continue=0]="Continue";e[e.Skip=1]="Skip";e[e.ReplaceItem=2]="ReplaceItem";e[e.ReplaceParent=3]="ReplaceParent"})(x||={});class c{action;by;constructor(a,n){this.action=a;this.by=n}static ReplaceParent(a){return new c(3,a)}static ReplaceItem(a){return new c(2,a)}static Skip(){return new c(1)}static Continue(){return new c(0)}}var u=async(a,n)=>{let r=async(e)=>{for(let[i,o]of Object.entries(e.value)){let s=await t({key:i,value:o,path:e.path,parent:e.value});switch(s.action){case 3:if(!e.key)throw new Error("Cannot remplace root");e.parent[e.key]=s.by;return;case 2:e.value[i]=s.by;break;case 1:return}}},t=async(e)=>{let i=e.key?await n(e):{action:0};if(i.action===0){if(typeof e.value==="object"){if(e.key)e.path.push(e.key);if(await r(e),e.key)e.path.pop()}}return i};await t({key:null,value:a,path:[],parent:null})};var h=(a,n,r)=>{let t=void 0,e=void 0,i=(o,s)=>{let p=s.pop();if(p){let l=o[p];if(!l)throw new Error(`Unknown path element: "${p}" in "${n}"`);return t=o,e=p,i(l,s)}if(r&&e)t[e]=r;return o};return i(a,n.split(".").reverse())};var q={env:(a)=>process.env[a],ref:(a,n,r,t)=>h(t,a)},R=async(a,n)=>{await u(a,async(r)=>{let t=[...r.path,r.key].join(".");if(n?.filters){if(!n.filters.some((e)=>e.test(t)))return c.Continue()}if(n?.keys){let e=n.keys[r.key];if(e)return c.ReplaceParent(await e(r.value,t,a))}if(typeof r.value==="string"){let e=r.value;if(n?.sources)e=e.replaceAll(n.sourceReg??/\$\((\w*):([^)]*)\)/g,(i,o,s)=>{if(n.sources?.[o])return n.sources[o](s,t,e,a);return i});if(n?.vars)e=e.replaceAll(n.varReg??/\$\(([^)]*)\)/g,(i,o)=>{return n.vars?.[o]??i});if(n?.funcs){let i=(n.funcReg??/^\$\((\w*):([^)]*)\)/).exec(e);if(i){let[,o,s]=i;if(n.funcs[o])return c.ReplaceItem(await n.funcs[o](s,t,r.value,a))}}return c.ReplaceItem(e)}return c.Continue()})};var H=async(a,n)=>{let r=[];return await u(a,(t)=>{if(n([...t.path,t.key].join("."),t.value))r.push(t.value);return c.Continue()}),r};export{h as select,v as processIncludes,R as processFields,u as navigate,y as merge,f as load,H as find,q as defaultSources,D as autoLoad,c as NavigateResult,x as NavigateAction};
package/dist/load.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export interface DataFile<T = any> {
2
+ content: T;
3
+ fullPath: string;
4
+ folderPath: string;
5
+ }
6
+ export interface LoadOptions {
7
+ dirs?: string[];
8
+ parsers?: Record<string, (data: string) => any>;
9
+ }
10
+ export declare const load: <T = any>(fileName: string, options?: LoadOptions) => Promise<DataFile<T>>;
@@ -0,0 +1,8 @@
1
+ export type MergeMode = 'merge' | 'source' | 'target' | 'skip';
2
+ export interface MergeOptions {
3
+ conflict?: (key: string, source: any, target: any) => any;
4
+ mismatch?: (key: string, source: any, target: any) => any;
5
+ navigate?: (key: string, source: any, target: any) => boolean | undefined;
6
+ mode?: (key: string, source: any, target: any) => MergeMode | void;
7
+ }
8
+ export declare const merge: (source: any, target: any, options?: MergeOptions) => void;
@@ -0,0 +1,23 @@
1
+ export interface NavigateContext {
2
+ key: string;
3
+ value: any;
4
+ path: string[];
5
+ parent: any;
6
+ }
7
+ export declare enum NavigateAction {
8
+ Continue = 0,
9
+ Skip = 1,
10
+ ReplaceItem = 2,
11
+ ReplaceParent = 3
12
+ }
13
+ export declare class NavigateResult {
14
+ readonly action: NavigateAction;
15
+ readonly by?: any | undefined;
16
+ constructor(action: NavigateAction, by?: any | undefined);
17
+ static ReplaceParent(by: any): NavigateResult;
18
+ static ReplaceItem(by: any): NavigateResult;
19
+ static Skip(): NavigateResult;
20
+ static Continue(): NavigateResult;
21
+ }
22
+ export type NavigateCallback = (context: NavigateContext) => Promise<NavigateResult> | NavigateResult;
23
+ export declare const navigate: (o: any, cb: NavigateCallback) => Promise<void>;
@@ -0,0 +1,14 @@
1
+ type FieldSourceMap = Record<string, (arg: string, path: string, value: string, root: any) => any>;
2
+ export interface ProcessFieldsOptions {
3
+ sources?: FieldSourceMap;
4
+ sourceReg?: RegExp;
5
+ funcs?: Record<string, (arg: string, path: string, value: string, root: any) => Promise<any>>;
6
+ funcReg?: RegExp;
7
+ vars?: Record<string, any>;
8
+ varReg?: RegExp;
9
+ keys?: Record<string, (value: any, path: string, root: any) => Promise<any>>;
10
+ filters?: RegExp[];
11
+ }
12
+ export declare const defaultSources: FieldSourceMap;
13
+ export declare const processFields: (root: any, options?: ProcessFieldsOptions) => Promise<void>;
14
+ export {};
@@ -0,0 +1 @@
1
+ export declare const select: <T = any>(from: any, path: string, set?: any) => T;
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@homedev/objector",
3
+ "version": "0.0.1",
4
+ "description": "object extensions for YAML/JSON",
5
+ "author": "julzor",
6
+ "license": "ISC",
7
+ "main": "dist/index.js",
8
+ "type": "module",
9
+ "typings": "dist/index.d.ts",
10
+ "peerDependencies": {
11
+ "yaml": "^2.7.0"
12
+ }
13
+ }