Skip to content

Commit

Permalink
make deserialize/serialize of config consistent; optimize if-else sta…
Browse files Browse the repository at this point in the history
…tement for getStoredState.js & createPersistoid.js
  • Loading branch information
scott1028 committed Jan 28, 2019
1 parent a94f291 commit 13a0b60
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/createPersistoid.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ export default function createPersistoid(config: PersistConfig): Persistoid {
config.keyPrefix !== undefined ? config.keyPrefix : KEY_PREFIX
}${config.key}`
const storage = config.storage
let serialize = config.serialize === false ? x => x : defaultSerialize
if (typeof config.serialize === 'function') {
let serialize
if (!config.serialize) {
serialize = defaultSerialize
} else if (typeof config.serialize === 'function') {
serialize = config.serialize
} else {
serialize = x => x
}
const writeFailHandler = config.writeFailHandler || null

Expand Down
8 changes: 6 additions & 2 deletions src/getStoredState.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ export default function getStoredState(
}${config.key}`
const storage = config.storage
const debug = config.debug
let deserialize = config.serialize === false ? x => x : defaultDeserialize
if (typeof config.deserialize === 'function') {
let deserialize
if (!config.deserialize) {
deserialize = defaultDeserialize
} else if (typeof config.deserialize === 'function') {
deserialize = config.deserialize
} else {
deserialize = x => x
}
return storage.getItem(storageKey).then(serialized => {
if (!serialized) return undefined
Expand Down
2 changes: 1 addition & 1 deletion src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type PersistConfig = {
getStoredState?: PersistConfig => Promise<PersistedState>, // used for migrations
debug?: boolean,
serialize?: boolean | Function,
deserialize?: Function | null,
deserialize?: boolean | Function,
timeout?: number,
writeFailHandler?: Function,
}
Expand Down

0 comments on commit 13a0b60

Please sign in to comment.