UNPKG

sub-redux

Version:

[![npm version](https://img.shields.io/npm/v/sub-redux.svg)](https://www.npmjs.com/package/sub-redux) [![npm](https://img.shields.io/npm/dm/sub-redux.svg)](https://www.npmjs.com/package/sub-redux)

35 lines (29 loc) 767 B
import { Action } from 'typesafe-actions'; import { SubAction } from './types'; // Wrap / unwrap sub actions *************************************************** export const wrapSubAction = ({ instance, subAction, }: { instance: string; subAction: SubAction; }) => ({ ...subAction, type: `SUB_REDUX/${instance}/${subAction.type}` as 'SUB_REDUX/x/SUB_ACTION', }); export const unwrapSubAction = ( action: Action, ): { instance: string; subAction: SubAction } => { const match = action.type.match(/^SUB_REDUX\/([0-9a-zA-Z]+)\/(.*)$/); if (!match) { throw new Error('Invalid action for unwrapSubAction'); } const [, instance, subType] = match; return { instance, subAction: { ...action, type: subType, }, }; };