redux-persist-transform-filter
Advanced tools
Comparing version 0.0.6 to 0.0.7
46
index.js
import { createTransform } from 'redux-persist'; | ||
import get from 'lodash.get'; | ||
import set from 'lodash.set'; | ||
import unset from 'lodash.unset'; | ||
export default (reducerName, inboundPaths, outboundPaths) => { | ||
export default function createFilter (reducerName, inboundPaths, outboundPaths, transformType = 'whitelist') { | ||
return createTransform( | ||
@@ -10,3 +11,3 @@ // inbound | ||
return inboundPaths | ||
? persistFilter(inboundState, inboundPaths) | ||
? persistFilter(inboundState, inboundPaths, transformType) | ||
: inboundState; | ||
@@ -18,13 +19,21 @@ }, | ||
return outboundPaths | ||
? persistFilter(outboundState, outboundPaths) | ||
? persistFilter(outboundState, outboundPaths, transformType) | ||
: outboundState; | ||
}, | ||
{whitelist: [reducerName]} | ||
{'whitelist': [reducerName]} | ||
); | ||
}; | ||
export function persistFilter (state, paths = []) { | ||
const subset = {}; | ||
export function createWhitelistFilter (reducerName, inboundPaths, outboundPaths) { | ||
return createFilter(reducerName, inboundPaths, outboundPaths, 'whitelist'); | ||
} | ||
export function createBlacklistFilter (reducerName, inboundPaths, outboundPaths) { | ||
return createFilter(reducerName, inboundPaths, outboundPaths, 'blacklist'); | ||
} | ||
export function persistFilter (state, paths = [], transformType = 'whitelist') { | ||
let subset = {}; | ||
// support only one key | ||
@@ -35,11 +44,24 @@ if (typeof paths === 'string') { | ||
paths.forEach((path) => { | ||
const value = get(state, path); | ||
if (transformType === 'whitelist') { | ||
paths.forEach((path) => { | ||
const value = get(state, path); | ||
if (typeof value !== 'undefined') { | ||
set(subset, path, value); | ||
} | ||
}); | ||
if (typeof value !== 'undefined') { | ||
set(subset, path, value); | ||
} | ||
}); | ||
} else if (transformType === 'blacklist') { | ||
subset = Object.assign({}, state); | ||
paths.forEach((path) => { | ||
const value = get(state, path); | ||
if (typeof value !== 'undefined') { | ||
unset(subset, path); | ||
} | ||
}); | ||
} else { | ||
subset = state; | ||
} | ||
return subset; | ||
} |
{ | ||
"name": "redux-persist-transform-filter", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Filter transformator for redux-persist", | ||
@@ -30,4 +30,5 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"lodash.get": "^4.4.2", | ||
"lodash.set": "^4.3.2" | ||
"lodash.get": "^4.5.0", | ||
"lodash.set": "^4.5.0", | ||
"lodash.unset": "^4.5.2" | ||
}, | ||
@@ -34,0 +35,0 @@ "devDependencies": { |
@@ -24,5 +24,11 @@ # redux-persist-transform-filter | ||
// you want to remove some keys before you save | ||
const saveSubsetBlacklistFilter = createBlacklistFilter( | ||
'myReducerTwo', | ||
['keyYouDontWantToSave1', 'keyYouDontWantToSave2'] | ||
); | ||
// you want to load only a subset of your state of reducer two | ||
const loadSubsetFilter = createFilter( | ||
'myReducerTwo', | ||
'myReducerThree', | ||
null, | ||
@@ -35,3 +41,3 @@ ['keyYouWantToLoad1', 'keyYouWantToLoad2'] | ||
const saveAndloadSubsetFilter = createFilter( | ||
'myReducerThree', | ||
'myReducerFour', | ||
['one', 'two'] | ||
@@ -44,2 +50,3 @@ ['three', 'four'] | ||
saveSubsetFilter, | ||
saveSubsetBlacklistFilter, | ||
loadSubsetFilter, | ||
@@ -46,0 +53,0 @@ saveAndloadSubsetFilter, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6811
90
63
4
+ Addedlodash.unset@^4.5.2
+ Addedlodash.unset@4.5.2(transitive)
- Removedlodash.get@4.4.2(transitive)
- Removedlodash.set@4.3.2(transitive)
Updatedlodash.get@^4.5.0
Updatedlodash.set@^4.5.0