Skip to content

Commit 128c639

Browse files
authored
fix PATCH_DOC #320
2 parents 742159f + e9390ab commit 128c639

File tree

6 files changed

+216
-75
lines changed

6 files changed

+216
-75
lines changed

dist/index.cjs.js

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ var vuexEasyAccess = require('vuex-easy-access');
1111
var isWhat = require('is-what');
1212
var copy = _interopDefault(require('copy-anything'));
1313
var mergeAnything = require('merge-anything');
14-
var flatten = _interopDefault(require('flatten-anything'));
14+
var flatten = require('flatten-anything');
15+
var flatten__default = _interopDefault(flatten);
16+
var pathToProp = _interopDefault(require('path-to-prop'));
1517
var compareAnything = require('compare-anything');
1618
var findAndReplaceAnything = require('find-and-replace-anything');
1719
var filter = _interopDefault(require('filter-anything'));
@@ -329,6 +331,28 @@ function isIncrementHelper(payload) {
329331
payload.isIncrementHelper === true);
330332
}
331333

334+
/**
335+
* Creates the params needed to $set a target based on a nested.path
336+
*
337+
* @param {object} target
338+
* @param {string} path
339+
* @param {*} value
340+
* @returns {[object, string, any]}
341+
*/
342+
function getSetParams(target, path, value) {
343+
var _a;
344+
var pathParts = path.split('.');
345+
var prop = pathParts.pop();
346+
var pathParent = pathParts.join('.');
347+
var targetForNestedProp = pathToProp(target, pathParent);
348+
if (targetForNestedProp === undefined) {
349+
// the target doesn't have an object ready at this level to set the value to
350+
// so we need to step down a level and try again
351+
return getSetParams(target, pathParent, (_a = {}, _a[prop] = value, _a));
352+
}
353+
var valueToSet = value;
354+
return [targetForNestedProp, prop, valueToSet];
355+
}
332356
/**
333357
* a function returning the mutations object
334358
*
@@ -402,29 +426,38 @@ function pluginMutations (userState) {
402426
}
403427
},
404428
PATCH_DOC: function (state, patches) {
405-
var _this = this;
429+
var _a;
406430
// Get the state prop ref
407431
var ref = state._conf.statePropName ? state[state._conf.statePropName] : state;
408432
if (state._conf.firestoreRefType.toLowerCase() === 'collection') {
409433
ref = ref[patches.id];
410434
}
411435
if (!ref)
412436
return error('patch-no-ref');
413-
return Object.keys(patches).forEach(function (key) {
414-
var newVal = patches[key];
415-
// Merge if exists
416-
function helpers(originVal, newVal) {
417-
if (isWhat.isArray(originVal) && isArrayHelper(newVal)) {
418-
newVal = newVal.executeOn(originVal);
419-
}
420-
if (isWhat.isNumber(originVal) && isIncrementHelper(newVal)) {
421-
newVal = newVal.executeOn(originVal);
422-
}
423-
return newVal; // always return newVal as fallback!!
437+
function convertHelpers(originVal, newVal) {
438+
if (isWhat.isArray(originVal) && isArrayHelper(newVal)) {
439+
newVal = newVal.executeOn(originVal);
424440
}
425-
newVal = mergeAnything.merge({ extensions: [helpers] }, ref[key], patches[key]);
426-
_this._vm.$set(ref, key, newVal);
427-
});
441+
if (isWhat.isNumber(originVal) && isIncrementHelper(newVal)) {
442+
newVal = newVal.executeOn(originVal);
443+
}
444+
return newVal; // always return newVal as fallback!!
445+
}
446+
// const refPropsPicked = filter(ref, Object.keys(patches))
447+
// const patchesSanitised = merge({ extensions: [convertHelpers] }, refPropsPicked, patches)
448+
var patchesFlat = flatten.flattenObject(patches);
449+
for (var _i = 0, _b = Object.entries(patchesFlat); _i < _b.length; _i++) {
450+
var _c = _b[_i], path = _c[0], value = _c[1];
451+
var targetVal = pathToProp(ref, path);
452+
var newVal = convertHelpers(targetVal, value);
453+
// do not update anything if the values are the same
454+
// this is technically not required, because vue takes care of this as well:
455+
if (targetVal === newVal)
456+
continue;
457+
// update just the nested value
458+
var setParams = getSetParams(ref, path, newVal);
459+
(_a = this._vm).$set.apply(_a, setParams);
460+
}
428461
},
429462
DELETE_DOC: function (state, id) {
430463
if (state._conf.firestoreRefType.toLowerCase() !== 'collection')
@@ -1286,9 +1319,9 @@ function pluginActions (Firebase) {
12861319
var getters = _a.getters, commit = _a.commit;
12871320
var defaultValues = getters.defaultValues;
12881321
var searchTarget = getters.collectionMode ? getters.storeRef[doc.id] : getters.storeRef;
1289-
var compareInfo = compareAnything.compareObjectProps(flatten(doc), // presentIn 0
1290-
flatten(defaultValues), // presentIn 1
1291-
flatten(searchTarget) // presentIn 2
1322+
var compareInfo = compareAnything.compareObjectProps(flatten__default(doc), // presentIn 0
1323+
flatten__default(defaultValues), // presentIn 1
1324+
flatten__default(searchTarget) // presentIn 2
12921325
);
12931326
Object.keys(compareInfo.presentIn).forEach(function (prop) {
12941327
// don't worry about props not in fillables
@@ -1918,7 +1951,7 @@ function pluginGetters (Firebase) {
19181951
patchData.updated_by = state._sync.userId;
19191952
// clean up item
19201953
var cleanedPatchData = filter(patchData, getters.fillables, getters.guard);
1921-
var itemToUpdate = flatten(cleanedPatchData);
1954+
var itemToUpdate = flatten__default(cleanedPatchData);
19221955
// add id (required to get ref later at apiHelpers.ts)
19231956
// @ts-ignore
19241957
itemToUpdate.id = id;

dist/index.esm.js

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { getDeepRef, getKeysFromPath } from 'vuex-easy-access';
55
import { isAnyObject, isPlainObject, isArray, isFunction, isNumber, isString, isDate } from 'is-what';
66
import copy from 'copy-anything';
77
import { merge } from 'merge-anything';
8-
import flatten from 'flatten-anything';
8+
import flatten, { flattenObject } from 'flatten-anything';
9+
import pathToProp from 'path-to-prop';
910
import { compareObjectProps } from 'compare-anything';
1011
import { findAndReplace, findAndReplaceIf } from 'find-and-replace-anything';
1112
import filter from 'filter-anything';
@@ -323,6 +324,28 @@ function isIncrementHelper(payload) {
323324
payload.isIncrementHelper === true);
324325
}
325326

327+
/**
328+
* Creates the params needed to $set a target based on a nested.path
329+
*
330+
* @param {object} target
331+
* @param {string} path
332+
* @param {*} value
333+
* @returns {[object, string, any]}
334+
*/
335+
function getSetParams(target, path, value) {
336+
var _a;
337+
var pathParts = path.split('.');
338+
var prop = pathParts.pop();
339+
var pathParent = pathParts.join('.');
340+
var targetForNestedProp = pathToProp(target, pathParent);
341+
if (targetForNestedProp === undefined) {
342+
// the target doesn't have an object ready at this level to set the value to
343+
// so we need to step down a level and try again
344+
return getSetParams(target, pathParent, (_a = {}, _a[prop] = value, _a));
345+
}
346+
var valueToSet = value;
347+
return [targetForNestedProp, prop, valueToSet];
348+
}
326349
/**
327350
* a function returning the mutations object
328351
*
@@ -396,29 +419,38 @@ function pluginMutations (userState) {
396419
}
397420
},
398421
PATCH_DOC: function (state, patches) {
399-
var _this = this;
422+
var _a;
400423
// Get the state prop ref
401424
var ref = state._conf.statePropName ? state[state._conf.statePropName] : state;
402425
if (state._conf.firestoreRefType.toLowerCase() === 'collection') {
403426
ref = ref[patches.id];
404427
}
405428
if (!ref)
406429
return error('patch-no-ref');
407-
return Object.keys(patches).forEach(function (key) {
408-
var newVal = patches[key];
409-
// Merge if exists
410-
function helpers(originVal, newVal) {
411-
if (isArray(originVal) && isArrayHelper(newVal)) {
412-
newVal = newVal.executeOn(originVal);
413-
}
414-
if (isNumber(originVal) && isIncrementHelper(newVal)) {
415-
newVal = newVal.executeOn(originVal);
416-
}
417-
return newVal; // always return newVal as fallback!!
430+
function convertHelpers(originVal, newVal) {
431+
if (isArray(originVal) && isArrayHelper(newVal)) {
432+
newVal = newVal.executeOn(originVal);
418433
}
419-
newVal = merge({ extensions: [helpers] }, ref[key], patches[key]);
420-
_this._vm.$set(ref, key, newVal);
421-
});
434+
if (isNumber(originVal) && isIncrementHelper(newVal)) {
435+
newVal = newVal.executeOn(originVal);
436+
}
437+
return newVal; // always return newVal as fallback!!
438+
}
439+
// const refPropsPicked = filter(ref, Object.keys(patches))
440+
// const patchesSanitised = merge({ extensions: [convertHelpers] }, refPropsPicked, patches)
441+
var patchesFlat = flattenObject(patches);
442+
for (var _i = 0, _b = Object.entries(patchesFlat); _i < _b.length; _i++) {
443+
var _c = _b[_i], path = _c[0], value = _c[1];
444+
var targetVal = pathToProp(ref, path);
445+
var newVal = convertHelpers(targetVal, value);
446+
// do not update anything if the values are the same
447+
// this is technically not required, because vue takes care of this as well:
448+
if (targetVal === newVal)
449+
continue;
450+
// update just the nested value
451+
var setParams = getSetParams(ref, path, newVal);
452+
(_a = this._vm).$set.apply(_a, setParams);
453+
}
422454
},
423455
DELETE_DOC: function (state, id) {
424456
if (state._conf.firestoreRefType.toLowerCase() !== 'collection')

package-lock.json

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"flatten-anything": "^1.4.1",
4646
"is-what": "^3.8.0",
4747
"merge-anything": "^2.4.4",
48+
"path-to-prop": "0.0.3",
4849
"vuex-easy-access": "^3.1.8"
4950
},
5051
"devDependencies": {
@@ -71,8 +72,14 @@
7172
},
7273
"ava": {
7374
"compileEnhancements": false,
74-
"extensions": ["ts"],
75-
"require": ["ts-node/register"],
76-
"helpers": ["**/helpers/**/*"]
75+
"extensions": [
76+
"ts"
77+
],
78+
"require": [
79+
"ts-node/register"
80+
],
81+
"helpers": [
82+
"**/helpers/**/*"
83+
]
7784
}
7885
}

src/module/mutations.ts

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { isArray, isFunction, isNumber } from 'is-what'
22
import { getDeepRef } from 'vuex-easy-access'
3+
import { flattenObject } from 'flatten-anything'
4+
import pathToProp from 'path-to-prop'
35
import logError from './errors'
46
import copy from 'copy-anything'
57
import { merge } from 'merge-anything'
@@ -8,6 +10,38 @@ import { isArrayHelper } from '../utils/arrayHelpers'
810
import { isIncrementHelper } from '../utils/incrementHelper'
911
import getStateWithSync from './state'
1012

13+
function convertHelpers (originVal, newVal) {
14+
if (isArray(originVal) && isArrayHelper(newVal)) {
15+
newVal = newVal.executeOn(originVal)
16+
}
17+
if (isNumber(originVal) && isIncrementHelper(newVal)) {
18+
newVal = newVal.executeOn(originVal)
19+
}
20+
return newVal // always return newVal as fallback!!
21+
}
22+
23+
/**
24+
* Creates the params needed to $set a target based on a nested.path
25+
*
26+
* @param {object} target
27+
* @param {string} path
28+
* @param {*} value
29+
* @returns {[object, string, any]}
30+
*/
31+
function getSetParams (target: object, path: string, value: any): [object, string, any] {
32+
const pathParts = path.split('.')
33+
const prop = pathParts.pop()
34+
const pathParent = pathParts.join('.')
35+
const targetForNestedProp = pathToProp(target, pathParent)
36+
if (targetForNestedProp === undefined) {
37+
// the target doesn't have an object ready at this level to set the value to
38+
// so we need to step down a level and try again
39+
return getSetParams(target, pathParent, { [prop]: value })
40+
}
41+
const valueToSet = value
42+
return [targetForNestedProp, prop, valueToSet]
43+
}
44+
1145
/**
1246
* a function returning the mutations object
1347
*
@@ -79,21 +113,18 @@ export default function (userState: object): AnyObject {
79113
ref = ref[patches.id]
80114
}
81115
if (!ref) return logError('patch-no-ref')
82-
return Object.keys(patches).forEach(key => {
83-
let newVal = patches[key]
84-
// Merge if exists
85-
function helpers (originVal, newVal) {
86-
if (isArray(originVal) && isArrayHelper(newVal)) {
87-
newVal = newVal.executeOn(originVal)
88-
}
89-
if (isNumber(originVal) && isIncrementHelper(newVal)) {
90-
newVal = newVal.executeOn(originVal)
91-
}
92-
return newVal // always return newVal as fallback!!
93-
}
94-
newVal = merge({ extensions: [helpers] }, ref[key], patches[key])
95-
this._vm.$set(ref, key, newVal)
96-
})
116+
117+
const patchesFlat = flattenObject(patches)
118+
for (const [path, value] of Object.entries(patchesFlat)) {
119+
const targetVal = pathToProp(ref, path)
120+
const newVal = convertHelpers(targetVal, value)
121+
// do not update anything if the values are the same
122+
// this is technically not required, because vue takes care of this as well:
123+
if (targetVal === newVal) continue
124+
// update just the nested value
125+
const setParams = getSetParams(ref, path, newVal)
126+
this._vm.$set(...setParams)
127+
}
97128
},
98129
DELETE_DOC (state, id) {
99130
if (state._conf.firestoreRefType.toLowerCase() !== 'collection') return

0 commit comments

Comments
 (0)