createTransform.js.flow 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. // @flow
  2. type TransformConfig = {
  3. whitelist?: Array<string>,
  4. blacklist?: Array<string>,
  5. }
  6. export default function createTransform(
  7. // @NOTE inbound: transform state coming from redux on its way to being serialized and stored
  8. inbound: ?Function,
  9. // @NOTE outbound: transform state coming from storage, on its way to be rehydrated into redux
  10. outbound: ?Function,
  11. config: TransformConfig = {}
  12. ) {
  13. let whitelist = config.whitelist || null
  14. let blacklist = config.blacklist || null
  15. function whitelistBlacklistCheck(key) {
  16. if (whitelist && whitelist.indexOf(key) === -1) return true
  17. if (blacklist && blacklist.indexOf(key) !== -1) return true
  18. return false
  19. }
  20. return {
  21. in: (state: Object, key: string, fullState: Object) =>
  22. !whitelistBlacklistCheck(key) && inbound
  23. ? inbound(state, key, fullState)
  24. : state,
  25. out: (state: Object, key: string, fullState: Object) =>
  26. !whitelistBlacklistCheck(key) && outbound
  27. ? outbound(state, key, fullState)
  28. : state,
  29. }
  30. }