nestjs-r2-storage 1.2.4 → 1.2.6
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/README.md
CHANGED
|
@@ -389,6 +389,20 @@ R2StorageModule.forRootAsync({
|
|
|
389
389
|
|
|
390
390
|
## Changelog
|
|
391
391
|
|
|
392
|
+
### v1.2.6 (2025-04-20)
|
|
393
|
+
|
|
394
|
+
- Refactored getNestedValue: access key first, then handle array segments
|
|
395
|
+
- Refactored setNestedValue: proper handling of empty brackets [] and indexed arrays [0]
|
|
396
|
+
- Robust parsing of paths: user.profile.image, gallery[].photo, variants[0].images[].url
|
|
397
|
+
|
|
398
|
+
### v1.2.5 (2025-04-20)
|
|
399
|
+
|
|
400
|
+
- Rewrote parseFieldPath to split by dot then parse each segment (fixes regex state bugs)
|
|
401
|
+
- Fixed getNestedValue array traversal when next key is a property (not array/index)
|
|
402
|
+
- Fixed setNestedValue for empty array brackets `[]` and indexed arrays `[0]`
|
|
403
|
+
- Added null/undefined guards throughout
|
|
404
|
+
- Supports: `gallery[].photo`, `variants[].images[].url`, `a[].b[0].c`
|
|
405
|
+
|
|
392
406
|
### v1.2.4 (2025-04-20)
|
|
393
407
|
|
|
394
408
|
- Fixed parseFieldPath regex to handle keys containing dots
|
|
@@ -10,110 +10,110 @@ exports.getArrayElementPath = getArrayElementPath;
|
|
|
10
10
|
exports.getAllArrayItemPaths = getAllArrayItemPaths;
|
|
11
11
|
function parseFieldPath(path) {
|
|
12
12
|
const segments = [];
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
const parts = path.split(".");
|
|
14
|
+
for (const part of parts) {
|
|
15
|
+
const match = part.match(/^([^\[\]]+)(?:\[(\d*)\])?$/);
|
|
16
|
+
if (!match)
|
|
17
|
+
continue;
|
|
18
|
+
const key = match[1];
|
|
19
|
+
const indexRaw = match[2];
|
|
20
|
+
const hasIndex = indexRaw !== undefined && indexRaw !== "";
|
|
21
|
+
const hasEmptyBrackets = indexRaw === "";
|
|
18
22
|
segments.push({
|
|
19
|
-
key
|
|
20
|
-
isArray:
|
|
21
|
-
arrayIndex: hasIndex ? parseInt(
|
|
23
|
+
key,
|
|
24
|
+
isArray: hasIndex || hasEmptyBrackets,
|
|
25
|
+
arrayIndex: hasIndex ? parseInt(indexRaw, 10) : undefined,
|
|
22
26
|
});
|
|
23
27
|
}
|
|
24
28
|
return { segments };
|
|
25
29
|
}
|
|
26
30
|
function getNestedValue(obj, path) {
|
|
27
|
-
if (!path || !obj)
|
|
31
|
+
if (!path || !obj)
|
|
28
32
|
return undefined;
|
|
29
|
-
}
|
|
30
33
|
const { segments } = parseFieldPath(path);
|
|
31
34
|
let current = obj;
|
|
32
35
|
for (let i = 0; i < segments.length; i++) {
|
|
33
36
|
const segment = segments[i];
|
|
34
|
-
if (current
|
|
37
|
+
if (current == null)
|
|
38
|
+
return undefined;
|
|
39
|
+
current = current[segment.key];
|
|
40
|
+
if (current == null)
|
|
35
41
|
return undefined;
|
|
36
|
-
}
|
|
37
42
|
if (segment.isArray) {
|
|
38
|
-
if (!Array.isArray(current))
|
|
43
|
+
if (!Array.isArray(current))
|
|
39
44
|
return undefined;
|
|
40
|
-
}
|
|
41
|
-
if (i === segments.length - 1) {
|
|
42
|
-
return current;
|
|
43
|
-
}
|
|
44
|
-
const nextSegment = segments[i + 1];
|
|
45
|
-
if (nextSegment && nextSegment.key && !nextSegment.isArray && !nextSegment.arrayIndex) {
|
|
46
|
-
return current;
|
|
47
|
-
}
|
|
48
45
|
if (segment.arrayIndex !== undefined) {
|
|
49
46
|
current = current[segment.arrayIndex];
|
|
47
|
+
continue;
|
|
50
48
|
}
|
|
51
|
-
|
|
49
|
+
if (i === segments.length - 1)
|
|
50
|
+
return current;
|
|
51
|
+
const next = segments[i + 1];
|
|
52
|
+
if (next && !next.isArray && next.arrayIndex === undefined) {
|
|
52
53
|
return current;
|
|
53
54
|
}
|
|
54
55
|
}
|
|
55
|
-
else {
|
|
56
|
-
current = current[segment.key];
|
|
57
|
-
}
|
|
58
56
|
}
|
|
59
57
|
return current;
|
|
60
58
|
}
|
|
61
59
|
function setNestedValue(obj, path, value) {
|
|
62
|
-
if (!path)
|
|
60
|
+
if (!path)
|
|
63
61
|
return obj;
|
|
64
|
-
}
|
|
65
62
|
const { segments } = parseFieldPath(path);
|
|
66
63
|
const result = { ...obj };
|
|
67
64
|
let current = result;
|
|
68
|
-
for (let i = 0; i < segments.length
|
|
65
|
+
for (let i = 0; i < segments.length; i++) {
|
|
69
66
|
const segment = segments[i];
|
|
70
|
-
const
|
|
71
|
-
if (current[segment.key]
|
|
72
|
-
current[segment.key] =
|
|
67
|
+
const isLast = i === segments.length - 1;
|
|
68
|
+
if (current[segment.key] == null) {
|
|
69
|
+
current[segment.key] = segment.isArray ? [] : {};
|
|
73
70
|
}
|
|
74
|
-
|
|
75
|
-
current[segment.key]
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
71
|
+
if (segment.isArray) {
|
|
72
|
+
if (!Array.isArray(current[segment.key])) {
|
|
73
|
+
current[segment.key] = [];
|
|
74
|
+
}
|
|
75
|
+
const arr = current[segment.key];
|
|
76
|
+
if (segment.arrayIndex !== undefined) {
|
|
77
|
+
if (!arr[segment.arrayIndex]) {
|
|
78
|
+
arr[segment.arrayIndex] = {};
|
|
79
|
+
}
|
|
80
|
+
if (isLast) {
|
|
81
|
+
arr[segment.arrayIndex] = value;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
arr[segment.arrayIndex] = { ...arr[segment.arrayIndex] };
|
|
85
|
+
current = arr[segment.arrayIndex];
|
|
86
|
+
}
|
|
84
87
|
}
|
|
85
88
|
else {
|
|
86
|
-
|
|
89
|
+
if (isLast) {
|
|
90
|
+
arr.push(value);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
const newItem = {};
|
|
94
|
+
arr.push(newItem);
|
|
95
|
+
current = newItem;
|
|
96
|
+
}
|
|
87
97
|
}
|
|
88
|
-
current = current[segment.arrayIndex];
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
const lastSegment = segments[segments.length - 1];
|
|
92
|
-
if (lastSegment.isArray && lastSegment.arrayIndex !== undefined) {
|
|
93
|
-
if (!Array.isArray(current)) {
|
|
94
|
-
current = [];
|
|
95
98
|
}
|
|
96
99
|
else {
|
|
97
|
-
|
|
100
|
+
if (isLast) {
|
|
101
|
+
current[segment.key] = value;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
current[segment.key] = { ...current[segment.key] };
|
|
105
|
+
current = current[segment.key];
|
|
106
|
+
}
|
|
98
107
|
}
|
|
99
|
-
current[lastSegment.arrayIndex] = {
|
|
100
|
-
...current[lastSegment.arrayIndex],
|
|
101
|
-
[lastSegment.key]: value,
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
else {
|
|
105
|
-
current[lastSegment.key] = value;
|
|
106
108
|
}
|
|
107
109
|
return result;
|
|
108
110
|
}
|
|
109
111
|
function collectNestedValues(obj, path) {
|
|
110
112
|
const value = getNestedValue(obj, path);
|
|
111
|
-
if (value === undefined)
|
|
113
|
+
if (value === undefined)
|
|
112
114
|
return [];
|
|
113
|
-
|
|
114
|
-
if (Array.isArray(value)) {
|
|
115
|
+
if (Array.isArray(value))
|
|
115
116
|
return value;
|
|
116
|
-
}
|
|
117
117
|
return [value];
|
|
118
118
|
}
|
|
119
119
|
function isArrayPath(path) {
|
|
@@ -124,12 +124,12 @@ function getArrayBasePath(path) {
|
|
|
124
124
|
return match ? match[1] : path;
|
|
125
125
|
}
|
|
126
126
|
function getArrayElementPath(path) {
|
|
127
|
-
return path.replace(
|
|
127
|
+
return path.replace("[]", "");
|
|
128
128
|
}
|
|
129
129
|
function getAllArrayItemPaths(path, arrayLength) {
|
|
130
130
|
const basePath = getArrayBasePath(path);
|
|
131
131
|
const elementPath = getArrayElementPath(path);
|
|
132
|
-
const cleanElementPath = elementPath.replace(/^\./,
|
|
132
|
+
const cleanElementPath = elementPath.replace(/^\./, "");
|
|
133
133
|
const paths = [];
|
|
134
134
|
for (let i = 0; i < arrayLength; i++) {
|
|
135
135
|
paths.push(`${basePath}[${i}].${cleanElementPath}`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nested-value.util.js","sourceRoot":"","sources":["../../../src/r2-storage/utils/nested-value.util.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"nested-value.util.js","sourceRoot":"","sources":["../../../src/r2-storage/utils/nested-value.util.ts"],"names":[],"mappings":";;AAgBA,wCAsBC;AAKD,wCAyCC;AAKD,wCA6DC;AAKD,kDAUC;AAKD,kCAEC;AAED,4CAGC;AAED,kDAEC;AAED,oDAeC;AAtLD,SAAgB,cAAc,CAAC,IAAY;IACzC,MAAM,QAAQ,GAAkB,EAAE,CAAC;IACnC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAE9B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAC;QACvD,IAAI,CAAC,KAAK;YAAE,SAAS;QAErB,MAAM,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACrB,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QAE1B,MAAM,QAAQ,GAAG,QAAQ,KAAK,SAAS,IAAI,QAAQ,KAAK,EAAE,CAAC;QAC3D,MAAM,gBAAgB,GAAG,QAAQ,KAAK,EAAE,CAAC;QAEzC,QAAQ,CAAC,IAAI,CAAC;YACZ,GAAG;YACH,OAAO,EAAE,QAAQ,IAAI,gBAAgB;YACrC,UAAU,EAAE,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC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