Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
export const APP = "r3";
export const createActionType = app => duck => type => `${app}/${duck}/${type}`;
export const createDuckType = createActionType(APP);
/**
* Constructs a new action with a required type and optional additional action properties supplied as
* optional argument(s*); *see `...rest` param below.
*
* @export
* @param {string} type the action type
* @param {...any} rest addition action properties; one or more.
* @returns {Action} An action with the provided type and any additional props.
* @throws {TypeError} for an undefined or otherwise invalid type property.
* @example
*
* import import { createDuckType } from ../path/to/import;
* const createActionType = createDuckType("myReducer");
* export const MY_ACTION_TYPE = createActionType("MY_ACTION_TYPE");
*/
export function createAction(type, ...rest) {
if (!type || typeof type !== "string") throw new TypeError(`An invalid type argument was supplied: ${type}`);
return { type, ...rest };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment