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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ngrx-immutable

v1.2.0

Published

Immutable objects made easy.

Readme

ngrx-immutable

Redux state made easy.

Wile there is lot of other tools in the wild - most of them are either overengineered or do not provide enough flexibility to do the job. Maintaining Redux state with plain JavaScript might be pain in the...neck - due to nested objects. Therefor lot of people go for the immutable-js along with redux-immutable. While it wokrs perfectly well it also brings thousands lines of code which is not really necessary... especially if you prefere classic approach.

Short overview of "immute" function:

immute (
	store: object | [any],
	path: Array<string | number | function>,
	value: any | (current: any) => any
): object | [any]

It accepts 3 parameters: store, path and value. Returned value is new state of the store.

1. "store" is an object or array
2. "path" is array of "string", "number" and "function" elements, where:

	a. "string" is consider to be a key of Object,
	b. "number" is consider to be an index of Array,
	c. "function" is consider to be a Array.findIndex() callable,
used for collection lookup.

3. "value" can be explicit value or function, where:

	a. explicit value should be primitive (string | number | boolean | undefined*),
or immutable object | Array<any>.

"undefined" is used to remove key from object or element from array. Example below.

	b. function accepts current value for selected part of the store.
Returned value should met the same criteria as "explicit value" does.

Please be aware that "immute" won't work if path doesn't match given store. That is intentional.

This is how our sample store looks like.

  users: [
    {
      id: 1,
      name: 'Tom',
      ownedVehicles: [{
          type: 'Car',
          subType: 'SUV',
          make: 'Suzuki',
          model: 'Vitara',
          features: [
            { id: 1, name: 'Radio'},
            { id: 2, name: 'A/c'},
          ],
      }, {
          type: 'Car',
          subType: 'Sedan',
          make: 'Audi',
          model: 'A4 B6',
          features: [
            { id: 1, name: 'Radio'},
            { id: 2, name: 'A/c'},
            { id: 3, name: 'AirBag'},
            { id: 4, name: 'Xenon'},
          ]
      }, {
          type: 'Motorcycle',
          subType: 'Sportbike',
          make: 'Suzuki',
          model: 'GSXR600',
          features: []
      },
    ]
  },
 ...
  ],
  ...
};

Let's now do couple of basic Redux store operations. For sake of presentation we will:

  1. Change Car Model for Audi from "A4 B6" to "R8",
  2. Add "Akrapovic exhaust" feature for Suzuki GSXR,
  3. Remove "A/c" feature from Suzuki Vitara

All of those presented head-to-head, vanilla javascript and ngrx-immutable "immute" function.

  1. Change of Car Model, vanilla javascript
const userIndex = store.users.findIndex(...);
const vehicleIndex = store.users[userIndex].ownedVehicles.findIndex(...);

store = {
  ...store,
  users: [
    ...store.users.slice(0, userIndex),
    {
      ...store.users[userIndex],
      ownedVehicles: [
        ...store.users[userIndex].ownedVehicles.slice(0, vehicleIndex),
        {
          ...store.users[userIndex].ownedVehicles[vehicleIndex],
          model: "R8"
        },
        ...store.users[userIndex].ownedVehicles.slice(vehicleIndex + 1),
      ]
    },
    ...store.users.slice(userIndex + 1)
  ]
}

? WAT ?

Now do same operation with immute:

store = immute(store, [
  'users',
  (user) => user.id === <something>,
  'ownedVehicles',
  (vehicle) => vehicle.<something> = <something>,
  'model'
], "R8"];

It is also possible to use function to produce value:

store = immute(store, [
  'users',
  (user) => user.id === <something>,
  'ownedVehicles',
  (vehicle) => vehicle.<something> = <something>
], (vehicle) => ({ ...vehicle, model: "R8"})];
  1. Add "Akrapovic exhaust" feature for Suzuki GSXR, vanilla javascript
const userIndex = store.users.findIndex(...);
const vehicleIndex = store.users[userIndex].ownedVehicles.findIndex(...);

store = {
  ...store,
  users: [
    ...store.users.slice(0, userIndex),
    {
      ...store.users[userIndex],
      ownedVehicles: [
        ...store.users[userIndex].ownedVehicles.slice(0, vehicleIndex),
        {
          ...store.users[userIndex].ownedVehicles[vehicleIndex],
          features: [
            ...store.users[userIndex].ownedVehicles[vehicleIndex].features,
            {
              id: 1,
              name: 'Akrapovic exhaust'
            }
          ]
        },
        ...store.users[userIndex].ownedVehicles.slice(vehicleIndex + 1),
      ]
    },
    ...store.users.slice(userIndex + 1)
  ]
}

This became unreadable in previous example already. Yet we had to add another level of nesting. Ouch!

Let's do the same with immute:

store = immute(store, [
 'users',
 (user) => user.id === <something>,
 'ownedVehicles',
 (vehicle) => vehicle.<something> = <something>,
 'features'
], (features) => ([ ...features, { id: 1, name: "Akrapovic exhaust"}]));

We can also shorten this syntax using named functions inside path:

  const selectUser = userID => userObj => userObj === userID;
  const selectVehicle = vehicleModel => vehicle => vehicle.model === vehicleModel

  store = immute(
    store,
    ['users', selectUser(1), 'ownedVehicles', selectVehicle('GSXR600'), 'features'],
    (features) => ([ ...features, { id: 1, name: "Akrapovic exhaust" } ])
  );

... And how do you can delete item from array you might ask. 3. Remove "A/c" feature from Suzuki Vitara using immute:

  const selectUser = userID => userObj => userObj === userID;
  const selectVehicle = vehicleModel => vehicle => vehicle.model === vehicleModel
  const selectFeature = featureName => feature => feature.name === featureName;

  store = immute(
    store,
    ['users', selectUser(1), 'ownedVehicles', selectVehicle('Vitara'), 'features', selectFeature('A/c')],
    undefined
  );

Passing "undefined | () => undefined" as a value will exclude pointed key from object or pointed element from array.

I've been recently asked if this library supports modifying a subset of a larger collection. While the answer is NO - you can still do it very easily by leveraging Array.reduce and Array.findIndex like so:

const data = {
	lvl1: {
		lvl2: [
			{
				userId: 1,
				data: { value: 'aaa', },
			},
			{
				userId: 2,
				data: { value: 'bbb', },
			},
			{
				userId: 1,
				data: { value: 'ccc', },
			},
			{
				userId: 3,
				data: { value: 'ddd', },
			}
		],
	},
};

const filterUser = (userId) => (user) => user.userId === userId;
const findUser = (_user) => (user) => user === _user;

immute(data, ['lvl1', 'lvl2'], (uArr) => uArr
	.filter(filterUser(1))
	.reduce((acc, _u) => immute(acc, [uArr.findIndex(findUser(_u))], (_uObj) => ({
			... _uObj,
			extra: 'some extra data'
		})
	), uArr)
);

In given situation the output would be:

{
	lvl1: {
		lvl2: [
			{
				userId: 1,
				extra: 'some extra data',   // this was added
				data: { value: 'aaa', },
			},
			{
				userId: 2,
				data: { value: 'bbb', },
			},
			{
				userId: 1,
				extra: 'some extra data',   // this was added
				data: { value: 'ccc', },
			},
			{
				userId: 3,
				data: { value: 'ddd', },
			}
		],
	},
};