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

cruducer

v1.2.2

Published

**What it is:** a library that provides an opinionated implementation of higher-order reducers and action-creators for performing simple CRUD-like operations on collection-like state.

Downloads

11

Readme

Cruducer

What it is: a library that provides an opinionated implementation of higher-order reducers and action-creators for performing simple CRUD-like operations on collection-like state.

What it does: cuts down on boilerplate of writing reducers and action-creators.

What it is helpful for: reducer composition of normalized substates/state-slices

Installation

yarn add cruducer

Usage

Two utility functions are provided:

  • createArrayModule: use this if your collection substate is an array
  • createObjectModule: use this if your collection substate is an object-literal (key-value map)

When you call either function, give it four action names (action type):

  • setAction: the action name for setting the items
  • addAction: the action name for adding an item
  • removeAction: the action name for removing an item
  • replaceAction: the action name for replacing an item

It will return an object containing:

  • a reducer that handles any of the above 4 actions
  • an actionCreators object which contains 4 action-creators (functions):
    • setItems - set the collection and overwrite the entire substate
    • addItem - add one item to the collection
    • removeItem - remove one item from the collection
    • replaceItem - replace (overwrite) one item in the collection

Now at this point, you have basic read and write actions-creators that can be used as building blocks for more complex operations on your state.

Examples

createArrayModule

Use this if your collection substate is an array.

import { createArrayModule } from 'cruducer';
import types from './actionTypes';

const { reducer, actionCreators } = createArrayModule({
  setAction:     types.SET_CHICKENS,
  addAction:     types.ADD_CHICKEN,
  removeAction:  types.REMOVE_CHICKEN,
  replaceAction: types.REPLACE_CHICKEN,
});

It will return 4 action creators:

const { setItems, addItem, removeItem, replaceItem } = actionCreators;

Assign them to variables with appropriate names for your use case:

const setChickens = actionCreators.setItem;
const addChicken = actionCreators.addItem;
const removeChicken = actionCreators.removeItem;
const replaceChicken = actionCreators.replaceItem;

And then use them like this:

setChickens([
    { id: 1, name: 'Beeky' },
    { id: 2, name: 'Bert' }
]);

addChicken({ id: 3, name: 'Wingz' })

removeChicken(2);

replaceChicken(1, { id: 1, name: 'Nuggy' })

createObjectModule

Use this if your collection substate is an object-literal (key-value map)

import { createObjectModule } from 'cruducer';
import types from './actionTypes';

const { reducer, actionCreators } = createObjectModule({
  setAction:     types.SET_CHICKENS,
  addAction:     types.ADD_CHICKEN,
  removeAction:  types.REMOVE_CHICKEN,
  replaceAction: types.REPLACE_CHICKEN,
});

It will return 4 action creators:

const { setItems, addItem, removeItem, replaceItem } = actionCreators;

Assign them to variables with appropriate names for your use case:

const setChickens = actionCreators.setItem;
const addChicken = actionCreators.addItem;
const removeChicken = actionCreators.removeItem;
const replaceChicken = actionCreators.replaceItem;

And then use them like this:

setChickens({
    1: { id: 1, name: 'Beeky' },
    2: { id: 2, name: 'Bert' }
});

addChicken(3, { id: 3, name: 'Wingz' })

removeChicken(2);

replaceChicken(1, { id: 1, name: 'Nuggy' })

Options

Custom key for array-based state items: In createArrayModule, by default, the replace and remove operations will look for id as the key on each object. To change this, pass a callback to the utility function:

const { reducer, actionCreators } = createArrayModule({
    // ...
    getKey: item => item.myCustomId
});