From f092526deda602720d057759843b7bc5723c84cf Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Wed, 23 Oct 2019 13:25:39 -0400 Subject: [PATCH] buildUpreducer --- src/buildUpreducer/index.js | 15 --------------- src/buildUpreducer/index.ts | 19 +++++++++++++++++++ src/types.ts | 10 ++++++++++ 3 files changed, 29 insertions(+), 15 deletions(-) delete mode 100644 src/buildUpreducer/index.js create mode 100644 src/buildUpreducer/index.ts create mode 100644 src/types.ts diff --git a/src/buildUpreducer/index.js b/src/buildUpreducer/index.js deleted file mode 100644 index 52724f6..0000000 --- a/src/buildUpreducer/index.js +++ /dev/null @@ -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); - }; -} diff --git a/src/buildUpreducer/index.ts b/src/buildUpreducer/index.ts new file mode 100644 index 0000000..bedc8b2 --- /dev/null +++ b/src/buildUpreducer/index.ts @@ -0,0 +1,19 @@ +import fp from 'lodash/fp'; + +import { Dictionary, Mutation, UpduxAction } from '../types'; + +function buildUpreducer(initial: S, mutations: Dictionary> ) { + 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; diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..414d299 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,10 @@ +import { Action } from 'redux'; + +export type UpduxAction = Action & Partial<{ + payload: any, + meta: any, +}> + +export type Dictionary = { [key: string]: T }; + +export type Mutation = (payload: any, action: Action) => (state: S) => S ;