@arrai-innovations/reactive-helpers 10.4.0 → 10.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrai-innovations/reactive-helpers",
3
- "version": "10.4.0",
3
+ "version": "10.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",
@@ -3,7 +3,7 @@ import {
3
3
  assignReactiveObject,
4
4
  AssignReactiveObjectError,
5
5
  } from "../../../utils/assignReactiveObject.js";
6
- import { computed, EffectScope, effectScope, reactive, toRef } from "vue";
6
+ import { computed, EffectScope, effectScope, reactive, toRef, unref } from "vue";
7
7
 
8
8
  describe("utils/assignReactiveObject", function () {
9
9
  describe.skip("addOrUpdateReactiveObject", function () {});
@@ -57,6 +57,68 @@ describe("utils/assignReactiveObject", function () {
57
57
  tes.stop();
58
58
  }
59
59
  });
60
+ it("when target is reactive and source are reactive, for non-primitive values", function () {
61
+ const target = reactive({
62
+ a: { e: 8 },
63
+ b: { f: 16 },
64
+ c: null,
65
+ });
66
+ const source = reactive({
67
+ a: { e: 1 },
68
+ b: { f: 2 },
69
+ c: { g: 4 },
70
+ });
71
+ target.c = toRef(source, "c");
72
+ const tes = effectScope();
73
+ let computedSum;
74
+ tes.run(() => {
75
+ computedSum = computed(() => target.a.e + target.b.f + target.c.g);
76
+ });
77
+ try {
78
+ expect(unref(target.a)).not.toBe(unref(source.a));
79
+ expect(unref(target.b)).not.toBe(unref(source.b));
80
+ expect(unref(target.c)).toBe(unref(source.c));
81
+ expect(computedSum.value).toBe(28);
82
+ assignReactiveObject(target, source);
83
+ expect(target).toEqual({
84
+ a: { e: 1 },
85
+ b: { f: 2 },
86
+ c: { g: 4 },
87
+ });
88
+ expect(unref(target.a)).toBe(unref(source.a));
89
+ expect(unref(target.b)).toBe(unref(source.b));
90
+ expect(unref(target.c)).toBe(unref(source.c));
91
+ expect(computedSum.value).toBe(7);
92
+ } finally {
93
+ computedSum = null;
94
+ tes.stop();
95
+ }
96
+ });
97
+ it("does nothing when all targets are objectlike and already point to the source", function () {
98
+ const target = reactive({});
99
+ const source = reactive({
100
+ a: { e: 1 },
101
+ b: { f: 2 },
102
+ c: { g: 4 },
103
+ });
104
+ target.a = toRef(source, "a");
105
+ target.b = toRef(source, "b");
106
+ target.c = toRef(source, "c");
107
+ expect(unref(toRef(target, "a"))).toBe(unref(source.a));
108
+ expect(unref(toRef(target, "b"))).toBe(unref(source.b));
109
+ expect(unref(toRef(target, "c"))).toBe(unref(source.c));
110
+ expect(target.a).toBe(source.a);
111
+ expect(target.b).toBe(source.b);
112
+ expect(target.c).toBe(source.c);
113
+ const didAnything = assignReactiveObject(target, source);
114
+ expect(didAnything).toBeFalsy();
115
+ expect(unref(toRef(target, "a"))).toBe(unref(source.a));
116
+ expect(unref(toRef(target, "b"))).toBe(unref(source.b));
117
+ expect(unref(toRef(target, "c"))).toBe(unref(source.c));
118
+ expect(target.a).toBe(source.a);
119
+ expect(target.b).toBe(source.b);
120
+ expect(target.c).toBe(source.c);
121
+ });
60
122
  });
61
123
  describe("should throw an error", function () {
62
124
  it("when target is not an array or object", function () {
package/use/listFilter.js CHANGED
@@ -42,6 +42,8 @@ export function useListFilter({
42
42
  excludedValues = {},
43
43
  allowedFilter,
44
44
  excludedFilter,
45
+ customIndexOptions = {},
46
+ customSearchOptions = {},
45
47
  }) {
46
48
  const state = reactive({
47
49
  objectIndexes: {},
@@ -77,7 +79,7 @@ export function useListFilter({
77
79
  state[key] = toRef(parentState, key);
78
80
  }
79
81
  if (useTextSearch) {
80
- textSearchIndex = useSearch();
82
+ textSearchIndex = useSearch(customIndexOptions, customSearchOptions);
81
83
  textSearchIndex.state.search = toRef(state, "textSearchValue");
82
84
  state.searched = toRef(textSearchIndex.state, "searched");
83
85
  state.searching = toRef(textSearchIndex.state, "searching");
@@ -2,6 +2,7 @@ import { keyDiff } from "./keyDiff.js";
2
2
  import inspect from "browser-util-inspect";
3
3
  import isArray from "lodash-es/isArray.js";
4
4
  import isObject from "lodash-es/isObject.js";
5
+ import isObjectLike from "lodash-es/isObjectLike.js";
5
6
  import { isReactive, isRef, toRef, unref } from "vue";
6
7
 
7
8
  /**
@@ -89,6 +90,16 @@ function reactiveReplaceKeys(target, source, keys, exclude = []) {
89
90
  for (const key of keys) {
90
91
  if (!exclude.includes(key)) {
91
92
  if (targetIsReactive && sourceIsReactive) {
93
+ const targetPropRaw = unref(toRef(target, key));
94
+ // if they are object like we can see if the values are the same
95
+ if (isObjectLike(targetPropRaw)) {
96
+ const sourcePropRaw = unref(toRef(source, key));
97
+ if (isObjectLike(sourcePropRaw)) {
98
+ if (targetPropRaw === sourcePropRaw) {
99
+ continue;
100
+ }
101
+ }
102
+ }
92
103
  target[key] = toRef(source, key);
93
104
  didAnything = true;
94
105
  } else if (target[key] !== source[key]) {
@@ -226,7 +237,7 @@ export function assignReactiveObject(target, source, exclude = []) {
226
237
  ({ target, source } = validateTargetAndSource(target, source));
227
238
  const { addedKeys, sameKeys, removedKeys } = keyDiff(Object.keys(source) || [], Object.keys(target) || []);
228
239
  let didAnything = false;
229
- didAnything |= trimReactiveObject(target, removedKeys, exclude);
240
+ didAnything |= trimReactiveObject(target, source, exclude, removedKeys);
230
241
  didAnything |= addOrUpdateReactiveObject(target, source, exclude, addedKeys, sameKeys);
231
242
  return didAnything;
232
243
  }
@@ -290,7 +301,7 @@ function recursiveInner(target, source, exclude, addedKeys, sameKeys, path, fn)
290
301
  function assignReactiveObjectRecursive(target, source, exclude = [], path = "") {
291
302
  let { addedKeys, sameKeys, removedKeys } = keyDiff(Object.keys(source) || [], Object.keys(target) || []);
292
303
  let didAnything = false;
293
- didAnything |= trimReactiveObject(target, removedKeys, exclude);
304
+ didAnything |= trimReactiveObject(target, source, exclude, removedKeys);
294
305
  didAnything |= recursiveInner(target, source, exclude, addedKeys, sameKeys, path, assignReactiveObjectRecursive);
295
306
  return didAnything;
296
307
  }