Skip to content

Instantly share code, notes, and snippets.

@Nauthiz-Jera
Created October 30, 2018 15:31
Show Gist options
  • Save Nauthiz-Jera/4a76a4c28a2a2d4058b2808cc29590a3 to your computer and use it in GitHub Desktop.
Save Nauthiz-Jera/4a76a4c28a2a2d4058b2808cc29590a3 to your computer and use it in GitHub Desktop.
import { comp, into, map, filter } from 'transducers-js'
/* eslint-disable no-return-assign */
// setReducer :: (a -> {a}) -> ({b} -> {a})
// add reducers that are missing from the store
const setReducer = injectedReducers => ({key, reducer}) => injectedReducers[key] = reducer
/* eslint-enable */
// hasReducer :: (a -> {a}) -> ({b} -> bool)
// returns a boolean based if the reducer to inject exists already or not
const hasReducer = injectedReducers => ({key, reducer}) =>
Reflect.has(injectedReducers, key) && injectedReducers[key] === reducer
// filterExistingReducersAddNewReducers :: a -> b
const filterExistingReducersAddNewReducers = injectReducers => comp(map(setReducer(injectReducers)), filter(hasReducer(injectReducers)))
// setupNewReducer ({a}, [{b}]) -> {a}
// add new reducers if they do do not already exist in the store
export const setupNewReducers = (injectedReducers, collection) => into(injectedReducers, filterExistingReducersAddNewReducers(injectedReducers), collection)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment