Skip to content

Commit

Permalink
fix PATCH_DOC #320
Browse files Browse the repository at this point in the history
  • Loading branch information
mesqueeb authored Apr 22, 2020
2 parents 742159f + e9390ab commit 128c639
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 75 deletions.
73 changes: 53 additions & 20 deletions dist/index.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ var vuexEasyAccess = require('vuex-easy-access');
var isWhat = require('is-what');
var copy = _interopDefault(require('copy-anything'));
var mergeAnything = require('merge-anything');
var flatten = _interopDefault(require('flatten-anything'));
var flatten = require('flatten-anything');
var flatten__default = _interopDefault(flatten);
var pathToProp = _interopDefault(require('path-to-prop'));
var compareAnything = require('compare-anything');
var findAndReplaceAnything = require('find-and-replace-anything');
var filter = _interopDefault(require('filter-anything'));
Expand Down Expand Up @@ -329,6 +331,28 @@ function isIncrementHelper(payload) {
payload.isIncrementHelper === true);
}

/**
* Creates the params needed to $set a target based on a nested.path
*
* @param {object} target
* @param {string} path
* @param {*} value
* @returns {[object, string, any]}
*/
function getSetParams(target, path, value) {
var _a;
var pathParts = path.split('.');
var prop = pathParts.pop();
var pathParent = pathParts.join('.');
var targetForNestedProp = pathToProp(target, pathParent);
if (targetForNestedProp === undefined) {
// the target doesn't have an object ready at this level to set the value to
// so we need to step down a level and try again
return getSetParams(target, pathParent, (_a = {}, _a[prop] = value, _a));
}
var valueToSet = value;
return [targetForNestedProp, prop, valueToSet];
}
/**
* a function returning the mutations object
*
Expand Down Expand Up @@ -402,29 +426,38 @@ function pluginMutations (userState) {
}
},
PATCH_DOC: function (state, patches) {
var _this = this;
var _a;
// Get the state prop ref
var ref = state._conf.statePropName ? state[state._conf.statePropName] : state;
if (state._conf.firestoreRefType.toLowerCase() === 'collection') {
ref = ref[patches.id];
}
if (!ref)
return error('patch-no-ref');
return Object.keys(patches).forEach(function (key) {
var newVal = patches[key];
// Merge if exists
function helpers(originVal, newVal) {
if (isWhat.isArray(originVal) && isArrayHelper(newVal)) {
newVal = newVal.executeOn(originVal);
}
if (isWhat.isNumber(originVal) && isIncrementHelper(newVal)) {
newVal = newVal.executeOn(originVal);
}
return newVal; // always return newVal as fallback!!
function convertHelpers(originVal, newVal) {
if (isWhat.isArray(originVal) && isArrayHelper(newVal)) {
newVal = newVal.executeOn(originVal);
}
newVal = mergeAnything.merge({ extensions: [helpers] }, ref[key], patches[key]);
_this._vm.$set(ref, key, newVal);
});
if (isWhat.isNumber(originVal) && isIncrementHelper(newVal)) {
newVal = newVal.executeOn(originVal);
}
return newVal; // always return newVal as fallback!!
}
// const refPropsPicked = filter(ref, Object.keys(patches))
// const patchesSanitised = merge({ extensions: [convertHelpers] }, refPropsPicked, patches)
var patchesFlat = flatten.flattenObject(patches);
for (var _i = 0, _b = Object.entries(patchesFlat); _i < _b.length; _i++) {
var _c = _b[_i], path = _c[0], value = _c[1];
var targetVal = pathToProp(ref, path);
var newVal = convertHelpers(targetVal, value);
// do not update anything if the values are the same
// this is technically not required, because vue takes care of this as well:
if (targetVal === newVal)
continue;
// update just the nested value
var setParams = getSetParams(ref, path, newVal);
(_a = this._vm).$set.apply(_a, setParams);
}
},
DELETE_DOC: function (state, id) {
if (state._conf.firestoreRefType.toLowerCase() !== 'collection')
Expand Down Expand Up @@ -1286,9 +1319,9 @@ function pluginActions (Firebase) {
var getters = _a.getters, commit = _a.commit;
var defaultValues = getters.defaultValues;
var searchTarget = getters.collectionMode ? getters.storeRef[doc.id] : getters.storeRef;
var compareInfo = compareAnything.compareObjectProps(flatten(doc), // presentIn 0
flatten(defaultValues), // presentIn 1
flatten(searchTarget) // presentIn 2
var compareInfo = compareAnything.compareObjectProps(flatten__default(doc), // presentIn 0
flatten__default(defaultValues), // presentIn 1
flatten__default(searchTarget) // presentIn 2
);
Object.keys(compareInfo.presentIn).forEach(function (prop) {
// don't worry about props not in fillables
Expand Down Expand Up @@ -1918,7 +1951,7 @@ function pluginGetters (Firebase) {
patchData.updated_by = state._sync.userId;
// clean up item
var cleanedPatchData = filter(patchData, getters.fillables, getters.guard);
var itemToUpdate = flatten(cleanedPatchData);
var itemToUpdate = flatten__default(cleanedPatchData);
// add id (required to get ref later at apiHelpers.ts)
// @ts-ignore
itemToUpdate.id = id;
Expand Down
64 changes: 48 additions & 16 deletions dist/index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { getDeepRef, getKeysFromPath } from 'vuex-easy-access';
import { isAnyObject, isPlainObject, isArray, isFunction, isNumber, isString, isDate } from 'is-what';
import copy from 'copy-anything';
import { merge } from 'merge-anything';
import flatten from 'flatten-anything';
import flatten, { flattenObject } from 'flatten-anything';
import pathToProp from 'path-to-prop';
import { compareObjectProps } from 'compare-anything';
import { findAndReplace, findAndReplaceIf } from 'find-and-replace-anything';
import filter from 'filter-anything';
Expand Down Expand Up @@ -323,6 +324,28 @@ function isIncrementHelper(payload) {
payload.isIncrementHelper === true);
}

/**
* Creates the params needed to $set a target based on a nested.path
*
* @param {object} target
* @param {string} path
* @param {*} value
* @returns {[object, string, any]}
*/
function getSetParams(target, path, value) {
var _a;
var pathParts = path.split('.');
var prop = pathParts.pop();
var pathParent = pathParts.join('.');
var targetForNestedProp = pathToProp(target, pathParent);
if (targetForNestedProp === undefined) {
// the target doesn't have an object ready at this level to set the value to
// so we need to step down a level and try again
return getSetParams(target, pathParent, (_a = {}, _a[prop] = value, _a));
}
var valueToSet = value;
return [targetForNestedProp, prop, valueToSet];
}
/**
* a function returning the mutations object
*
Expand Down Expand Up @@ -396,29 +419,38 @@ function pluginMutations (userState) {
}
},
PATCH_DOC: function (state, patches) {
var _this = this;
var _a;
// Get the state prop ref
var ref = state._conf.statePropName ? state[state._conf.statePropName] : state;
if (state._conf.firestoreRefType.toLowerCase() === 'collection') {
ref = ref[patches.id];
}
if (!ref)
return error('patch-no-ref');
return Object.keys(patches).forEach(function (key) {
var newVal = patches[key];
// Merge if exists
function helpers(originVal, newVal) {
if (isArray(originVal) && isArrayHelper(newVal)) {
newVal = newVal.executeOn(originVal);
}
if (isNumber(originVal) && isIncrementHelper(newVal)) {
newVal = newVal.executeOn(originVal);
}
return newVal; // always return newVal as fallback!!
function convertHelpers(originVal, newVal) {
if (isArray(originVal) && isArrayHelper(newVal)) {
newVal = newVal.executeOn(originVal);
}
newVal = merge({ extensions: [helpers] }, ref[key], patches[key]);
_this._vm.$set(ref, key, newVal);
});
if (isNumber(originVal) && isIncrementHelper(newVal)) {
newVal = newVal.executeOn(originVal);
}
return newVal; // always return newVal as fallback!!
}
// const refPropsPicked = filter(ref, Object.keys(patches))
// const patchesSanitised = merge({ extensions: [convertHelpers] }, refPropsPicked, patches)
var patchesFlat = flattenObject(patches);
for (var _i = 0, _b = Object.entries(patchesFlat); _i < _b.length; _i++) {
var _c = _b[_i], path = _c[0], value = _c[1];
var targetVal = pathToProp(ref, path);
var newVal = convertHelpers(targetVal, value);
// do not update anything if the values are the same
// this is technically not required, because vue takes care of this as well:
if (targetVal === newVal)
continue;
// update just the nested value
var setParams = getSetParams(ref, path, newVal);
(_a = this._vm).$set.apply(_a, setParams);
}
},
DELETE_DOC: function (state, id) {
if (state._conf.firestoreRefType.toLowerCase() !== 'collection')
Expand Down
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"flatten-anything": "^1.4.1",
"is-what": "^3.8.0",
"merge-anything": "^2.4.4",
"path-to-prop": "0.0.3",
"vuex-easy-access": "^3.1.8"
},
"devDependencies": {
Expand All @@ -71,8 +72,14 @@
},
"ava": {
"compileEnhancements": false,
"extensions": ["ts"],
"require": ["ts-node/register"],
"helpers": ["**/helpers/**/*"]
"extensions": [
"ts"
],
"require": [
"ts-node/register"
],
"helpers": [
"**/helpers/**/*"
]
}
}
61 changes: 46 additions & 15 deletions src/module/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { isArray, isFunction, isNumber } from 'is-what'
import { getDeepRef } from 'vuex-easy-access'
import { flattenObject } from 'flatten-anything'
import pathToProp from 'path-to-prop'
import logError from './errors'
import copy from 'copy-anything'
import { merge } from 'merge-anything'
Expand All @@ -8,6 +10,38 @@ import { isArrayHelper } from '../utils/arrayHelpers'
import { isIncrementHelper } from '../utils/incrementHelper'
import getStateWithSync from './state'

function convertHelpers (originVal, newVal) {
if (isArray(originVal) && isArrayHelper(newVal)) {
newVal = newVal.executeOn(originVal)
}
if (isNumber(originVal) && isIncrementHelper(newVal)) {
newVal = newVal.executeOn(originVal)
}
return newVal // always return newVal as fallback!!
}

/**
* Creates the params needed to $set a target based on a nested.path
*
* @param {object} target
* @param {string} path
* @param {*} value
* @returns {[object, string, any]}
*/
function getSetParams (target: object, path: string, value: any): [object, string, any] {
const pathParts = path.split('.')
const prop = pathParts.pop()
const pathParent = pathParts.join('.')
const targetForNestedProp = pathToProp(target, pathParent)
if (targetForNestedProp === undefined) {
// the target doesn't have an object ready at this level to set the value to
// so we need to step down a level and try again
return getSetParams(target, pathParent, { [prop]: value })
}
const valueToSet = value
return [targetForNestedProp, prop, valueToSet]
}

/**
* a function returning the mutations object
*
Expand Down Expand Up @@ -79,21 +113,18 @@ export default function (userState: object): AnyObject {
ref = ref[patches.id]
}
if (!ref) return logError('patch-no-ref')
return Object.keys(patches).forEach(key => {
let newVal = patches[key]
// Merge if exists
function helpers (originVal, newVal) {
if (isArray(originVal) && isArrayHelper(newVal)) {
newVal = newVal.executeOn(originVal)
}
if (isNumber(originVal) && isIncrementHelper(newVal)) {
newVal = newVal.executeOn(originVal)
}
return newVal // always return newVal as fallback!!
}
newVal = merge({ extensions: [helpers] }, ref[key], patches[key])
this._vm.$set(ref, key, newVal)
})

const patchesFlat = flattenObject(patches)
for (const [path, value] of Object.entries(patchesFlat)) {
const targetVal = pathToProp(ref, path)
const newVal = convertHelpers(targetVal, value)
// do not update anything if the values are the same
// this is technically not required, because vue takes care of this as well:
if (targetVal === newVal) continue
// update just the nested value
const setParams = getSetParams(ref, path, newVal)
this._vm.$set(...setParams)
}
},
DELETE_DOC (state, id) {
if (state._conf.firestoreRefType.toLowerCase() !== 'collection') return
Expand Down
Loading

0 comments on commit 128c639

Please sign in to comment.