Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

reducer-composer

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reducer-composer

utilities for composing reducers

  • 0.0.9
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

reducer-composer

Compose your reducers effortlessly, with intellisense and strict type-checking.

Usage

Edit reducer-composer

import {
  createReducer,
  createCombinedReducer,
  createReducerSequence,
  createBatchReducer,
  createKeyedReducer,
  createReducerCrudHandlers,
  createReducerOnAction,
  ActionOfReducer,
  ignore
} from "reducer-composer";

interface User {
  id: string;
  name: string;
  birth: Date;
}

const user = createReducer<User>()({
  USER_CHANGE_NAME(user, { name }: { name: string }) {
    return { ...user, name };
  },
  USER_CHANGE_BIRTH(user, { birth }: { birth: Date }) {
    return { ...user, birth };
  }
});

const click = createReducer<number>()({
  CLICK(clicks) {
    return clicks + 1;
  },
  CLICK_SET(_, { clicks }: { clicks: number }) {
    return clicks;
  }
});

const userClick = createCombinedReducer({
  user,
  click
});

const userClickBatchReducer = createBatchReducer("CLICKS", userClick);

const usersReducer = createKeyedReducer("id", user);

const companyUsers = createReducerSequence(
  usersReducer,
  createReducer<Record<string, User>>()({
    USER_ADD(state, user: User) {
      return { ...state, [user.id]: user };
    }
  })
);

const usersCounter = createReducerOnAction<
  number,
  ActionOfReducer<typeof companyUsers>
>()({
  USER_ADD(count) {
    return count + 1;
  },
  USER_CHANGE_BIRTH: ignore,
  USER_CHANGE_NAME: ignore
});

interface Car {
  id: string;
  name: string;
  km: number;
}
const carCrud = createReducerCrudHandlers(
  (payload: Car) => [payload.id, payload],
  (payload: { id: string }) => payload.id
);
const car = createReducer<Record<string, Car>>()({
  CAR_ADD: carCrud.create,
  CAR_UPDATE: carCrud.update,
  CAR_DESTROY: carCrud.discard
});

more usage

Todo

[ ] handle unknown actions on keyed and keyedBy reducer [ ] documentation [ ] dtslint tests [ ] jest tests [ ] performance tests

License MIT

Keywords

FAQs

Package last updated on 14 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc