import { update } from 'lodash' ; import { Action , createAction } from 'redux-actions' ; import { Reducer } from '../types/reducer' ; import { ActionCreator } from '../types/actionCreator' ; export interface IReducerAndAction < P > { reducer : Reducer < { } > ; action : ActionCreator ; } export const makeScope = ( namespacing , excludeField ? ) => < LocalState , P > ( type , fn ) : IReducerAndAction < P > => { const reducer = ( state , action ) => { const keyPath = excludeField ? namespacing . slice ( 1 , - 1 ) : namespacing . slice ( 1 ) ; const nextState = { ... state } ; if ( keyPath . length ) { return update ( nextState , keyPath . join ( '.' ) , ( scopedState ) => fn ( scopedState , action ) ) ; } return fn ( state as any , action ) ; } ; return { reducer , action : createAction ( type ) , } ; } ;