From 125800a8b97170a45abc3d8eb9619b293e14ff45 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Wed, 23 Oct 2019 17:01:48 -0400 Subject: [PATCH] buildMutations => ts --- src/buildMutations/index.js | 53 ----------------------------- src/buildMutations/index.ts | 66 +++++++++++++++++++++++++++++++++++++ src/types.ts | 2 +- 3 files changed, 67 insertions(+), 54 deletions(-) delete mode 100644 src/buildMutations/index.js create mode 100644 src/buildMutations/index.ts diff --git a/src/buildMutations/index.js b/src/buildMutations/index.js deleted file mode 100644 index f7dab81..0000000 --- a/src/buildMutations/index.js +++ /dev/null @@ -1,53 +0,0 @@ -import fp from 'lodash/fp'; -import u from 'updeep'; - -const composeMutations = (mutations) => - mutations.reduce( (m1,m2) => - (payload=null,action={}) => state => m2(payload,action)( - m1(payload,action)(state) )); - -export default function buildMutations(mutations = {}, subduxes= {}) { - // we have to differentiate the subduxes with '*' than those - // without, as the root '*' is not the same as any sub-'*' - - const actions = fp.uniq( Object.keys(mutations).concat( - ...Object.values( subduxes ).map( ({mutations = {}}) => Object.keys(mutations) ) - ) ); - - let mergedMutations = {}; - - let [ globby, nonGlobby ] = fp.partition( - ([_,{mutations={}}]) => mutations['*'], - Object.entries(subduxes) - ); - - globby = - fp.flow([ - fp.fromPairs, - fp.mapValues( - ({reducer}) => (_,action={}) => state => - reducer(state,action) ), - ])(globby); - - const globbyMutation = (payload,action) => u( - fp.mapValues( (mut) => mut(payload,action) )(globby) - ); - - actions.forEach( action => { - mergedMutations[action] = [ globbyMutation ] - }); - - nonGlobby.forEach( ([slice, {mutations={},reducer={}}]) => { - Object.entries(mutations).forEach(([type,mutation]) => { - const localized = (payload=null,action={}) => u.updateIn( slice )( (mutation)(payload,action) ); - - mergedMutations[type].push(localized); - }) - }); - - Object.entries(mutations).forEach(([type,mutation]) => { - mergedMutations[type].push(mutation); - }); - - return fp.mapValues( composeMutations )(mergedMutations); -} diff --git a/src/buildMutations/index.ts b/src/buildMutations/index.ts new file mode 100644 index 0000000..472a1a0 --- /dev/null +++ b/src/buildMutations/index.ts @@ -0,0 +1,66 @@ +import fp from 'lodash/fp'; +import u from 'updeep'; +import {Mutation, Action, Dictionary} from '../types'; + +const composeMutations = (mutations: Mutation[]) => + mutations.reduce((m1, m2) => (payload: any = null, action: Action) => state => + m2(payload, action)(m1(payload, action)(state)), + ); + +type SubMutations = { + [ slice: string ]: Dictionary +} + +function buildMutations( + mutations :Dictionary = {}, + subduxes = {} +) { + // we have to differentiate the subduxes with '*' than those + // without, as the root '*' is not the same as any sub-'*' + + const actions = fp.uniq( + Object.keys(mutations).concat( + ...Object.values(subduxes).map(({mutations = {}}:any) => + Object.keys(mutations), + ), + ), + ); + + let mergedMutations :Dictionary = {}; + + let [globby, nonGlobby] = fp.partition( + ([_, {mutations = {}}]:any) => mutations['*'], + Object.entries(subduxes), + ); + + globby = fp.flow([ + fp.fromPairs, + fp.mapValues(({reducer}) => (_:any, action :Action) => ( state: any ) => + reducer(state, action), + ), + ])(globby); + + const globbyMutation = (payload:any, action:Action) => + u(fp.mapValues((mut:any) => mut(payload, action))(globby)); + + actions.forEach(action => { + mergedMutations[action] = [globbyMutation]; + }); + + nonGlobby.forEach(([slice, {mutations = {}, reducer = {}}]:any[]) => { + Object.entries(mutations).forEach(([type, mutation]) => { + const localized = (payload = null, action :Action) => + u.updateIn(slice)((mutation as Mutation)(payload, action)); + + mergedMutations[type].push(localized); + }); + }); + + Object.entries(mutations).forEach(([type, mutation]) => { + mergedMutations[type].push(mutation); + }); + + return fp.mapValues(composeMutations)(mergedMutations); +} + +export default buildMutations; diff --git a/src/types.ts b/src/types.ts index 78701e6..5bacf08 100644 --- a/src/types.ts +++ b/src/types.ts @@ -7,7 +7,7 @@ export type Action = { export type Dictionary = { [key: string]: T }; -export type Mutation = (payload: any, action: Action) => (state: S) => S ; +export type Mutation = (payload: any, action: Action) => (state: S) => S ; export type ActionCreator = (...args: any[] ) => Action;