Skip to content

Commit

Permalink
Removed lodash.merge in favor of deepmerge
Browse files Browse the repository at this point in the history
  • Loading branch information
Ap3rtur3 committed Dec 8, 2019
1 parent c859810 commit c7b4ebd
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ this.$hydrate({ /* config */ });

## Frameworks

_This section is in progress_
_This section is in progress..._

#### Laravel

Expand Down
20 changes: 11 additions & 9 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"decoupled"
],
"dependencies": {
"lodash.merge": "^4.6.2"
"deepmerge": "^4.2.2"
},
"devDependencies": {
"@vue/cli-plugin-babel": "^3.8.0",
Expand Down
9 changes: 4 additions & 5 deletions src/lib.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import merge from 'lodash.merge';
import merge from 'deepmerge';

let isSilent = false;
const isTestMode = (process && process.env && process.env.NODE_ENV === 'test');
Expand Down Expand Up @@ -39,8 +39,7 @@ const fetchData = ({ id, name }) => {
* @returns {{}}
*/
const mergeStateWithData = (rootState, data) => {
const newState = {};
merge(newState, rootState);
const newState = merge({}, rootState);
return Object.keys(data)
.reduce((newState, moduleName) => {
if (!data.hasOwnProperty(moduleName)) {
Expand All @@ -50,12 +49,12 @@ const mergeStateWithData = (rootState, data) => {
const moduleState = data[moduleName];
if (moduleName === ROOT_MODULE_NAME) {
// Merge with root state
merge(newState, moduleState);
newState = merge(newState, moduleState);
} else {
// Build nested module state and merge with new state
const moduleParts = moduleName.split('/').reverse();
const moduleObj = moduleParts.reduce((obj, name) => ({ [name]: obj }), moduleState);
merge(newState, moduleObj);
newState = merge(newState, moduleObj);
}
return newState;
}, newState);
Expand Down
49 changes: 31 additions & 18 deletions tests/unit/hydra.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,38 +78,51 @@ describe('Hydra', () => {
});

it('merges data with root state', () => {
const test1 = 'test1';
const test2 = 'test2';
const rootState = { test1 };
const data = { root: { test2 } };
setupStore(rootState);
const test1 = 'stay';
const test2 = 'overwrite';
const test3 = 'remove';
const data = { root: { test2, test3: undefined } };
setupStore({ test1, test2: null, test3 });
vm.$hydrate({ data });
expect(vm.$store.state.test1).toEqual(test1);
expect(vm.$store.state.test2).toEqual(test2);
expect(vm.$store.state.test3).toBeUndefined();
});

it('merges data with module state', () => {
const test1 = 'test1';
const test2 = 'test2';
const test3 = 'test3';
const rootState = { test1 };
const moduleState = { test2, test3: null };
const data = { module: { test3 } };
setupStore(rootState, { module: moduleState });
const test1 = 'stay';
const test2 = 'overwrite';
const test3 = 'remove';
const data = { module: { test2, test3: undefined } };
setupStore({ test1 }, {
module: { test1, test2: null, test3 }
});
vm.$hydrate({ data });
expect(vm.$store.state.test1).toEqual(test1);
expect(vm.$store.state.test3).toBeUndefined();
expect(vm.$store.state.module.test1).toEqual(test1);
expect(vm.$store.state.module.test2).toEqual(test2);
expect(vm.$store.state.module.test3).toEqual(test3);
expect(vm.$store.state.module.test3).toBeUndefined();
});

it('merges nested module state', () => {
const test1 = 'test1';
const test2 = 'test2';
const moduleState = { test1 };
const data = { 'nested/module': { test2 } };
setupStore({}, { nested: { module: moduleState } });
const test1 = 'stay';
const test2 = 'overwrite';
const test3 = 'remove';
const data = {
'nested/module': { test2, test3: undefined },
'really/nested/module': { test2, test3: undefined }
};
setupStore({}, {
nested: { module: { test1, test2: null, test3 } },
really: { nested: { module: { test1, test2: null, test3 } } },
});
vm.$hydrate({ data });
expect(vm.$store.state.nested.module.test1).toEqual(test1);
expect(vm.$store.state.nested.module.test2).toEqual(test2);
expect(vm.$store.state.nested.module.test3).toBeUndefined();
expect(vm.$store.state.really.nested.module.test1).toEqual(test1);
expect(vm.$store.state.really.nested.module.test2).toEqual(test2);
expect(vm.$store.state.really.nested.module.test3).toBeUndefined();
});
});

0 comments on commit c7b4ebd

Please sign in to comment.