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

create-reducer-extra

v0.2.0

Published

Reduce boilerplate in your redux reducers

Downloads

11,315

Readme

create-reducer-extra

CircleCI codecov Greenkeeper badge Npm

A few helpful utilities for creating boilerplate-free Redux reducers with first class support for Typescript

Installation

yarn add create-reducer-extra

Or using npm

npm install --save create-reducer-extra

Usage

// actions.ts
import { action } from 'create-reducer-extra'

export const actionA = (someNumber) => ({ type: 'A', payload: someNumber })
export const actionB = (someString) => ({ type: 'B', payload: someString })
export const actionC = (someBool) => action('C', someBool)

// reducer.ts
import { createReducer } from 'create-reducer-extra'

const initialState = { counter: 0 }

export const reducer = createReducer<State, HandledActions>(initialState, {
  A: (state, payload) => ({ counter: state.counter + payload[0] }),
  B: (state, payload) => ({ counter: state.counter + Number(payload) }),
  C: (state, payload) => ({ counter: payload ? state.counter + 1 : state.counter - 1 }),
})

API

Note that all of the createReducer functions:

  • Expects actions to be of the form { type: string, payload: any }
  • Your your handler functions will be called directly with the payload of the action

action

A tiny utility for creating actions in the format createReducer expects

// actions.ts
import { action } from 'create-reducer-extra'

const changeName = (name) => action('ChangeName', { newName: name })

changeName('Fluffy') // { type: 'ChangeName', payload: { newName: 'Fluffy' } }

createMergeReducer

Allows you to only specify what has changed in your reducing function, e.g

import { createMergeReducer } from 'create-reducer-extra'

const initialState = {
  animals: ['ant', 'bat'],
  counter: 2,
}

const reducer = createMergeReducer(initialState, {
  Add: ({ counter }, incrementAmount) => ({ counter: counter + incrementAmount}),
  NewAnimals: ({ animals }, newAnimals) => ({ animals: [...animals, ...newAnimals] }),    
})

reducer(initialState, { payload: 5, type: 'Add' })
// { counter: 7, animals: ['ant', 'bat'] }

reducer(initialState, { payload: ['cat', 'dog'], type: 'NewAnimals' })
// { counter: 2, animals: ['ant', 'bat', 'cat', 'dog] }

createReducer

For when you want to specify exactly what the next state will be

import { createReducer } from 'create-reducer-extra'

const initialState = {
  animals: ['ant', 'bat'],
  counter: 2,
}

const reducer = createReducer(initialState, {
  Add: ({ counter }, incrementAmount) => ({ counter: counter + incrementAmount}),
  NewAnimals: ({ animals, counter }, newAnimals) => ({ animals: [...animals, ...newAnimals], counter }),    
})

reducer(initialState, { payload: 3, type: 'Add' })
// { counter: 5 }
// Note the missing 'animals' property

createResettableReducer

Provides the ability to reset a reducer to its initial state.

This can be useful for handling things such as a logout in a single page app.

The ResetState can be overridden in the handler to provide custom behaviour.

import { createResettableReducer, resetState } from 'create-reducer-extra'

const initialState = { animals: ['ant', 'bat'], counter: 2 }

const reducer = createResettableReducer(initialState, {
  Add: ({ counter }, incrementAmount) => ({ counter: counter + incrementAmount}),
  NewAnimals: ({ animals }, newAnimals) => ({ animals: [...animals, ...newAnimals] }),    
})

const nextState = reducer(initialState, { payload: 5, type: 'Add' })
// { counter: 7, animals: ['ant', 'bat'] }

reducer(nextState, resetState())
// { animals: ['ant', 'bat'], counter: 2 } === initialState

createResetMergeReducer

Combines the functionality of createMergeReducer and createResettableReducer.

Note that if the ResetState action is handled by the reducer, the result returned will be merged into the current state e.g.

import { createResetMergeReducer, resetState } from 'create-reducer-extra'

const initialState = { animals: ['ant', 'bat'], counter: 2 }

const reducer = createResetMergeReducer(initialState, {
  Add: ({ counter }, incrementAmount) => ({ counter: counter + incrementAmount}),
  [ResetState]: ({ animals }) => ({ animals: [] }),    
})

const nextState = reducer(initialState, resetState())
// { counter: 2, animals: [] }

Usage with Typescript

This library leverages some new features introduced in Typescript 2.8 to provide complete type safety with minimal boilerplate. To take advantage of completely type-safe reducers you need to:

  1. Define your action creators
// actions.ts
import { action, Action } from 'create-reducer-extra'

const actionA = (param: Array<number>): Action<'A', Array<number>> =>
  ({ type: 'A', payload: param })

const actionB = (param: string): Action<'B', string> => ({ type: 'B', payload: param })

const actionC = () => action('C', false)
  1. In your reducer create:
  • A type to represent the state of the reducer
  • A type to represent the actions your reducer should handled
  1. Provide those as type parameters to your reducer-creator of choice:
// reducer.ts
import { createReducer } from 'create-reducer-extra'
import * as actions from './actions'

type State = { counter: number }
type HandledActions = typeof actions

const reducer = createReducer<State, HandledActions>(initialState, {
  A: (state, payload) => ({ counter: state.counter + payload[0] }),
  B: (state, payload) => ({ counter: state.counter + Number(payload) }),
  C: (state, payload) => ({ counter: payload ? state.counter + 1 : state.counter - 1 }),
})

Voila, everything is type-safe!