@homedev/objector 0.0.1 → 0.0.3

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 CHANGED
@@ -1 +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};
1
+ import G from"path";import E from"fs/promises";import U from"path";import q from"yaml";var B=async(m)=>{try{return await E.access(m,E.constants.R_OK),!0}catch{return!1}},w=async(m,d)=>{let F=await(async()=>{let n=[""];if(!U.isAbsolute(m)&&d?.dirs)n.push(...d.dirs);for(let j of n){let s=U.join(j,m);if(await B(s))return U.resolve(s)}throw new Error(`File not found: "${m}"`)})(),r=await E.readFile(F),P=U.dirname(F),M={"[.](yml|yaml)$":q.parse,"[.]json$":JSON.parse,...d?.parsers},C=Object.entries(M).find((n)=>F.match(n[0]));if(!C)throw new Error(`Unhandled file type: ${m}`);return{content:C[1](r.toString()),fullPath:F,folderPath:P}};var Y=async(m,d)=>{for(let f of m){let F=d?.root?G.join(d.root,f):f,r=await w(F,d?.loadOptions);if(d?.loaded)await d.loaded(r)}};var H=(m,d,f)=>{for(let F of Object.keys(d)){if(m[F]===void 0)continue;let r=d[F],P=m[F];if(typeof P!==typeof r||Array.isArray(P)!==Array.isArray(r)){let C=f?.mismatch?.(F,P,r);if(C!==void 0){d[F]=C;continue}throw new Error(`Type mismatch: ${F}`)}let M=f?.mode?.(F,P,r)??"merge";switch(M){case"source":d[F]=P;continue;case"target":continue;case"skip":delete d[F],delete m[F];continue;case"merge":break;default:throw new Error(`Unknown merge mode: ${M}`)}if(typeof P==="object")if(Array.isArray(P))r.push(...P);else{if(f?.navigate?.(F,P,r)===!1)continue;H(P,r,f)}else{if(P===r)continue;let C=f?.conflict?.(F,P,r);d[F]=C===void 0?P:C}}for(let F of Object.keys(m))if(d[F]===void 0)d[F]=m[F]};var J;((r)=>{r[r.Continue=0]="Continue";r[r.Skip=1]="Skip";r[r.ReplaceItem=2]="ReplaceItem";r[r.ReplaceParent=3]="ReplaceParent"})(J||={});class O{action;by;constructor(m,d){this.action=m;this.by=d}static ReplaceParent(m){return new O(3,m)}static ReplaceItem(m){return new O(2,m)}static Skip(){return new O(1)}static Continue(){return new O(0)}}var y=async(m,d)=>{let f=async(r)=>{for(let[P,M]of Object.entries(r.value)){let C=await F({key:P,value:M,path:r.path,parent:r.value});switch(C.action){case 3:if(!r.key)throw new Error("Cannot remplace root");r.parent[r.key]=C.by;return;case 2:r.value[P]=C.by;break;case 1:return}}},F=async(r)=>{let P=r.key?await d(r):{action:0};if(P.action===0){if(r.value&&typeof r.value==="object"){if(r.key)r.path.push(r.key);if(await f(r),r.key)r.path.pop()}}return P};await F({key:null,value:m,path:[],parent:null})};var $=async(m,d,f,F,r)=>{for(let P of r?.functions??[]){let M=P[0].exec(f.value);if(M){let[,C,...T]=M;if(P[1][C])return O.ReplaceItem(await P[1][C]({args:T,path:d,value:m.value,root:F}))}}};var I=async(m,d,f,F)=>{for(let r of F?.keys??[]){let P=r[0].exec(m.key);if(P){let[,M,...C]=P;if(r[1][M])return O.ReplaceParent(await r[1][M]({args:C,path:d,value:m.value,root:f}))}}};var K=(m,d,f,F)=>{for(let r of F?.sources??[])d.value=d.value.replaceAll(r[0],(P,M,...C)=>r[1][M]?.({args:C,path:m,value:d.value,root:f})??P)};var _=(m,d)=>{for(let[f,F]of d?.variables??[])m.value=m.value.replaceAll(f,(r,P)=>F[P]??r)};var a=async(m,d)=>y(m,async(f)=>{let F=[...f.path,f.key].join(".");if(d?.filters){if(!d.filters.some((P)=>P.test(F)))return O.Continue()}let r=await I(f,F,m,d);if(r)return r;if(typeof f.value==="string"){let P={value:f.value};return K(F,P,m,d),_(P,d),await $(f,F,P,m,d)??O.ReplaceItem(P.value)}return O.Continue()});var rr=(m,d,f)=>{let F=void 0,r=void 0,P=(M,C)=>{let T=C.pop();if(T){let n=M[T];if(!n)throw new Error(`Unknown path element: "${T}" in "${d}"`);return F=M,r=T,P(n,C)}if(f&&r)F[r]=f;return M};return P(m,d.split(".").reverse())};var Pr=async(m,d)=>{let f=[];return await y(m,(F)=>{if(d([...F.path,F.key].join("."),F.value))f.push(F.value);return O.Continue()}),f};export{rr as select,Y as processIncludes,a as processFields,y as navigate,H as merge,w as load,Pr as find,O as NavigateResult,J as NavigateAction};
@@ -0,0 +1,162 @@
1
+ /**
2
+ * @public
3
+ */
4
+ export declare interface DataFile<T = any> {
5
+ content: T;
6
+ fullPath: string;
7
+ folderPath: string;
8
+ }
9
+
10
+ export declare interface FieldContext {
11
+ args: string[];
12
+ path: string;
13
+ value: string;
14
+ root: any;
15
+ }
16
+
17
+ export declare type FieldProcessorAsyncMap = Record<string, (context: FieldContext) => MaybePromise<any>>;
18
+
19
+ export declare type FieldProcessorMap = Record<string, (context: FieldContext) => any>;
20
+
21
+ export declare type FieldProcessorMatcher = [matcher: RegExp, handlers: FieldProcessorMap];
22
+
23
+ export declare type FieldProcessorMatcherAsync = [matcher: RegExp, handlers: FieldProcessorAsyncMap];
24
+
25
+ export declare type FieldVariableMatcher = [matcher: RegExp, variables: Record<string, any>];
26
+
27
+ /**
28
+ * @public
29
+ * @param from
30
+ * @param cb
31
+ * @returns
32
+ */
33
+ export declare const find: (from: any, cb: (path: string, value: string) => any) => Promise<any[]>;
34
+
35
+ /**
36
+ * @public
37
+ */
38
+ export declare interface IncludeOptions {
39
+ root?: string;
40
+ loaded?: (data: DataFile) => Promise<void> | void;
41
+ loadOptions?: LoadOptions;
42
+ }
43
+
44
+ /**
45
+ * @public
46
+ * @param fileName
47
+ * @param options
48
+ * @returns
49
+ */
50
+ export declare const load: <T = any>(fileName: string, options?: LoadOptions) => Promise<DataFile<T>>;
51
+
52
+ /**
53
+ * @public
54
+ */
55
+ export declare interface LoadOptions {
56
+ dirs?: string[];
57
+ parsers?: Record<string, (data: string) => any>;
58
+ }
59
+
60
+ declare type MaybePromise<T> = T | Promise<T>;
61
+
62
+ /**
63
+ * @public
64
+ * @param source
65
+ * @param target
66
+ * @param options
67
+ */
68
+ export declare const merge: (source: any, target: any, options?: MergeOptions) => void;
69
+
70
+ /**
71
+ * @public
72
+ */
73
+ export declare type MergeMode = 'merge' | 'source' | 'target' | 'skip';
74
+
75
+ /**
76
+ * @public
77
+ */
78
+ export declare interface MergeOptions {
79
+ conflict?: (key: string, source: any, target: any) => any;
80
+ mismatch?: (key: string, source: any, target: any) => any;
81
+ navigate?: (key: string, source: any, target: any) => boolean | undefined;
82
+ mode?: (key: string, source: any, target: any) => MergeMode | void;
83
+ }
84
+
85
+ /**
86
+ * @public
87
+ * @param o
88
+ * @param cb
89
+ */
90
+ export declare const navigate: (o: any, cb: NavigateCallback) => Promise<void>;
91
+
92
+ /**
93
+ * @public
94
+ */
95
+ export declare enum NavigateAction {
96
+ Continue = 0,
97
+ Skip = 1,
98
+ ReplaceItem = 2,
99
+ ReplaceParent = 3
100
+ }
101
+
102
+ /**
103
+ * @public
104
+ */
105
+ export declare type NavigateCallback = (context: NavigateContext) => Promise<NavigateResult> | NavigateResult;
106
+
107
+ /**
108
+ * @public
109
+ */
110
+ export declare interface NavigateContext {
111
+ key: string;
112
+ value: any;
113
+ path: string[];
114
+ parent: any;
115
+ }
116
+
117
+ /**
118
+ * @public
119
+ */
120
+ export declare class NavigateResult {
121
+ readonly action: NavigateAction;
122
+ readonly by?: any | undefined;
123
+ constructor(action: NavigateAction, by?: any | undefined);
124
+ static ReplaceParent(by: any): NavigateResult;
125
+ static ReplaceItem(by: any): NavigateResult;
126
+ static Skip(): NavigateResult;
127
+ static Continue(): NavigateResult;
128
+ }
129
+
130
+ /**
131
+ * @public
132
+ * @param root
133
+ * @param options
134
+ * @returns
135
+ */
136
+ export declare const processFields: (root: any, options?: ProcessFieldsOptions) => Promise<void>;
137
+
138
+ export declare interface ProcessFieldsOptions {
139
+ sources?: FieldProcessorMatcher[];
140
+ functions?: FieldProcessorMatcherAsync[];
141
+ keys?: FieldProcessorMatcherAsync[];
142
+ variables?: FieldVariableMatcher[];
143
+ filters?: RegExp[];
144
+ }
145
+
146
+ /**
147
+ * @public
148
+ * @param includes
149
+ * @param options
150
+ */
151
+ export declare const processIncludes: (includes: string[], options?: IncludeOptions) => Promise<void>;
152
+
153
+ /**
154
+ * @public
155
+ * @param from
156
+ * @param path
157
+ * @param set
158
+ * @returns
159
+ */
160
+ export declare const select: <T = any>(from: any, path: string, set?: any) => T;
161
+
162
+ export { }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@homedev/objector",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "object extensions for YAML/JSON",
5
5
  "author": "julzor",
6
6
  "license": "ISC",
7
7
  "main": "dist/index.js",
8
8
  "type": "module",
9
- "typings": "dist/index.d.ts",
9
+ "typings": "dist/objector.d.ts",
10
10
  "peerDependencies": {
11
11
  "yaml": "^2.7.0"
12
12
  }
package/dist/auto.d.ts DELETED
@@ -1,10 +0,0 @@
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 DELETED
@@ -1 +0,0 @@
1
- export declare const find: (from: any, cb: (path: string, value: string) => any) => Promise<any[]>;
package/dist/include.d.ts DELETED
@@ -1,7 +0,0 @@
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>;
package/dist/index.d.ts DELETED
@@ -1,8 +0,0 @@
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/load.d.ts DELETED
@@ -1,10 +0,0 @@
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>>;
package/dist/merge.d.ts DELETED
@@ -1,8 +0,0 @@
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;
@@ -1,23 +0,0 @@
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>;
@@ -1,14 +0,0 @@
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 {};
package/dist/select.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare const select: <T = any>(from: any, path: string, set?: any) => T;