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

@mariosant/imm

v1.0.0

Published

Develop reducers easier

Downloads

207

Readme

Imm

Write better reducers!

NPM version CircleCI

Imm is a set of tools to help you simplify your reducers. It works with all flux-style architectures, including Redux, Recompose's withReducer and React's useReducer hook.

Installation

Just add @mariosant/imm to your package.json.

$ npm install @mariosant/imm

# or
$ yarn add @mariosant/imm

You can now import the module and use it like

import { createReducer, when } from '@mariosant/imm';

// or if you are on node or browserify
const { createReducer, when } = require('@mariosant/imm');

Usage

Using imm is pretty straightforward and simple. All it requires, is to dispatch actions that comply with the Flux Standard Action.

import { createReducer, when } from '@mariosant/imm';

const initialState = {};

const reducer = createReducer([
	when('SET_NAME', (state, { payload }) => ({ ...state, name: payload })),
	when('SET_EMAIL', (state, { payload }) => ({ ...state, email: payload })),
], initialState);

or even better:

import { createAction, createReducer, when, select } from '@mariosant/imm';
const SET_NAME = 'SET_NAME';
const SET_EMAIL = 'SET_EMAIL';

const initialState = {};

const setUserNameAction = createAction(SET_NAME);
const setUserName = (state, { payload }) =>
	select(['user', 'name'], _name => payload, state);

// dot notation for paths work as well
const setUserEmailAction = createAction(SET_EMAIL);
const setUserEmail = (state, { payload }) =>
	select('user.email', _email => payload, state);

const reducer = createReducer([
	when(SET_NAME, setUserName),
	when(SET_EMAIL, setUserEmail),
], initialState);

The first argument passed to when, can be a String or a Function. If it is a String, it compares it with the type of your dispatched action. If it is an actionCreator, it uses it to get its type and executes the associated transfomer.

Here is an example.

import { createReducer, when, createAction } from '@mariosant/imm';
import { propEq } from 'ramda';

const email = createAction('SET_EMAIL');

const setName = (state, { payload }) => ({ ...state, name: payload });
const setEmail = (state, { payload }) => ({ ...state, email: payload });
const setAge = (state, { payload }) => ({ ...state, age: payload });

const reducer = createReducer(when('SET_NAME', setName), when(email, setEmail));

By using this paradigm, you have less things to worry about. You don't have to care about multiple return statements and you only have to test your transformers and if you use the feature, your predicates,

Development

Easy enough!

$ yarn install  # to install dependencies
$ yarn test     # to run the test suite

Meta

Marios Antonoudiou – @marios_ant[email protected]

Distributed under the MIT license.

https://github.com/mariosant/imm

Contributing

  1. Fork it (https://github.com/mariosant/imm/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes using a semantic commit message.
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request