-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mohammad Hasani
committed
Jan 18, 2019
1 parent
5da4b52
commit 279e517
Showing
7 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
src/__tests__/__snapshots__/create-handler-map.dts.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`createHandlerMap handle([increment, increase], (state: number) => state + 1) (type) should match snapshot: createHandlerMap handle([increment, increase], (state: number) => state + 1) 1`] = `"import(\\"/Users/brodmann/Projects/@thebrodmann/deox/src/create-handler-map\\").HandlerMap<number, { type: \\"[Counter] increment\\"; } | { type: \\"[Counter] increase\\"; }>"`; | ||
|
||
exports[`createHandlerMap handle(increment, (state: number) => state + 1) (type) should match snapshot: createHandlerMap handle(increment, (state: number) => state + 1) 1`] = `"import(\\"/Users/brodmann/Projects/@thebrodmann/deox/src/create-handler-map\\").HandlerMap<number, { type: \\"[Counter] increment\\"; }>"`; |
14 changes: 14 additions & 0 deletions
14
src/__tests__/__snapshots__/create-handler-map.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`createHandlerMap should belong multiple actions to one handler 1`] = ` | ||
Object { | ||
"[Counter] increase": [Function], | ||
"[Counter] increment": [Function], | ||
} | ||
`; | ||
|
||
exports[`createHandlerMap should belong one action to one handler 1`] = ` | ||
Object { | ||
"[Counter] increment": [Function], | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { createAction } from '../create-action' | ||
import { createHandlerMap as handle } from '../create-handler-map' | ||
|
||
// @dts-jest:group createHandlerMap | ||
|
||
const increment = createAction('[Counter] increment') | ||
const increase = createAction('[Counter] increase') | ||
|
||
// @dts-jest:pass:snap | ||
handle(increment, (state: number) => state + 1) | ||
|
||
// @dts-jest:pass:snap | ||
handle([increment, increase], (state: number) => state + 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { createAction } from '../create-action' | ||
import { createHandlerMap as handle } from '../create-handler-map' | ||
|
||
describe('createHandlerMap', () => { | ||
it('should belong one action to one handler', () => { | ||
const increment = createAction('[Counter] increment') | ||
|
||
expect(handle(increment, (state: number) => state + 1)).toMatchSnapshot() | ||
}) | ||
|
||
it('should belong multiple actions to one handler', () => { | ||
const increment = createAction('[Counter] increment') | ||
const increase = createAction('[Counter] increase') | ||
|
||
expect( | ||
handle([increment, increase], (state: number) => state + 1) | ||
).toMatchSnapshot() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { ActionCreator } from './create-action' | ||
import { AnyAction } from './action' | ||
import { getType } from './get-type' | ||
|
||
export type HandlerMap<State, Actions extends AnyAction> = { | ||
[key in Actions['type']]: Reducer<State, Actions> | ||
} | ||
|
||
export type CreateHandlerMap<State> = < | ||
AC extends ActionCreator<string>, | ||
Actions extends AnyAction = AC extends (...args: any[]) => infer T ? T : never | ||
>( | ||
actionCreators: AC | AC[], | ||
handler: Reducer<State, Actions> | ||
) => HandlerMap<State, Actions> | ||
|
||
/** | ||
* Handler map factory | ||
* @description create an action(s) to reducer map | ||
* @example | ||
* createHandlerMap(increment, (state: number) => state + 1) | ||
* @example | ||
* createHandlerMap([increment, increase], (state: number) => state + 1) | ||
*/ | ||
export const createHandlerMap = < | ||
AC extends ActionCreator<string>, | ||
State, | ||
Actions extends AnyAction = AC extends (...args: any[]) => infer T ? T : never | ||
>( | ||
actionCreators: AC | AC[], | ||
reducer: Reducer<State, Actions> | ||
) => | ||
(Array.isArray(actionCreators) ? actionCreators : [actionCreators]) | ||
.map(getType) | ||
.reduce<HandlerMap<State, Actions>>( | ||
(acc, type) => { | ||
acc[type] = reducer | ||
return acc | ||
}, | ||
{} as any | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type Reducer<State, Actions> = (prevState: State, action: Actions) => State |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters