@vorplex/core 0.0.44 → 0.0.46

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 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,4 @@
1
+ export declare class $PathSelector {
2
+ static parse<T>(selector: string | ((value: T) => any)): string[];
3
+ static sanitize(...segments: string[]): string;
4
+ }
@@ -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
+ }
@@ -1,4 +1,4 @@
1
- import { parsePath, parsePathSelector } from '../../functions/parse-path-selector.function';
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 = parsePathSelector(pathSelector);
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' ? parsePath(path) : parsePathSelector(path);
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 = parsePath(path);
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 = parsePath(path);
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@vorplex/core",
3
- "version": "0.0.44",
3
+ "version": "0.0.46",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "files": [
@@ -1,2 +0,0 @@
1
- export declare function parsePath(path: string): string[];
2
- export declare function parsePathSelector<T>(selector: (value: T) => any): string[];
@@ -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
- }