@vorplex/core 0.0.45 → 0.0.47
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 +1 -1
- package/dist/index.js +1 -1
- package/dist/modules/path-selector/path-selector.util.d.ts +4 -0
- package/dist/modules/path-selector/path-selector.util.js +64 -0
- package/dist/modules/tson/schemas/array.js +10 -10
- package/dist/modules/value/value.util.js +5 -5
- package/package.json +1 -1
- package/dist/functions/parse-path-selector.function.d.ts +0 -2
- package/dist/functions/parse-path-selector.function.js +0 -17
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,6 @@ export * from './consts/unit.const';
|
|
|
3
3
|
export * from './functions/debounce.function';
|
|
4
4
|
export * from './functions/is-node-environment.function';
|
|
5
5
|
export * from './functions/ok.function';
|
|
6
|
-
export * from './functions/parse-path-selector.function';
|
|
7
6
|
export * from './functions/parse-url.function';
|
|
8
7
|
export * from './interfaces/group.interface';
|
|
9
8
|
export * from './interfaces/key-value.interface';
|
|
@@ -41,6 +40,7 @@ export * from './modules/math/size';
|
|
|
41
40
|
export * from './modules/number/number.util';
|
|
42
41
|
export * from './modules/object/object.util';
|
|
43
42
|
export * from './modules/path/path.util';
|
|
43
|
+
export * from './modules/path-selector/path-selector.util';
|
|
44
44
|
export * from './modules/random/random.util';
|
|
45
45
|
export * from './modules/readable-stream/readable-stream.util';
|
|
46
46
|
export * from './modules/reflection/interfaces/parameter.interface';
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,6 @@ export * from './consts/unit.const';
|
|
|
4
4
|
export * from './functions/debounce.function';
|
|
5
5
|
export * from './functions/is-node-environment.function';
|
|
6
6
|
export * from './functions/ok.function';
|
|
7
|
-
export * from './functions/parse-path-selector.function';
|
|
8
7
|
export * from './functions/parse-url.function';
|
|
9
8
|
export * from './interfaces/group.interface';
|
|
10
9
|
export * from './interfaces/key-value.interface';
|
|
@@ -42,6 +41,7 @@ export * from './modules/math/size';
|
|
|
42
41
|
export * from './modules/number/number.util';
|
|
43
42
|
export * from './modules/object/object.util';
|
|
44
43
|
export * from './modules/path/path.util';
|
|
44
|
+
export * from './modules/path-selector/path-selector.util';
|
|
45
45
|
export * from './modules/random/random.util';
|
|
46
46
|
export * from './modules/readable-stream/readable-stream.util';
|
|
47
47
|
export * from './modules/reflection/interfaces/parameter.interface';
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export class $PathSelector {
|
|
2
|
+
static parse(selector) {
|
|
3
|
+
if (typeof selector === 'function') {
|
|
4
|
+
const path = [];
|
|
5
|
+
const proxy = new Proxy({}, {
|
|
6
|
+
get: (target, key) => {
|
|
7
|
+
if (typeof key === 'string')
|
|
8
|
+
path.push(key);
|
|
9
|
+
return proxy;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
selector(proxy);
|
|
13
|
+
return path;
|
|
14
|
+
}
|
|
15
|
+
const segments = [];
|
|
16
|
+
let i = 0;
|
|
17
|
+
const length = selector.length;
|
|
18
|
+
function readToken(endDelimiters) {
|
|
19
|
+
let token = '';
|
|
20
|
+
while (i < length) {
|
|
21
|
+
if (selector[i] === '\\' && i + 1 < length) {
|
|
22
|
+
i++;
|
|
23
|
+
token += selector[i++];
|
|
24
|
+
}
|
|
25
|
+
else if (endDelimiters.includes(selector[i])) {
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
token += selector[i++];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return token;
|
|
33
|
+
}
|
|
34
|
+
while (i < length) {
|
|
35
|
+
if (selector[i] === '.') {
|
|
36
|
+
i++;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (selector[i] === '[') {
|
|
40
|
+
i++;
|
|
41
|
+
const inner = readToken([']']);
|
|
42
|
+
if (i < length && selector[i] === ']')
|
|
43
|
+
i++;
|
|
44
|
+
if (inner !== '')
|
|
45
|
+
segments.push(inner);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const token = readToken(['.', '[']);
|
|
49
|
+
if (token !== '')
|
|
50
|
+
segments.push(token);
|
|
51
|
+
}
|
|
52
|
+
return segments;
|
|
53
|
+
}
|
|
54
|
+
static sanitize(...segments) {
|
|
55
|
+
return segments
|
|
56
|
+
.map(s => s
|
|
57
|
+
.replace(/\\/g, `\\\\`) // escape \
|
|
58
|
+
.replace(/\./g, `\\.`) // escape .
|
|
59
|
+
.replace(/\[/g, `\\[`) // escape [
|
|
60
|
+
.replace(/\]/g, `\\]`) // escape ]
|
|
61
|
+
)
|
|
62
|
+
.join('.');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -63,16 +63,16 @@ export class TsonArray extends TsonSchemaBase {
|
|
|
63
63
|
throw new TsonError(`Array min length of ${this.definition.min} expected`, value, this);
|
|
64
64
|
if (this.definition.max != null && value.length > this.definition.max)
|
|
65
65
|
throw new TsonError(`Array max length of ${this.definition.max} expected`, value, this);
|
|
66
|
-
if (this.definition.itemDefinition)
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
throw
|
|
75
|
-
}
|
|
66
|
+
if (!this.definition.itemDefinition)
|
|
67
|
+
return [...value];
|
|
68
|
+
for (const [index, item] of value.entries()) {
|
|
69
|
+
try {
|
|
70
|
+
result[index] = $Tson.parse(this.definition.itemDefinition).parse(item);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (!(error instanceof TsonError))
|
|
74
|
+
throw error;
|
|
75
|
+
throw new TsonError(`Invalid item in array. ${error.message}`, item, this, `[${index}]${error.path}`);
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
return result;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { $PathSelector } from '../path-selector/path-selector.util';
|
|
2
2
|
import { State } from '../state/state.model';
|
|
3
3
|
import { $String } from '../string/string.util';
|
|
4
4
|
export class $Value {
|
|
@@ -12,7 +12,7 @@ export class $Value {
|
|
|
12
12
|
return !isNaN(parseInt(value));
|
|
13
13
|
}
|
|
14
14
|
static update(target, pathSelector, update) {
|
|
15
|
-
const path =
|
|
15
|
+
const path = $PathSelector.parse(pathSelector);
|
|
16
16
|
const isUnassigned = (value) => value == null || typeof value !== 'object';
|
|
17
17
|
const updateAtPath = (value, pathIndex) => {
|
|
18
18
|
if (pathIndex === path.length)
|
|
@@ -38,7 +38,7 @@ export class $Value {
|
|
|
38
38
|
static set(target, path, value) {
|
|
39
39
|
if ($String.isNullOrEmpty(path))
|
|
40
40
|
return value;
|
|
41
|
-
const selectors = typeof path === 'string' ?
|
|
41
|
+
const selectors = typeof path === 'string' ? $PathSelector.parse(path) : $PathSelector.parse(path);
|
|
42
42
|
const isUnassigned = (value) => value == null || typeof value !== 'object';
|
|
43
43
|
if (isUnassigned(target))
|
|
44
44
|
target = ($Value.isNumeric(selectors[0]) ? [] : {});
|
|
@@ -55,7 +55,7 @@ export class $Value {
|
|
|
55
55
|
return target;
|
|
56
56
|
}
|
|
57
57
|
static unset(target, path) {
|
|
58
|
-
const selectors =
|
|
58
|
+
const selectors = $PathSelector.parse(path);
|
|
59
59
|
for (const [index, selector] of selectors.entries()) {
|
|
60
60
|
if (index < selectors.length - 1) {
|
|
61
61
|
target = target?.[selector];
|
|
@@ -72,7 +72,7 @@ export class $Value {
|
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
static get(value, path) {
|
|
75
|
-
const selectors =
|
|
75
|
+
const selectors = $PathSelector.parse(path);
|
|
76
76
|
for (const selector of selectors)
|
|
77
77
|
value = value?.[selector];
|
|
78
78
|
return value;
|
package/package.json
CHANGED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { $String } from '../modules/string/string.util';
|
|
2
|
-
export function parsePath(path) {
|
|
3
|
-
return $String.isNullOrEmpty(path) ? []
|
|
4
|
-
: Array.from(path.matchAll(new RegExp(`(?<property>[^.[\\]]+)|\\[(['"\`])(?<index>.*?)\\2\\]`, 'g')))
|
|
5
|
-
.map(match => match.groups.property ?? match.groups.index);
|
|
6
|
-
}
|
|
7
|
-
export function parsePathSelector(selector) {
|
|
8
|
-
const path = [];
|
|
9
|
-
const proxy = new Proxy({}, {
|
|
10
|
-
get: (target, key) => {
|
|
11
|
-
path.push(String(key));
|
|
12
|
-
return proxy;
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
selector(proxy);
|
|
16
|
-
return path;
|
|
17
|
-
}
|