From a94f291397592cf29376e6e22e101e36d66daff0 Mon Sep 17 00:00:00 2001 From: scott Date: Thu, 3 Jan 2019 18:38:17 +0800 Subject: [PATCH] add customizing serializer/deserializer config --- src/createPersistoid.js | 5 ++++- src/getStoredState.js | 5 ++++- src/types.js | 3 ++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/createPersistoid.js b/src/createPersistoid.js index c4423af60..701170326 100644 --- a/src/createPersistoid.js +++ b/src/createPersistoid.js @@ -16,7 +16,10 @@ export default function createPersistoid(config: PersistConfig): Persistoid { config.keyPrefix !== undefined ? config.keyPrefix : KEY_PREFIX }${config.key}` const storage = config.storage - const serialize = config.serialize === false ? x => x : defaultSerialize + let serialize = config.serialize === false ? x => x : defaultSerialize + if (typeof config.serialize === 'function') { + serialize = config.serialize + } const writeFailHandler = config.writeFailHandler || null // initialize stateful values diff --git a/src/getStoredState.js b/src/getStoredState.js index bcf78553c..d30981e8a 100644 --- a/src/getStoredState.js +++ b/src/getStoredState.js @@ -13,7 +13,10 @@ export default function getStoredState( }${config.key}` const storage = config.storage const debug = config.debug - const deserialize = config.serialize === false ? x => x : defaultDeserialize + let deserialize = config.serialize === false ? x => x : defaultDeserialize + if (typeof config.deserialize === 'function') { + deserialize = config.deserialize + } return storage.getItem(storageKey).then(serialized => { if (!serialized) return undefined else { diff --git a/src/types.js b/src/types.js index 13549104b..a0a76120b 100644 --- a/src/types.js +++ b/src/types.js @@ -23,7 +23,8 @@ export type PersistConfig = { stateReconciler?: false | Function, getStoredState?: PersistConfig => Promise, // used for migrations debug?: boolean, - serialize?: boolean, + serialize?: boolean | Function, + deserialize?: Function | null, timeout?: number, writeFailHandler?: Function, }