npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

deeds

v3.0.0

Published

A tool to generate typed action creators given a set of reducers.

Downloads

6

Readme

Deeds

A tiny library to generate strongly typed action creators from a set of reducers for Redux.

The core of Redux is dispatching actions containing some data and then writing reducers that act on the current state and that data. Keeping the data in the action in sync with that required by the reducer can be tricky. TypeScript helps but only if you strongly type the reducers and strongly type the actions which can be a chore. I found myself writing out a lot of boilerplate to keep things in sync and make TypeScript happy.

This library simplifies a lot of this by automatically generating strongly typed action creators from a set of reducers.

Examples

import { createStore } from "redux";
import { reducer, actionCreators, Deed } from "deeds";

const reducers = {
  add: (state: number, val: number): number => {
    return state + val;
  },
  del: (state: number, val: number): number => {
    return state - val;
  }
}

const store = createStore(reducer(reducers), 1);
const actions = actionCreators<typeof reducers>();

store.dispatch(actions.add(5));
// state is now 6.
store.dispatch(actions.del(3));
// state is now 3.

let action = actions.add(4);
// action is strongly typed at this point but mostly you will not need that and
// can just cast to Deed.
let untyped: Deed = action;
store.dispatch(untyped);

immer

Optionally this supports using immer for an immutable state:

import { createStore } from "redux";
import { Draft } from "immer";
import { reducer, actionCreators, Deed } from "deeds/immer";

interface State {
  value: readonly number;
}

const reducers = {
  add: (state: Draft<State>, val: number): void => {
    state.value += val;
  },
  del: (state: Draft<State>, val: number): void => {
    state.value -= val;
  }
}

const store = createStore(reducer(reducers), { value: 1 });
const actions = actionCreators<typeof reducers>();

store.dispatch(actions.add(5));
// state is now 6.
store.dispatch(actions.del(3));
// state is now 3.

deeds/immer exports functions with the same names as those in deeds. While some of those are just re-exports from deeds right now semantic versioning will increased on the basis that you are only using the exports of one of deeds or deeds/immer for any given store.

Uninitialized stores

The reducer you pass to Redux's createStore must accept an undefined store state and the reducer returned from the reducer call matches that signature, however in order to avoid having to define every reducer as accepting an undefined state it will simply throw an exception if an undefined state is ever passed. This works fine if you always pass an initialized state to createStore but will be a problem if you create an uninitialized store and then use an action to initialize it. In this case you will need to create your own reducer to handle the unitialized case:

import { createStore } from "redux";
import { baseReducer, Deed } from "deeds";

interface InitializeAction {
  type: "INITIALIZE";
  value: number;
}

const reducers = {
  add: (state: number, val: number): number => {
    return state + val;
  },
  del: (state: number, val: number): number => {
    return state - val;
  }
}

// `baseReducer` is identical to `reducer` but only accepts an initialized state.
const reducer = baseReducer(reducers);
const store = createStore((state: number | undefined, action: Deed | InitializeAction) => {
  if (!state) {
    if (action.type == "INITIALIZE") {
      return action.value;
    }

    // Early action?
    state = 0;
  }

  return reducer(state, action);
});
const actions = actionCreators<typeof reducers>();