Created
July 8, 2019 01:20
-
-
Save NEO97online/92a1684a8fe2f697745317fe383698c2 to your computer and use it in GitHub Desktop.
Simplified action type creator with constant validation
This file contains hidden or 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
import camelCase from 'lodash/camelCase' | |
export default function createActions(actionTypes, actionTypeConstants) { | |
const actions = {} | |
for (const actionType of actionTypes) { | |
if (actionTypeConstants && !actionTypeConstants[actionType]) { | |
throw new Error(`Tried creating invalid action type: ${actionType}`) | |
} | |
const key = camelCase(actionType) | |
actions[key] = (payload) => ({ | |
type: actionType, | |
payload | |
}) | |
} | |
return actions | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment