@arrai-innovations/reactive-helpers 11.3.0 → 11.4.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/.circleci/config.yml +5 -5
- package/package.json +2 -2
- package/tests/unit/.eslintrc.cjs +1 -0
- package/tests/unit/utils/deleteKey.spec.js +265 -0
- package/utils/deleteKey.js +50 -0
- package/utils/index.js +1 -0
package/.circleci/config.yml
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
version: 2.1
|
|
2
2
|
orbs:
|
|
3
|
-
eslint: arrai/eslint@7.
|
|
4
|
-
prettier: arrai/prettier@5.
|
|
5
|
-
npm: arrai/npm@2.
|
|
3
|
+
eslint: arrai/eslint@7.8.0
|
|
4
|
+
prettier: arrai/prettier@5.7.0
|
|
5
|
+
npm: arrai/npm@2.11.0
|
|
6
6
|
github: arrai/github@1.1.0
|
|
7
7
|
workflows:
|
|
8
8
|
test:
|
|
@@ -46,7 +46,7 @@ workflows:
|
|
|
46
46
|
ignore: /.*/
|
|
47
47
|
lint:
|
|
48
48
|
jobs:
|
|
49
|
-
- eslint/
|
|
49
|
+
- eslint/eslint_fixed_ip:
|
|
50
50
|
name: eslint
|
|
51
51
|
context: arrai-global
|
|
52
52
|
files: "."
|
|
@@ -72,7 +72,7 @@ workflows:
|
|
|
72
72
|
branches:
|
|
73
73
|
ignore:
|
|
74
74
|
- main
|
|
75
|
-
- prettier/
|
|
75
|
+
- prettier/code_style_fixed_ip:
|
|
76
76
|
name: prettier
|
|
77
77
|
context: arrai-global
|
|
78
78
|
filters:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arrai-innovations/reactive-helpers",
|
|
3
|
-
"version": "11.
|
|
3
|
+
"version": "11.4.0",
|
|
4
4
|
"description": "VueJS 3 utility composition functions to help manipulate objects and lists.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"homepage": "https://github.com/arrai-innovations/reactive-helpers#readme",
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@arrai-innovations/commitlint-config": "^1.1.0",
|
|
31
|
-
"@commitlint/cli": "^
|
|
31
|
+
"@commitlint/cli": "^18.4.3",
|
|
32
32
|
"@godaddy/dmd": "^1.0.4",
|
|
33
33
|
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
|
|
34
34
|
"@vitest/coverage-v8": "^0.32.2",
|
package/tests/unit/.eslintrc.cjs
CHANGED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { del, lodashLikePathSplit } from "../../../utils/deleteKey.js";
|
|
2
|
+
|
|
3
|
+
describe("utils/deleteKey.js", () => {
|
|
4
|
+
describe("pathSplitRegex", () => {
|
|
5
|
+
const tests = [
|
|
6
|
+
{
|
|
7
|
+
path: "",
|
|
8
|
+
expected: [""],
|
|
9
|
+
obj: {},
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
path: "a",
|
|
13
|
+
expected: ["a"],
|
|
14
|
+
obj: { a: 1 },
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
path: "a.",
|
|
18
|
+
expected: ["a", ""],
|
|
19
|
+
obj: { a: 1 },
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
path: "a.b.c",
|
|
23
|
+
expected: ["a", "b", "c"],
|
|
24
|
+
obj: { a: { b: { c: 1 } } },
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
path: "a[b[c]]",
|
|
28
|
+
expected: ["a", "b", "c"],
|
|
29
|
+
obj: { a: { b: { c: 1 } } },
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
path: "a.b.",
|
|
33
|
+
expected: ["a", "b", ""],
|
|
34
|
+
obj: { a: { b: { c: 1 } } },
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
path: "a[b[1]]",
|
|
38
|
+
expected: ["a", "b", "1"],
|
|
39
|
+
obj: { a: { b: [1, 2, 3] } },
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
path: "a[b[1].c]",
|
|
43
|
+
expected: ["a", "b", "1", "c"],
|
|
44
|
+
obj: { a: { b: [{ c: 1 }, { c: 2 }, { c: 3 }] } },
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
path: "a[1]",
|
|
48
|
+
expected: ["a", "1"],
|
|
49
|
+
obj: { a: [1, 2, 3] },
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
path: "a[1].b",
|
|
53
|
+
expected: ["a", "1", "b"],
|
|
54
|
+
obj: { a: [{ b: 1 }, { b: 2 }, { b: 3 }] },
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
path: "a[1].b[1]",
|
|
58
|
+
expected: ["a", "1", "b", "1"],
|
|
59
|
+
obj: { a: [{ b: [1, 2, 3] }, { b: [4, 5, 6] }, { b: [7, 8, 9] }] },
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
path: "a[1].",
|
|
63
|
+
expected: ["a", "1", ""],
|
|
64
|
+
obj: { a: [{}, {}, {}] },
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
path: "a[1].b[1].",
|
|
68
|
+
expected: ["a", "1", "b", "1", ""],
|
|
69
|
+
obj: { a: [{ b: [1, {}, 3] }, { b: [4, {}, 6] }, { b: [7, {}, 9] }] },
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
path: "a[1].c",
|
|
73
|
+
expected: ["a", "1", "c"],
|
|
74
|
+
obj: { a: [{ b: [1, 2, 3] }, { b: [4, 5, 6] }, { c: [7, 8, 9] }] },
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
path: "c[",
|
|
78
|
+
expected: ["c["],
|
|
79
|
+
obj: { "c[": 1 },
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
path: "c[1",
|
|
83
|
+
expected: ["c[1"],
|
|
84
|
+
obj: { "c[1": 1 },
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
path: "c]",
|
|
88
|
+
expected: ["c]"],
|
|
89
|
+
obj: { "c]": 1 },
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
path: "c1]",
|
|
93
|
+
expected: ["c1]"],
|
|
94
|
+
obj: { "c1]": 1 },
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
path: "$property",
|
|
98
|
+
expected: ["$property"],
|
|
99
|
+
obj: { $property: 1 },
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
path: "~path",
|
|
103
|
+
expected: ["~path"],
|
|
104
|
+
obj: { "~path": 1 },
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
path: "name_with_special$characters",
|
|
108
|
+
expected: ["name_with_special$characters"],
|
|
109
|
+
obj: { name_with_special$characters: 1 },
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
path: "a[1][1]",
|
|
113
|
+
expected: ["a", "1", "1"],
|
|
114
|
+
obj: [{ a: [1, 2, 3] }, { a: [4, 5, 6] }, { a: [7, 8, 9] }],
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
path: "b[[1]]",
|
|
118
|
+
expected: ["b", "1"],
|
|
119
|
+
obj: [{ b: [1, 2, 3] }, { b: [4, 5, 6] }, { b: [7, 8, 9] }],
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
path: "c[1][",
|
|
123
|
+
expected: ["c", "1"],
|
|
124
|
+
obj: {
|
|
125
|
+
c: [{}, { "[": 1 }, {}],
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
path: "c[1][",
|
|
130
|
+
expected: ["c[1]["],
|
|
131
|
+
obj: {
|
|
132
|
+
"c[1][": 1,
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
path: "c[[2]",
|
|
137
|
+
expected: ["c", "2"],
|
|
138
|
+
obj: {
|
|
139
|
+
c: [1, 2, 3],
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
path: "c[1][.f[3]",
|
|
144
|
+
expected: ["c", "1", "f", "3"],
|
|
145
|
+
obj: {
|
|
146
|
+
c: [{}, { f: [1, 2, 3, 4] }],
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
path: "c[1][.f[3]",
|
|
151
|
+
expected: ["c[1][.f[3]"],
|
|
152
|
+
obj: {
|
|
153
|
+
"c[1][.f[3]": 1,
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
path: "[1][1]",
|
|
158
|
+
expected: ["1", "1"],
|
|
159
|
+
obj: [
|
|
160
|
+
[1, 2, 3],
|
|
161
|
+
[4, 5, 6],
|
|
162
|
+
[7, 8, 9],
|
|
163
|
+
],
|
|
164
|
+
},
|
|
165
|
+
];
|
|
166
|
+
for (let i = 0; i < tests.length; i++) {
|
|
167
|
+
it(`should split ${tests[i].path}`, () => {
|
|
168
|
+
const path = tests[i].path;
|
|
169
|
+
const expected = tests[i].expected;
|
|
170
|
+
const obj = tests[i].obj;
|
|
171
|
+
const actual = lodashLikePathSplit(path, obj);
|
|
172
|
+
expect(actual, `Failed on path ${path} for obj ${JSON.stringify(obj)}`).toEqual(expected);
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
describe("del", () => {
|
|
177
|
+
describe("Basic Cases", () => {
|
|
178
|
+
it("should delete a key from an object", () => {
|
|
179
|
+
const obj = { a: 1, b: 2 };
|
|
180
|
+
del(obj, "a");
|
|
181
|
+
expect(obj).toEqual({ b: 2 });
|
|
182
|
+
});
|
|
183
|
+
it("should delete a deep key from an object", () => {
|
|
184
|
+
const obj = { a: { b: { c: 1 } } };
|
|
185
|
+
del(obj, "a.b.c");
|
|
186
|
+
expect(obj).toEqual({ a: { b: {} } });
|
|
187
|
+
});
|
|
188
|
+
it("should delete an index from an array", () => {
|
|
189
|
+
const obj = { a: [1, 2, 3] };
|
|
190
|
+
del(obj, "a[1]");
|
|
191
|
+
expect(obj).toEqual({ a: [1, undefined, 3] });
|
|
192
|
+
});
|
|
193
|
+
it("should delete a deep index from an array", () => {
|
|
194
|
+
const obj = { a: [{ b: 1 }, { b: 2 }, { b: 3 }] };
|
|
195
|
+
del(obj, "a[1].b");
|
|
196
|
+
expect(obj).toEqual({ a: [{ b: 1 }, {}, { b: 3 }] });
|
|
197
|
+
});
|
|
198
|
+
it("should delete a deep index from a key with multiple indexes", () => {
|
|
199
|
+
const obj = { a: [{ b: [1, 2, 3] }, { b: [4, 5, 6] }, { b: [7, 8, 9] }] };
|
|
200
|
+
del(obj, "a[1].b[1]");
|
|
201
|
+
expect(obj).toEqual({ a: [{ b: [1, 2, 3] }, { b: [4, undefined, 6] }, { b: [7, 8, 9] }] });
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
describe("Edge Cases", () => {
|
|
205
|
+
it("should handle deleting a non-existent key", () => {
|
|
206
|
+
const obj = { a: 1, b: 2 };
|
|
207
|
+
del(obj, "c");
|
|
208
|
+
expect(obj).toEqual({ a: 1, b: 2 });
|
|
209
|
+
});
|
|
210
|
+
it("should handled deleting a key in a non-existent object", () => {
|
|
211
|
+
const obj = { a: 1, b: 2 };
|
|
212
|
+
del(obj, "c.d");
|
|
213
|
+
expect(obj).toEqual({ a: 1, b: 2 });
|
|
214
|
+
});
|
|
215
|
+
it("should handle deleting an index in a non-existent array", () => {
|
|
216
|
+
const obj = { a: 1, b: 2 };
|
|
217
|
+
del(obj, "c[0]");
|
|
218
|
+
expect(obj).toEqual({ a: 1, b: 2 });
|
|
219
|
+
});
|
|
220
|
+
it("should handle deleting a non-existent index in an array", () => {
|
|
221
|
+
const obj = { a: [1, 2, 3] };
|
|
222
|
+
del(obj, "a[3]");
|
|
223
|
+
expect(obj).toEqual({ a: [1, 2, 3] });
|
|
224
|
+
});
|
|
225
|
+
it("should handle deleting a non-existent index in a deep array", () => {
|
|
226
|
+
const obj = { a: [{ b: [1, 2, 3] }, { b: [4, 5, 6] }, { b: [7, 8, 9] }] };
|
|
227
|
+
del(obj, "a[1].b[3]");
|
|
228
|
+
expect(obj).toEqual({ a: [{ b: [1, 2, 3] }, { b: [4, 5, 6] }, { b: [7, 8, 9] }] });
|
|
229
|
+
});
|
|
230
|
+
it("should handled unpaired brackets as a key", () => {
|
|
231
|
+
const obj = { a: 1, b: 2, "c[": 3 };
|
|
232
|
+
del(obj, "c[");
|
|
233
|
+
expect(obj).toEqual({ a: 1, b: 2 });
|
|
234
|
+
});
|
|
235
|
+
it("should handle deleting a key with a trailing dot", () => {
|
|
236
|
+
const obj = { a: 1, b: 2 };
|
|
237
|
+
del(obj, "a.");
|
|
238
|
+
expect(obj).toEqual({ a: 1, b: 2 });
|
|
239
|
+
});
|
|
240
|
+
it("should handle deleting a key with a trailing dot in a deep object", () => {
|
|
241
|
+
const obj = { a: { b: { c: 1 } } };
|
|
242
|
+
del(obj, "a.b.");
|
|
243
|
+
expect(obj).toEqual({ a: { b: { c: 1 } } });
|
|
244
|
+
});
|
|
245
|
+
it("should handle an array as the root object", () => {
|
|
246
|
+
const obj = [1, 2, 3];
|
|
247
|
+
del(obj, "[1]");
|
|
248
|
+
expect(obj).toEqual([1, undefined, 3]);
|
|
249
|
+
});
|
|
250
|
+
it("should handle nested arrays as the root object", () => {
|
|
251
|
+
const obj = [
|
|
252
|
+
[1, 2, 3],
|
|
253
|
+
[4, 5, 6],
|
|
254
|
+
[7, 8, 9],
|
|
255
|
+
];
|
|
256
|
+
del(obj, "[1][1]");
|
|
257
|
+
expect(obj).toEqual([
|
|
258
|
+
[1, 2, 3],
|
|
259
|
+
[4, undefined, 6],
|
|
260
|
+
[7, 8, 9],
|
|
261
|
+
]);
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import isKey from "lodash-es/_isKey.js";
|
|
2
|
+
import isArray from "lodash-es/isArray.js";
|
|
3
|
+
import isSymbol from "lodash-es/isSymbol.js";
|
|
4
|
+
|
|
5
|
+
const reEscapeChar = /\\(\\)?/g;
|
|
6
|
+
const rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g;
|
|
7
|
+
|
|
8
|
+
function toKey(value) {
|
|
9
|
+
if (typeof value == "string" || isSymbol(value)) {
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
var result = value + "";
|
|
13
|
+
// noinspection EqualityComparisonWithCoercionJS
|
|
14
|
+
return result == "0" && 1 / value == -(1 / 0) ? "-0" : result;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function lodashLikePathSplit(string, object) {
|
|
18
|
+
if (isArray(string)) {
|
|
19
|
+
return string;
|
|
20
|
+
}
|
|
21
|
+
if (isKey(string, object)) {
|
|
22
|
+
return [string];
|
|
23
|
+
}
|
|
24
|
+
const result = [];
|
|
25
|
+
if (string.charCodeAt(0) === 46 /* . */) {
|
|
26
|
+
result.push("");
|
|
27
|
+
}
|
|
28
|
+
string.replace(rePropName, function (match, number, quote, subString) {
|
|
29
|
+
result.push(quote ? subString.replace(reEscapeChar, "$1") : number || match);
|
|
30
|
+
});
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function del(obj, path) {
|
|
35
|
+
// lodash-like delete function, as companion for get/set
|
|
36
|
+
if (!obj) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const pathArray = lodashLikePathSplit(path, obj);
|
|
40
|
+
let index = 0;
|
|
41
|
+
const length = pathArray.length;
|
|
42
|
+
|
|
43
|
+
while (obj != null && index < length - 1) {
|
|
44
|
+
obj = obj[toKey(pathArray[index++])];
|
|
45
|
+
}
|
|
46
|
+
if (!obj) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
delete obj[toKey(pathArray[index])];
|
|
50
|
+
}
|
package/utils/index.js
CHANGED
|
@@ -3,6 +3,7 @@ export * from "./classes.js";
|
|
|
3
3
|
export * from "./compact.js";
|
|
4
4
|
export * from "./debugMessage.js";
|
|
5
5
|
export * from "./debugWatch.js";
|
|
6
|
+
export * from "./deleteKey.js";
|
|
6
7
|
export * from "./flattenPaths.js";
|
|
7
8
|
export * from "./getFakeId.js";
|
|
8
9
|
export * from "./keyDiff.js";
|