@thisisagile/easy 14.2.12 → 15.0.0
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/utils/View.d.ts +17 -19
- package/dist/utils/View.js +33 -31
- package/dist/utils/View.js.map +1 -1
- package/package.json +2 -2
- package/src/utils/View.ts +72 -95
package/dist/utils/View.d.ts
CHANGED
|
@@ -1,21 +1,17 @@
|
|
|
1
|
-
import { Constructor, DontInfer, Json, List, PageList } from '../types';
|
|
1
|
+
import { Constructor, DontInfer, Json, List, PageList, Primitive } from '../types';
|
|
2
2
|
type Func<T = unknown> = (a: any, key?: string) => T;
|
|
3
|
-
export type InOut = {
|
|
4
|
-
in?: Func | View<any>;
|
|
5
|
-
col?: string;
|
|
6
|
-
};
|
|
7
|
-
type Views<V = Json> = Partial<Record<keyof V, string | Func | InOut | number | boolean | undefined>>;
|
|
8
3
|
type Viewer = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
f: Func;
|
|
12
|
-
};
|
|
4
|
+
key: string;
|
|
5
|
+
f: Func;
|
|
13
6
|
};
|
|
7
|
+
type ViewType = Primitive | Constructor | Func | View | undefined;
|
|
8
|
+
type ViewRecord<V = Json> = Partial<Record<keyof V, ViewType>>;
|
|
9
|
+
export declare const toViewer: (key: string, value: ViewType) => Viewer;
|
|
14
10
|
export declare class View<V = Json> {
|
|
15
11
|
private views;
|
|
16
12
|
readonly startsFrom: 'scratch' | 'source';
|
|
17
13
|
readonly viewers: Viewer[];
|
|
18
|
-
constructor(views?:
|
|
14
|
+
constructor(views?: Partial<Record<keyof V, ViewType>>, startsFrom?: 'scratch' | 'source', viewers?: Viewer[]);
|
|
19
15
|
get fromSource(): View<V>;
|
|
20
16
|
from<T = unknown>(source: PageList<T>): PageList<V>;
|
|
21
17
|
from<T = unknown>(source: List<T>): List<V>;
|
|
@@ -24,15 +20,17 @@ export declare class View<V = Json> {
|
|
|
24
20
|
same: (one?: unknown, another?: unknown) => boolean;
|
|
25
21
|
private reduce;
|
|
26
22
|
}
|
|
27
|
-
export declare const
|
|
28
|
-
export declare const view: <V = Json>(views: Partial<Record<keyof DontInfer<V>,
|
|
23
|
+
export declare const isSimpleView: (a: unknown) => a is View<Json>;
|
|
24
|
+
export declare const view: <V = Json>(views: Partial<Record<keyof DontInfer<V>, ViewType>>) => View<V>;
|
|
29
25
|
export declare const views: {
|
|
30
|
-
ignore:
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
keepOr: (alt?: unknown) => (a: unknown, key?: string) => unknown;
|
|
34
|
-
or: (key: string, alt?: string) => (a: unknown) => {};
|
|
26
|
+
ignore: symbol;
|
|
27
|
+
keep: symbol;
|
|
28
|
+
skip: symbol;
|
|
35
29
|
value: (value: unknown) => () => unknown;
|
|
36
|
-
|
|
30
|
+
or: {
|
|
31
|
+
key: (altKey: string) => (a: unknown, key?: string) => unknown;
|
|
32
|
+
value: (altValue: unknown) => (a: unknown, key?: string) => unknown;
|
|
33
|
+
func: (altFunc: Func) => (a: unknown, key?: string) => unknown;
|
|
34
|
+
};
|
|
37
35
|
};
|
|
38
36
|
export {};
|
package/dist/utils/View.js
CHANGED
|
@@ -1,58 +1,60 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.views = exports.view = exports.
|
|
3
|
+
exports.views = exports.view = exports.isSimpleView = exports.View = exports.toViewer = void 0;
|
|
4
4
|
const types_1 = require("../types");
|
|
5
5
|
const Traverse_1 = require("./Traverse");
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const isColAndFunction = (v) => (0, types_1.isObject)(v) && (0, types_1.isDefined)(v.col) && (0, types_1.isFunction)(v.in);
|
|
9
|
-
const isColAndView = (v) => (0, types_1.isObject)(v) && (0, types_1.isDefined)(v.col) && v.in instanceof View;
|
|
10
|
-
const toFunc = (a, col, f = a => a) => (0, types_1.tryTo)((0, Traverse_1.traverse)(a, col)).map(v => ((0, types_1.isArray)(v) ? () => v.map(i => f(i, col)) : (a) => f((0, Traverse_1.traverse)(a, col)))).value;
|
|
6
|
+
const ignore = Symbol('view.ignore');
|
|
7
|
+
const keep = Symbol('view.keep');
|
|
11
8
|
const toViewer = (key, value) => (0, types_1.choose)(value)
|
|
12
|
-
.is.not.defined(v => v,
|
|
13
|
-
.type(types_1.isBoolean, b =>
|
|
14
|
-
.
|
|
15
|
-
.
|
|
16
|
-
.
|
|
17
|
-
.
|
|
18
|
-
.type(
|
|
19
|
-
.type(
|
|
20
|
-
|
|
21
|
-
.
|
|
22
|
-
|
|
23
|
-
.
|
|
24
|
-
.
|
|
9
|
+
.is.not.defined(v => v, { key, f: () => undefined })
|
|
10
|
+
.type(types_1.isBoolean, b => ({ key, f: () => b }))
|
|
11
|
+
.equals(ignore, { key, f: () => undefined })
|
|
12
|
+
.equals(0, { key, f: () => undefined })
|
|
13
|
+
.equals(keep, { key, f: (a) => (0, Traverse_1.traverse)(a, key) })
|
|
14
|
+
.equals(1, { key, f: (a) => (0, Traverse_1.traverse)(a, key) })
|
|
15
|
+
.type(types_1.isString, s => ({ key, f: (a) => (0, Traverse_1.traverse)(a, s) }))
|
|
16
|
+
.type(types_1.isConstructor, c => ({
|
|
17
|
+
key,
|
|
18
|
+
f: (a, key) => (0, types_1.use)((0, Traverse_1.traverse)(a, key), v => (0, types_1.isArray)(v) ? v.map(i => new c(i)) : new c(v))
|
|
19
|
+
}))
|
|
20
|
+
.type(exports.isSimpleView, f => ({ key, f: (a) => f.from((0, Traverse_1.traverse)(a, key)) }))
|
|
21
|
+
.type(types_1.isFunction, f => ({ key, f }))
|
|
22
|
+
.else(v => ({ key, f: () => v }));
|
|
23
|
+
exports.toViewer = toViewer;
|
|
24
|
+
const toViewers = (views) => (0, types_1.meta)(views).entries().map(([k, v]) => (0, exports.toViewer)(k, v));
|
|
25
25
|
class View {
|
|
26
26
|
constructor(views = {}, startsFrom = 'scratch', viewers = toViewers(views)) {
|
|
27
27
|
this.views = views;
|
|
28
28
|
this.startsFrom = startsFrom;
|
|
29
29
|
this.viewers = viewers;
|
|
30
30
|
this.same = (one, another) => (0, types_1.isEqual)(this.from(one), this.from(another));
|
|
31
|
-
this.reduce = (
|
|
31
|
+
this.reduce = (source) => (0, types_1.use)((0, types_1.asJson)(source), src => this.viewers.reduce((acc, v) => types_1.json.set(acc, v.key, v.f(src, v.key)), this.startsFrom === 'scratch' ? {} : src));
|
|
32
32
|
}
|
|
33
33
|
get fromSource() {
|
|
34
34
|
return new View(this.views, 'source', this.viewers);
|
|
35
35
|
}
|
|
36
36
|
from(source) {
|
|
37
37
|
if ((0, types_1.isPageList)(source))
|
|
38
|
-
return (0, types_1.toPageList)(source.map(s => this.reduce(
|
|
38
|
+
return (0, types_1.toPageList)(source.map(s => this.reduce(s)), source);
|
|
39
39
|
if ((0, types_1.isArray)(source))
|
|
40
|
-
return source.map(s => this.reduce(
|
|
41
|
-
return this.reduce(
|
|
40
|
+
return source.map(s => this.reduce(s));
|
|
41
|
+
return this.reduce(source);
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
exports.View = View;
|
|
45
|
-
const
|
|
46
|
-
exports.
|
|
45
|
+
const isSimpleView = (a) => a instanceof View;
|
|
46
|
+
exports.isSimpleView = isSimpleView;
|
|
47
47
|
const view = (views) => new View(views);
|
|
48
48
|
exports.view = view;
|
|
49
49
|
exports.views = {
|
|
50
|
-
ignore
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
keepOr: (alt) => (a, key) => (0, Traverse_1.traverse)(a, key) ?? alt,
|
|
54
|
-
or: (key, alt = '') => (a) => (0, Traverse_1.traverse)(a, key) ?? alt,
|
|
50
|
+
ignore,
|
|
51
|
+
keep,
|
|
52
|
+
skip: ignore,
|
|
55
53
|
value: (value) => () => value,
|
|
56
|
-
|
|
54
|
+
or: {
|
|
55
|
+
key: (altKey) => (a, key) => (0, Traverse_1.traverse)(a, key) ?? (0, Traverse_1.traverse)(a, altKey),
|
|
56
|
+
value: (altValue) => (a, key) => (0, Traverse_1.traverse)(a, key) ?? altValue,
|
|
57
|
+
func: (altFunc) => (a, key) => (0, Traverse_1.traverse)(a, key) ?? altFunc(a, key),
|
|
58
|
+
},
|
|
57
59
|
};
|
|
58
60
|
//# sourceMappingURL=View.js.map
|
package/dist/utils/View.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"View.js","sourceRoot":"","sources":["../../src/utils/View.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"View.js","sourceRoot":"","sources":["../../src/utils/View.ts"],"names":[],"mappings":";;;AAAA,oCAoBkB;AAClB,yCAAoC;AAQpC,MAAM,MAAM,GAAG,MAAM,CAAC,aAAa,CAAC,CAAC;AACrC,MAAM,IAAI,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;AAE1B,MAAM,QAAQ,GAAG,CAAC,GAAW,EAAE,KAAe,EAAU,EAAE,CAC7D,IAAA,cAAM,EAAC,KAAK,CAAC;KACR,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,SAAS,EAAW,CAAC;KAC3D,IAAI,CAAC,iBAAS,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,EAAC,CAAC,CAAC;KACzC,MAAM,CAAC,MAAM,EAAE,EAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,SAAS,EAAC,CAAC;KACzC,MAAM,CAAC,CAAC,EAAE,EAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,SAAS,EAAC,CAAC;KACpC,MAAM,CAAC,IAAI,EAAE,EAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAM,EAAE,EAAE,CAAC,IAAA,mBAAQ,EAAC,CAAC,EAAE,GAAG,CAAC,EAAC,CAAC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CAAC,EAAE,GAAG,CAAC,IAAI,OAAO,CAAC,CAAC,EAAE,GAAG,CAAC;KAC7F;CACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thisisagile/easy",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "15.0.0",
|
|
4
4
|
"description": "Straightforward library for building domain-driven microservice architectures",
|
|
5
5
|
"author": "Sander Hoogendoorn",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"access": "public"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@thisisagile/easy-test": "
|
|
34
|
+
"@thisisagile/easy-test": "15.0.0",
|
|
35
35
|
"@types/form-urlencoded": "^4.4.0",
|
|
36
36
|
"@types/jsonwebtoken": "^8.5.9",
|
|
37
37
|
"@types/luxon": "3.2.0",
|
package/src/utils/View.ts
CHANGED
|
@@ -1,114 +1,91 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
tryTo,
|
|
2
|
+
asJson,
|
|
3
|
+
choose,
|
|
4
|
+
Constructor,
|
|
5
|
+
DontInfer,
|
|
6
|
+
isArray,
|
|
7
|
+
isBoolean,
|
|
8
|
+
isConstructor,
|
|
9
|
+
isEqual,
|
|
10
|
+
isFunction,
|
|
11
|
+
isPageList,
|
|
12
|
+
isString,
|
|
13
|
+
json,
|
|
14
|
+
Json,
|
|
15
|
+
List,
|
|
16
|
+
meta,
|
|
17
|
+
PageList,
|
|
18
|
+
Primitive,
|
|
19
|
+
toPageList,
|
|
20
|
+
use,
|
|
22
21
|
} from '../types';
|
|
23
|
-
import {
|
|
22
|
+
import {traverse} from './Traverse';
|
|
24
23
|
|
|
25
24
|
type Func<T = unknown> = (a: any, key?: string) => T;
|
|
25
|
+
type Viewer = { key: string; f: Func };
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
type ViewType = Primitive | Constructor | Func | View | undefined;
|
|
28
|
+
type ViewRecord<V = Json> = Partial<Record<keyof V, ViewType>>;
|
|
28
29
|
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
const isColAndFunction = (
|
|
32
|
-
v: unknown
|
|
33
|
-
): v is {
|
|
34
|
-
col: string;
|
|
35
|
-
in: Func;
|
|
36
|
-
} => isObject(v) && isDefined(v.col) && isFunction(v.in);
|
|
37
|
-
const isColAndView = (
|
|
38
|
-
v: unknown
|
|
39
|
-
): v is {
|
|
40
|
-
col: string;
|
|
41
|
-
in: View;
|
|
42
|
-
} => isObject(v) && isDefined(v.col) && v.in instanceof View;
|
|
30
|
+
const ignore = Symbol('view.ignore');
|
|
31
|
+
const keep = Symbol('view.keep');
|
|
43
32
|
|
|
44
|
-
|
|
45
|
-
|
|
33
|
+
export const toViewer = (key: string, value: ViewType): Viewer =>
|
|
34
|
+
choose(value)
|
|
35
|
+
.is.not.defined(v => v, {key, f: () => undefined} as Viewer)
|
|
36
|
+
.type(isBoolean, b => ({key, f: () => b}))
|
|
37
|
+
.equals(ignore, {key, f: () => undefined})
|
|
38
|
+
.equals(0, {key, f: () => undefined})
|
|
39
|
+
.equals(keep, {key, f: (a: any) => traverse(a, key)})
|
|
40
|
+
.equals(1, {key, f: (a: any) => traverse(a, key)})
|
|
41
|
+
.type(isString, s => ({key, f: (a: any) => traverse(a, s)}))
|
|
42
|
+
.type(isConstructor, c => ({
|
|
43
|
+
key,
|
|
44
|
+
f: (a, key) => use(traverse(a, key), v => isArray(v) ? v.map(i => new c(i)) : new c(v))
|
|
45
|
+
}))
|
|
46
|
+
.type(isSimpleView, f => ({key, f: (a: any) => f.from(traverse(a, key))}))
|
|
47
|
+
.type(isFunction, f => ({key, f}))
|
|
48
|
+
.else(v => ({key, f: () => v}));
|
|
46
49
|
|
|
47
|
-
const
|
|
48
|
-
tryTo(traverse(a, col)).map(v => (isArray(v) ? () => v.map(i => f(i, col)) : (a: any) => f(traverse(a, col)))).value;
|
|
49
|
-
|
|
50
|
-
const toViewer = (key: string, value: unknown): Viewer =>
|
|
51
|
-
choose(value)
|
|
52
|
-
.is.not.defined(
|
|
53
|
-
v => v,
|
|
54
|
-
() => toViewer(key, () => undefined)
|
|
55
|
-
)
|
|
56
|
-
.type(isBoolean, b => toViewer(key, () => b))
|
|
57
|
-
.type(isNumber, n => toViewer(key, () => n))
|
|
58
|
-
.type(isString, s => toViewer(key, (a: any) => toFunc(a, s)(a)))
|
|
59
|
-
.type(isColOnly, io => toViewer(key, io.col))
|
|
60
|
-
.type(isFunction, f => toViewer(key, { in: { key, f } }))
|
|
61
|
-
.type(isInOnly, io => toViewer(key, { in: { key, f: io.in } }))
|
|
62
|
-
.type(isColAndFunction, io => toViewer(key, { in: { key, f: (a: any) => toFunc(a, io.col, io.in)(a) } }))
|
|
63
|
-
.type(isColAndView, io => toViewer(key, { in: { key, f: (a: any) => io.in.from(traverse(a, io.col)) } }))
|
|
64
|
-
.else(v => v as Viewer);
|
|
65
|
-
|
|
66
|
-
const toViewers = (views: Views): Viewer[] =>
|
|
67
|
-
meta(views)
|
|
68
|
-
.entries()
|
|
69
|
-
.map(([k, v]) => toViewer(k, v));
|
|
50
|
+
const toViewers = (views: ViewRecord): Viewer[] => meta(views).entries<ViewType>().map(([k, v]) => toViewer(k, v));
|
|
70
51
|
|
|
71
52
|
export class View<V = Json> {
|
|
72
|
-
|
|
53
|
+
constructor(private views = {} as ViewRecord<V>, readonly startsFrom: 'scratch' | 'source' = 'scratch', readonly viewers: Viewer[] = toViewers(views)) {
|
|
54
|
+
}
|
|
73
55
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
56
|
+
get fromSource(): View<V> {
|
|
57
|
+
return new View(this.views, 'source', this.viewers);
|
|
58
|
+
}
|
|
77
59
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
);
|
|
88
|
-
if (isArray(source)) return source.map(s => this.reduce(asJson(s)));
|
|
89
|
-
return this.reduce(asJson(source));
|
|
90
|
-
}
|
|
60
|
+
from<T = unknown>(source: PageList<T>): PageList<V>;
|
|
61
|
+
from<T = unknown>(source: List<T>): List<V>;
|
|
62
|
+
from<T = unknown>(source: T[]): V[];
|
|
63
|
+
from<T = unknown>(source: T): V;
|
|
64
|
+
from<T = unknown>(source: PageList<T> | List<T> | T[] | T): PageList<V> | List<V> | V[] | V {
|
|
65
|
+
if (isPageList(source)) return toPageList(source.map(s => this.reduce(s)), source);
|
|
66
|
+
if (isArray(source)) return source.map(s => this.reduce(s));
|
|
67
|
+
return this.reduce(source);
|
|
68
|
+
}
|
|
91
69
|
|
|
92
|
-
|
|
70
|
+
same = (one?: unknown, another?: unknown): boolean => isEqual(this.from(one), this.from(another));
|
|
93
71
|
|
|
94
|
-
|
|
72
|
+
private reduce = (source: any): any => use(asJson(source), src => this.viewers.reduce((acc, v) => json.set(acc, v.key, v.f(src, v.key)), this.startsFrom === 'scratch' ? {} : src));
|
|
95
73
|
}
|
|
96
74
|
|
|
97
|
-
export const
|
|
98
|
-
|
|
75
|
+
export const isSimpleView = (a: unknown): a is View => a instanceof View;
|
|
76
|
+
|
|
77
|
+
export const view = <V = Json>(views: ViewRecord<DontInfer<V>>): View<V> => new View<V>(views);
|
|
99
78
|
|
|
100
79
|
export const views = {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
to:
|
|
111
|
-
<T>(ctor: Constructor<T>) =>
|
|
112
|
-
(a: unknown, key?: string) =>
|
|
113
|
-
new ctor(traverse(a, key)),
|
|
80
|
+
ignore,
|
|
81
|
+
keep,
|
|
82
|
+
skip: ignore,
|
|
83
|
+
value: (value: unknown) => () => value,
|
|
84
|
+
or: {
|
|
85
|
+
key: (altKey: string) => (a: unknown, key?: string) => traverse(a, key) ?? traverse(a, altKey),
|
|
86
|
+
value: (altValue: unknown) => (a: unknown, key?: string) => traverse(a, key) ?? altValue,
|
|
87
|
+
func: (altFunc: Func) => (a: unknown, key?: string) => traverse(a, key) ?? altFunc(a, key),
|
|
88
|
+
},
|
|
114
89
|
};
|
|
90
|
+
|
|
91
|
+
// spread: (a: any, key: string) => ({...a, ...(use(traverse(a, key), v => isObject(v) ? v : ({[key]: v})))}),
|