Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add stateMergeFunction to config #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ An interface defining the configuration attributes to bootstrap `localStorageSyn
* `storage` (optional) `Storage`: Specify an object that conforms to the [Storage interface](https://github.com/Microsoft/TypeScript/blob/master/lib/lib.dom.d.ts#L9708) to use, this will default to `localStorage`.
* `removeOnUndefined` (optional) `boolean`: Specify if the state is removed from the storage when the new value is undefined, this will default to `false`.
* `storageKeySerializer` (optional) `(key: string) => string`: Сustom serialize function for storage keys, used to avoid Storage conflicts.
* `stateMergeFunction` (optional) `(state: any, rehydratedState: any) => any`: A custom state merge function. By default, the `Object.merge({}, [..])` method is used, which does not take nested keys into account. This allows for employing libraries such as `deepmerge`, which gracefully handles nested objects, should your use-case require this behaviour.
* `restoreDates` \(*boolean? = true*): Restore serialized date objects. If you work directly with ISO date strings, set this option to `false`.
* `syncCondition` (optional) `(state) => boolean`: When set, sync to storage medium will only occur when this function returns a true boolean. Example: `(state) => state.config.syncToStorage` will check the state tree under config.syncToStorage and if true, it will sync to the storage. If undefined or false it will not sync to storage. Often useful for "remember me" options in login.
Usage: `localStorageSync({keys: ['todos', 'visibilityFilter'], storageKeySerializer: (key) => 'cool_' + key, ... })`. In this example `Storage` will use keys `cool_todos` and `cool_visibilityFilter` keys to store `todos` and `visibilityFilter` slices of state). The key itself is used by default - `(key) => key`.
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export const localStorageSync = (config: LocalStorageConfig) => (
(action.type === INIT_ACTION || action.type === UPDATE_ACTION) &&
rehydratedState
) {
state = Object.assign({}, state, rehydratedState);
state = config.stateMergeFunction ? config.stateMergeFunction(state, rehydratedState) : Object.assign({}, state, rehydratedState);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case project maintainers decide to accept this concept, this ternary should instead check whether config.stateMergeFunction is callable: typeof config.stateMergeFunction === 'function'

}
const nextState = reducer(state, action);
syncStateUpdate(
Expand Down Expand Up @@ -291,4 +291,5 @@ export interface LocalStorageConfig {
restoreDates?: boolean;
storageKeySerializer?: (key: string) => string;
syncCondition?: (state: any) => any;
stateMergeFunction?: (state: any, rehydratedState: any) => any;
}