Skip to content

Instantly share code, notes, and snippets.

@jagretz
Created October 19, 2018 18:40
Show Gist options
  • Save jagretz/a8ac551d5b0a4c0262ceaa80b59839e2 to your computer and use it in GitHub Desktop.
Save jagretz/a8ac551d5b0a4c0262ceaa80b59839e2 to your computer and use it in GitHub Desktop.
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