buildUpreducer

typescript
Yanick Champoux 2019-10-23 13:25:39 -04:00
parent 11ff89501d
commit f092526ded
3 changed files with 29 additions and 15 deletions

View File

@ -1,15 +0,0 @@
import fp from 'lodash/fp';
export default function buildUpreducer(initial, mutations) {
return (action = {}) => (state) => {
if (state === null) state = initial;
const a =
mutations[(action).type] ||
mutations['*'];
if(!a) return state;
return a((action).payload, action)(state);
};
}

View File

@ -0,0 +1,19 @@
import fp from 'lodash/fp';
import { Dictionary, Mutation, UpduxAction } from '../types';
function buildUpreducer<S>(initial: S, mutations: Dictionary<Mutation<S>> ) {
return (action :UpduxAction) => (state: S) => {
if (state === null) state = initial;
const a =
mutations[action.type] ||
mutations['*'];
if(!a) return state;
return a(action.payload, action)(state);
};
}
export default buildUpreducer;

10
src/types.ts Normal file
View File

@ -0,0 +1,10 @@
import { Action } from 'redux';
export type UpduxAction = Action & Partial<{
payload: any,
meta: any,
}>
export type Dictionary<T> = { [key: string]: T };
export type Mutation<S> = (payload: any, action: Action) => (state: S) => S ;