0.0.11 • Published 5 years ago

yet-another-redux-helpers v0.0.11

Weekly downloads
16
License
MIT
Repository
github
Last release
5 years ago

yet-another-redux-helpers

overview

This package exports a set of useful types and helpers for redux store of your app.

Compatible with typescript, angular and react.

installation

npm i -S yet-another-redux-helpers

development

npm i && npm run build

types

Action

Provides an interface to describe an action with optional typed payload.

ReducerType

Provides a type to describe a reducer.

helpers

toPayload

Extracts payload from action.

reduce

Provides a utility function to reduce your state. Features:

  • creates empty state (using state factory) if no state is provided
  • does not mutate state, creates new state based on previous one
  • reduces state using provided reducer map

test helpers

Package also export one test helper for unit testing your reducers.

ReducerTestSuite

Tests if your reducer comply with the redux paradigm:

  • should create initial state in response to a unknown action if the source state is not defined
  • should create initial state in response to a known action if the source state is not defined
  • should not return source state in response to a known action, but should return new state
  • should return source state in response to an unknown action

usage

implementation

import {Action, ReducerType, reduce} from 'yet-another-redux-helpers';
import {State} from './state';
import {ACTION_TYPE} from './action-types';
import {someReducer} from './reducers';

// declare action type
export const ACTION_TYPE = 'some-action';

// declare action
export class SomeAction implements Action {
    readonly type = ACTION_TYPE;
    constructor(public payload:any) {}
}

// declare state
export class State {
    value:number = 0;
}

// declare reducer
export function someReducer(state:State, action:SomeAction):State {
    state.value = 1;
    return state;
}

// declare reducer map
export const actionReducerMap = new Map<string, ReducerType<State>>([
    [ACTION_TYPE, someReducer],
]);

// create reducer
export function reducer(state:State, action:Action):State {
    return reduce<State>(
        () => new State(),
        actionReducerMap,
        state,
        action,
    );
}

testing

import {ReducerTestSuite} from 'yet-another-redux-helpers';
import {State} from './state';
import {someReducer} from './reducer';
import {SomeAction} from './actions';

describe('SomeReducer', ReducerTestSuite(
    someReducer,
    new State(),
    new SomeAction()
));
0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago