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 🙏

© 2025 – Pkg Stats / Ryan Hefner

immutable-gene

v1.0.2

Published

helper library to remove boilplate code from reducers

Downloads

3

Readme

immutable-gene

clean your boilerplate code inside reducers

Utility library that will help you clean mess in your flux/redux reducers. It updates state and returns brand new object with configured modifications.

    npm install --save immutable-gene

Quick start

    import {Gene} from 'immutable-gene';

    let state:any = { a: { b: {c: [{id:1, name:'jon'}, {id:2, name:'bob'}]}}};
    let result = Gene.mutate(state)
        .update('a.b.c[id=1]', {name: 'josh'})
        .push('a.b.c', {id: 3, name: 'zofia'})
        .value();

    //result:
    //state = { a: { b: {c: [{id=1, name='josh'}, {id=2, name='bob'}, {id=3, name='zofia'}]}}};
    import {Gene} from 'immutable-gene';

    let state:any ={groups: [
                        {
                          "name": "sit",
                          "users": [
                            {"id": 0, "name": "Lara Lowery"},
                            {"id": 1, "name": "Gayle Whitfield"},
                            {"id": 2, "name": "Shaw Schmidt"}]
                        },
                        {
                          "name": "ad",
                          "users": [
                            {"id": 3, "name": "Ruby Pickett"},
                            {"id": 4, "name": "Campos Flowers"},
                            {"id": 5,  "name": "Kirkland Faulkner"}
                          ]
                        }]};
    let userId = 4;
    let result = Gene.update(state, ['groups[*].users[id=?]', userId], {name: 'XYZ'})
    //it is flattening group collection and looks on each of them for user with id = 4 and updates its name to XYX
    //result:
    //              {groups: [
    //                     {
    //                       "name": "sit",
    //                       "users": [
    //                         {"id": 0, "name": "Lara Lowery"},
    //                         {"id": 1, "name": "Gayle Whitfield"},
    //                         {"id": 2, "name": "Shaw Schmidt"}]
    //                     },
    //                     {
    //                       "name": "ad",
    //                       "users": [
    //                         {"id": 3, "name": "Ruby Pickett"},
    //                         {"id": 4, "name": "XYZ"},
    //                         {"id": 5,  "name": "Kirkland Faulkner"}
    //                       ]
    //                     }]};

supported methods :

  • Gene.update(state, selector, object) - updates nested object found by selector like assign(i, x) from lodash
  • Gene.push(state, selector, object) - adds object to nested array
  • Gene.remove(state, selector) - removes object from array or from parent object
  • Gene.mutate(state) - allows fluid interfacet for chainig update operations.. to get final result invoke .value() method

slecotrs

it uses json-query npm package under the hood so it suports all selectors from this awesome packange: https://www.npmjs.com/package/json-query

for example:

Array filter

By default only the first matching item will be returned:

people[name=Matt]

But if you add an asterisk (*), all matching items will be returned:

people[*country=NZ]

You can use comparative operators:

people[*rating>=3]

Motivation

Most of reducer code in any flux implementation looks like below:


export default function todos(state = initialState, action) {
  switch (action.type) {
    case ADD_TODO:
      return [
        {
          id: state.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + 1,
          completed: false,
          text: action.text
        },
        ...state
      ]

    case DELETE_TODO:
      return state.filter(todo =>
        todo.id !== action.id
      )

    case EDIT_TODO:
      return state.map(todo =>
        todo.id === action.id ?
          { ...todo, text: action.text } :
          todo
      )

    case COMPLETE_TODO:
      return state.map(todo =>
        todo.id === action.id ?
          { ...todo, completed: !todo.completed } :
          todo
      )

    case COMPLETE_ALL:
      const areAllMarked = state.every(todo => todo.completed)
      return state.map(todo => ({
        ...todo,
        completed: !areAllMarked
      }))

    case CLEAR_COMPLETED:
      return state.filter(todo => todo.completed === false)

    default:
      return state
  }
}

It's pretty messy, hard to read and hard to write. And above exaple is just a simple case scenario. situation is getting more comlicated when you try to update some nested property in your state.

With usage of immutable-bene above code would look like below:


export default function todos(state = initialState, action) {
  switch (action.type) {
    case ADD_TODO:
      return Gene.push(sate, '', {
                                   id: state.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + 1,
                                   completed: false,
                                   text: action.text
                                 });
    case DELETE_TODO:
      return Gene.remove(state, ['[id=?]', action.id]);

    case EDIT_TODO:
      return Gene.update(state, ['[id=?]', action.id], {text: action.text });

    case COMPLETE_TODO:
      return Gene.update(state, ['[id=?]', action.id], {completed: true });

    case COMPLETE_ALL:
      const areAllMarked = state.every(todo => todo.completed)
      return Gene.update(state, '', {completed: areAllMarked });

    case CLEAR_COMPLETED:
      return state.filter(todo => todo.completed === false)

    default:
      return state
  }
}