Skip to content

Commit

Permalink
refactor(dependency): migrate deepmerge to ts-deepmerge
Browse files Browse the repository at this point in the history
Migrated the deepmerge library to ts-deepmerge for better TypeScript compatibility and enhanced type checking.

fixes btroncone#229
  • Loading branch information
NeizanSenpai committed Aug 5, 2023
1 parent ce3f480 commit cfedc12
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 deletions.
19 changes: 17 additions & 2 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 @@ -22,8 +22,8 @@
"@angular/platform-browser-dynamic": "^16.0.1",
"@angular/router": "^16.0.1",
"@ngrx/store": "^16.0.0",
"deepmerge": "^4.2.2",
"rxjs": "~7.5.5",
"ts-deepmerge": "^6.2.0",
"tslib": "^2.3.0",
"zone.js": "~0.13.0"
},
Expand Down
2 changes: 1 addition & 1 deletion projects/lib/ng-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"entryFile": "src/public_api.ts"
},
"allowedNonPeerDependencies": [
"deepmerge"
"ts-deepmerge"
]
}
2 changes: 1 addition & 1 deletion projects/lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@ngrx/store": "^16.0.0"
},
"dependencies": {
"deepmerge": "^4.2.2",
"ts-deepmerge": "^6.2.0",
"tslib": "^2.3.0"
}
}
11 changes: 6 additions & 5 deletions projects/lib/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import deepmerge from 'deepmerge';
import merge from 'ts-deepmerge';

// Cannot import from the @ngrx/store package due to a module resolution issue.
// See Issue #206.
Expand Down Expand Up @@ -226,12 +226,13 @@ export const syncStateUpdate = (
// Default merge strategy is a full deep merge.
export const defaultMergeReducer = (state: any, rehydratedState: any, action: any) => {
if ((action.type === INIT_ACTION || action.type === UPDATE_ACTION) && rehydratedState) {
const overwriteMerge = (destinationArray: any, sourceArray: any, options: any) => sourceArray;
const options: deepmerge.Options = {
arrayMerge: overwriteMerge,
const options = {
allowUndefinedOverrides: false,
mergeArrays: true,
uniqueArrayItems: true
};

state = deepmerge(state, rehydratedState, options);
state = merge.withOptions(options, rehydratedState, state);
}

return state;
Expand Down
12 changes: 8 additions & 4 deletions spec/index_spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
declare var it, describe, expect;
require('es6-shim');
import * as CryptoJS from 'crypto-js';
import deepmerge from 'deepmerge';
import merge from 'ts-deepmerge';
import 'localstorage-polyfill';
import { dateReviver, localStorageSync, rehydrateApplicationState, syncStateUpdate } from '../projects/lib/src/public_api';

Expand Down Expand Up @@ -525,14 +525,18 @@ describe('ngrxLocalStorage', () => {
const reducer = (state = initialState, action) => state;
const mergeReducer = (state, rehydratedState, action) => {
// Perform a merge where we only want a single property from feature1
// but a deepmerge with feature2

// but a merge with feature2
const options = {
allowUndefinedOverrides: false,
mergeArrays: true,
uniqueArrayItems: false
};
return {
...state,
feature1: {
slice11: rehydratedState.feature1.slice11,
},
feature2: deepmerge(state.feature2, rehydratedState.feature2),
feature2: merge.withOptions(options, rehydratedState.feature2, state.feature2),
};
};
const metaReducer = localStorageSync({
Expand Down

0 comments on commit cfedc12

Please sign in to comment.