property-accessor 2.1.2 → 2.3.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/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
|
6
6
|
and this project adheres to [Semantic Versioning](http://semver.org/).
|
|
7
7
|
|
|
8
|
+
## [2.3.0] - 2025-05-20
|
|
9
|
+
|
|
10
|
+
### Fixed
|
|
11
|
+
|
|
12
|
+
- Decided to don't deal with float-like keys, since it is not possible to clarify parsing strategy for some cases
|
|
13
|
+
|
|
14
|
+
## [2.2.0] - 2025-05-19
|
|
15
|
+
|
|
16
|
+
### Fixed
|
|
17
|
+
|
|
18
|
+
- Fixed parsing keys when using floating numbers [final cut]
|
|
19
|
+
|
|
8
20
|
## [2.1.2] - 2025-05-19
|
|
9
21
|
|
|
10
22
|
### Fixed
|
|
@@ -2,10 +2,9 @@ export declare class PropertyAccessor {
|
|
|
2
2
|
protected target: any;
|
|
3
3
|
constructor(target: any);
|
|
4
4
|
get(key: string): any;
|
|
5
|
-
set(key: string, value: any):
|
|
5
|
+
set(key: string, value: any): any;
|
|
6
6
|
flat(): any;
|
|
7
7
|
static get(path: string, src: any): any;
|
|
8
8
|
static set(path: string, value: any, src: any): boolean;
|
|
9
9
|
static flat(target: any): any;
|
|
10
|
-
static parsePath(path: string): string[];
|
|
11
10
|
}
|
|
@@ -26,33 +26,46 @@ class PropertyAccessor {
|
|
|
26
26
|
if (!(0, _helpers_1.validatePath)(path)) {
|
|
27
27
|
return;
|
|
28
28
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (acc === undefined || acc === null)
|
|
29
|
+
return path.split('.').reduce((acc, part) => {
|
|
30
|
+
if (acc === undefined || acc === null) {
|
|
32
31
|
return;
|
|
33
|
-
|
|
32
|
+
}
|
|
33
|
+
const parts = part.split(/[\[\]]/).filter(Boolean);
|
|
34
|
+
for (const part of parts) {
|
|
35
|
+
acc = acc?.[part];
|
|
36
|
+
if (acc === undefined) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
34
40
|
return acc;
|
|
35
41
|
}, src);
|
|
36
42
|
}
|
|
37
43
|
static set(path, value, src) {
|
|
38
|
-
if (!src
|
|
44
|
+
if (!src) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (!path) {
|
|
39
48
|
return false;
|
|
40
49
|
}
|
|
41
|
-
|
|
50
|
+
if (!(0, _helpers_1.validatePath)(path)) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
const keys = path.split('.');
|
|
42
54
|
let target = src;
|
|
43
55
|
for (let i = 0; i < keys.length; i++) {
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
56
|
+
const parts = keys[i].split(/[\[\]]/).filter(Boolean);
|
|
57
|
+
for (let j = 0; j < parts.length; j++) {
|
|
58
|
+
const key = parts[j];
|
|
59
|
+
const isLastKey = i === keys.length - 1 && j === parts.length - 1;
|
|
60
|
+
if (isLastKey) {
|
|
61
|
+
target[key] = value;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
if (!target[key] || typeof target[key] !== 'object') {
|
|
65
|
+
target[key] = /^\d+$/.test(parts[j + 1] || '') ? [] : {};
|
|
66
|
+
}
|
|
67
|
+
target = target[key];
|
|
54
68
|
}
|
|
55
|
-
target = target[key];
|
|
56
69
|
}
|
|
57
70
|
}
|
|
58
71
|
return true;
|
|
@@ -62,19 +75,6 @@ class PropertyAccessor {
|
|
|
62
75
|
(0, _helpers_1.flatKeyHelper)(target, flat);
|
|
63
76
|
return flat;
|
|
64
77
|
}
|
|
65
|
-
static parsePath(path) {
|
|
66
|
-
if ((!path.includes('.') && !path.includes('[')) ||
|
|
67
|
-
(!path.includes('[') && /^\d+\.\d+$/.test(path))) {
|
|
68
|
-
return [path];
|
|
69
|
-
}
|
|
70
|
-
const parts = [];
|
|
71
|
-
const regex = /[^.[\]]+/g;
|
|
72
|
-
let match;
|
|
73
|
-
while ((match = regex.exec(path)) !== null) {
|
|
74
|
-
parts.push(match[0]);
|
|
75
|
-
}
|
|
76
|
-
return parts;
|
|
77
|
-
}
|
|
78
78
|
}
|
|
79
79
|
exports.PropertyAccessor = PropertyAccessor;
|
|
80
80
|
//# sourceMappingURL=property-accessor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"property-accessor.js","sourceRoot":"","sources":["../../src/library/property-accessor.ts"],"names":[],"mappings":";;;AAAA,uCAAuD;AAEvD,MAAa,gBAAgB;IACjB,MAAM,CAAM;IAEtB,YAAY,MAAW;QACrB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;
|
|
1
|
+
{"version":3,"file":"property-accessor.js","sourceRoot":"","sources":["../../src/library/property-accessor.ts"],"names":[],"mappings":";;;AAAA,uCAAuD;AAEvD,MAAa,gBAAgB;IACjB,MAAM,CAAM;IAEtB,YAAY,MAAW;QACrB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IACM,GAAG,CAAC,GAAW;QACpB,OAAO,gBAAgB,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IAChD,CAAC;IAEM,GAAG,CAAC,GAAW,EAAE,KAAU;QAChC,OAAO,gBAAgB,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IACvD,CAAC;IAEM,IAAI;QACT,OAAO,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC5C,CAAC;IAED,MAAM,CAAC,GAAG,CAAC,IAAY,EAAE,GAAQ;QAC/B,IAAI,CAAC,GAAG,EAAE,CAAC;YACT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C,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YACtD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;gBACtC,MAAM,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;gBACrB,MAAM,SAAS,GAAG,CAAC,KAAK,IAAI,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,KAAK,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;gBAClE,IAAI,SAAS,EAAE,CAAC;oBACd,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;gBACtB,CAAC;qBAAM,CAAC;oBACN,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,OAAO,MAAM,CAAC,GAAG,CAAC,KAAK,QAAQ,EAAE,CAAC;wBACpD,MAAM,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;oBAC3D,CAAC;oBACD,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;gBACvB,CAAC;YACH,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,CAAC,IAAI,CAAC,MAAW;QACrB,MAAM,IAAI,GAAwB,EAAE,CAAC;QACrC,IAAA,wBAAa,EAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAC5B,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AA9ED,4CA8EC"}
|
package/package.json
CHANGED
package/test/test.js
CHANGED
|
@@ -30,5 +30,8 @@ const propertyAccessor = new PropertyAccessor({ a: [{ b: 2 }] });
|
|
|
30
30
|
|
|
31
31
|
assert.equal(propertyAccessor.get('a[0].b'), 2);
|
|
32
32
|
assert.equal(propertyAccessor.set('a[0].c', 3), true);
|
|
33
|
-
|
|
33
|
+
assert.equal(propertyAccessor.get('a[0].c'), 3);
|
|
34
|
+
assert.equal(propertyAccessor.set('a[0].23',1), true);
|
|
35
|
+
assert.equal(propertyAccessor.get('a[0].23'), 1);
|
|
36
|
+
console.log(propertyAccessor.flat())
|
|
34
37
|
console.log('tests passed');
|