@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 +1 -1
- package/dist/objector.d.ts +162 -0
- package/package.json +2 -2
- package/dist/auto.d.ts +0 -10
- package/dist/find.d.ts +0 -1
- package/dist/include.d.ts +0 -7
- package/dist/index.d.ts +0 -8
- package/dist/load.d.ts +0 -10
- package/dist/merge.d.ts +0 -8
- package/dist/navigate.d.ts +0 -23
- package/dist/processFields.d.ts +0 -14
- package/dist/select.d.ts +0 -1
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
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.
|
|
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/
|
|
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
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;
|
package/dist/navigate.d.ts
DELETED
|
@@ -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>;
|
package/dist/processFields.d.ts
DELETED
|
@@ -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;
|