@arrai-innovations/reactive-helpers 11.3.0 → 11.4.1

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.
@@ -1,34 +1,34 @@
1
1
  version: 2.1
2
2
  orbs:
3
- eslint: arrai/eslint@7.6.0
4
- prettier: arrai/prettier@5.5.0
5
- npm: arrai/npm@2.9.0
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:
9
9
  jobs:
10
- # - npm/coverage:
11
- # name: tests
12
- # context: arrai-global
13
- # filters:
14
- # branches:
15
- # only:
16
- # - main
17
- # - npm/coverage:
18
- # name: tests-no-badge
19
- # context: arrai-global
20
- # create_badges: false
21
- # filters:
22
- # tags:
23
- # only: /.*/
24
- # branches:
25
- # ignore:
26
- # - main
10
+ - npm/coverage:
11
+ name: tests
12
+ context: arrai-global
13
+ filters:
14
+ branches:
15
+ only:
16
+ - main
17
+ - npm/coverage:
18
+ name: tests-no-badge
19
+ context: arrai-global
20
+ create_badges: false
21
+ filters:
22
+ tags:
23
+ only: /.*/
24
+ branches:
25
+ ignore:
26
+ - main
27
27
  - npm/publish:
28
28
  name: publish
29
29
  context: arrai-private-package-publishing
30
- # requires:
31
- # - tests-no-badge
30
+ requires:
31
+ - tests-no-badge
32
32
  filters:
33
33
  tags:
34
34
  only: /.*/
@@ -37,8 +37,8 @@ workflows:
37
37
  - github/create_release:
38
38
  name: release_on_github
39
39
  context: arrai-global
40
- # requires:
41
- # - tests-no-badge
40
+ requires:
41
+ - tests-no-badge
42
42
  filters:
43
43
  tags:
44
44
  only: /.*/
@@ -46,7 +46,7 @@ workflows:
46
46
  ignore: /.*/
47
47
  lint:
48
48
  jobs:
49
- - eslint/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/code_style:
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.0",
3
+ "version": "11.4.1",
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": "^16.2.4",
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",
@@ -6,5 +6,6 @@ module.exports = {
6
6
  },
7
7
  rules: {
8
8
  "vitest/no-conditional-expect": "off",
9
+ "vitest/valid-expect": "off", // we want to use expect(value, message).toBe(expected), which is not supported by this rule
9
10
  },
10
11
  };
@@ -593,12 +593,14 @@ describe("use/listInstance.spec.js", function () {
593
593
  expect(isReadonly(passedIsCancelled)).toBe(true);
594
594
  expect(passedIsCancelled.value).toBe(false);
595
595
  expect(cancelablePromise.cancel).toBeTruthy();
596
+ expect(listInstance.state.loading).toBe(true);
596
597
 
597
598
  const cancelPromise = cancelablePromise.cancel();
598
599
  myListFnCancelResolve();
599
600
  await cancelPromise;
600
601
 
601
602
  expect(passedIsCancelled.value).toBe(true);
603
+ expect(listInstance.state.loading).toBe(false);
602
604
  });
603
605
  });
604
606
  });
@@ -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
+ });
@@ -67,10 +67,6 @@ export function useListCalculated({ parentState, calculatedObjectsRules }) {
67
67
  function calculatedObjectsWatch() {
68
68
  const calculatedObjectsRulesIsEmpty = !state.calculatedObjectsRules || isEmpty(state.calculatedObjectsRules);
69
69
  for (const objectKey of Object.keys(state.calculatedObjects)) {
70
- const originalObject = toRef(parentState.objects, objectKey);
71
- const relatedObject = parentState.relatedObjects
72
- ? toRef(parentState.relatedObjects, objectKey)
73
- : ref(undefined);
74
70
  if (!state.calculatedObjects[objectKey]) {
75
71
  state.calculatedObjects[objectKey] = {};
76
72
  }
@@ -96,12 +92,16 @@ export function useListCalculated({ parentState, calculatedObjectsRules }) {
96
92
  if (!calculatedObjectsEffectScopes[objectKey]) {
97
93
  calculatedObjectsEffectScopes[objectKey] = effectScope();
98
94
  }
95
+ const originalObjectRef = toRef(parentState.objects, objectKey);
96
+ const relatedObjectRef = parentState.relatedObjects
97
+ ? toRef(parentState.relatedObjects, objectKey)
98
+ : ref(undefined);
99
99
  calculatedObjectsEffectScopes[objectKey].run(() => {
100
100
  for (const addedRuleKey of addedRuleKeys) {
101
101
  calculatedObjectsObject[addedRuleKey] = computed(() =>
102
102
  state.calculatedObjectsRules?.[addedRuleKey]?.(
103
- unref(originalObject),
104
- unref(relatedObject),
103
+ unref(originalObjectRef),
104
+ unref(relatedObjectRef),
105
105
  calculatedObjectsObject
106
106
  )
107
107
  );
@@ -198,6 +198,7 @@ export function useListInstance({ props, functions = {}, keepOldPages = false })
198
198
  if (promise) {
199
199
  await promise;
200
200
  }
201
+ state.loading = false;
201
202
  };
202
203
  }
203
204
  // the indirection of promises here is to allow us to do additional work on listPromise's cancel
@@ -92,8 +92,6 @@ export function useListRelated({ parentState, relatedObjectsRules }) {
92
92
  function relatedObjectsWatch() {
93
93
  const relatedObjectsRulesIsEmpty = !state.relatedObjectsRules || isEmpty(state.relatedObjectsRules);
94
94
  for (const objectKey of Object.keys(state.relatedObjects)) {
95
- const originalObjectRef = toRef(parentState.objects, objectKey);
96
- const relatedObjectRef = toRef(state.relatedObjects, objectKey);
97
95
  let removedRuleKeys, addedRuleKeys;
98
96
  if (!relatedObjectsRulesIsEmpty) {
99
97
  ({ removedKeys: removedRuleKeys, addedKeys: addedRuleKeys } = keyDiff(
@@ -119,6 +117,8 @@ export function useListRelated({ parentState, relatedObjectsRules }) {
119
117
  if (!relatedObjectsEffectScopes[objectKey]) {
120
118
  relatedObjectsEffectScopes[objectKey] = effectScope();
121
119
  }
120
+ const originalObjectRef = toRef(parentState.objects, objectKey);
121
+ const relatedObjectRef = toRef(state.relatedObjects, objectKey);
122
122
  relatedObjectsEffectScopes[objectKey].run(() => {
123
123
  for (const addedRuleKey of addedRuleKeys) {
124
124
  const rules = toRef(state.relatedObjectsRules, addedRuleKey);
@@ -79,7 +79,6 @@ export function useObjectRelated({ parentState, relatedObjectRules }) {
79
79
  // deal with computed objects being passed.
80
80
  const ruleObjects = unref(state.relatedObjectRules?.[addedRuleKey]?.objects);
81
81
  const rulePkKey = state.relatedObjectRules?.[addedRuleKey]?.pkKey || addedRuleKey;
82
- const ruleOrder = unref(state.relatedObjectRules?.[addedRuleKey]?.order);
83
82
  if (!ruleObjects || !rulePkKey) {
84
83
  state.relatedObject[addedRuleKey] = undefined;
85
84
  return;
@@ -90,6 +89,7 @@ export function useObjectRelated({ parentState, relatedObjectRules }) {
90
89
  return;
91
90
  }
92
91
  if (isArray(value)) {
92
+ const ruleOrder = unref(state.relatedObjectRules?.[addedRuleKey]?.order);
93
93
  // the related list could be sorted differently than the original list.
94
94
  if (ruleOrder?.length) {
95
95
  value = value.filter(identity);
@@ -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";