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

immutability-util

v0.0.4

Published

Mutate a copy of data without changing the original source. Inspired by immutability-helper.

Downloads

282

Readme

immutability-util

Mutate a copy of data without changing the original source. Inspired by kolodny/immutability-helper / ProtoTeam/immutability-helper-x and rewrite with ES6 syntax for convenient API and higher performance.

Build Status Coverage Status npm npm download

1. Features

Features or key points.

  1. Use chainble API to mutate a copy of object.
  2. Simplify your code by finding target to be updated with path string.
  3. Maybe can optimize your web app with a bit higher performance.

2. Install & Usage

Install with NPM.

npm install -S immutability-util

Then use it.

const iu = require('immutability-util');
// or 
import iu from 'immutability-util';

// obj need to be mutated.
var obj = {
  a: 1,
  b: 2,
  c: {
    d: 3,
    e: {
      f: [4, 5, 6],
      g: {
        h: 'iu',
      },
    },
    i: {
      j: 'hello, world.',
      k: [7, 8, 9],
      l: [10, 11, 12],
    }
  },
};

// chainable usage.
const state = iu(obj)
  .$apply(['a'], v => v + 1)
  .$merge(['c', 'e', 'g'], { m: 'update'})
  .$push(['c', 'e', 'f'], [7, 8, 9])
  .$set(['c', 'i', 'j'], 'hello node 8.')
  .$splice(['c', 'i', 'k'], [[1, 1, 10]])
  .$unset(['c'], ['d'])
  .$unshift(['c', 'i', 'l'], [13])
  .value(); // then get the mutated copy.
  
// or use path string.
iu(obj).$set('c.i.j', 'hello node 8.').value();

And process array like this:

const obj = {
  a: {
    b: [{
      c: [1, 2, 3],
    }, {
      d: 4,
    }, {
      e: [5, 6],
    }]
  }
};
const state = iu(obj)
  .$apply('a.b[1].d', v => v + 1)
  .$push('a.b[0].c', [4])
  .$set('a.b[2].e[0]', 'hello node 8.')
  .value();

3. Available API

After got the instance of ImmutabilityUtil, you can use the chainable methods below.

  • $apply(path, function): passes in the current value to the function and updates it with the new returned value.
  • $merge(path, object): merge the keys of object with the target.
  • $push(path, array): push() all the items in array on the target.
  • $set(path, any): replace the target entirely.
  • $splice(path, array of arrays): call splice() with the array of arrays on the target with the parameters provided by the item.
  • $unset(path, array of strings): remove the list of keys in array from the target object.
  • $unshift(path, array): unshift() all the items in array on the target.

Then you can use API `value()` to get the immutable data copy. All the value of method are compatible with `immutability-helper`.

Your pull requests are needed for more API.


## 4. Build & Test

You can develop and test as below.
```sh
npm run build
# run the testcases
npm run test
```

You can run `npm run benchmark` to get the comparison of performance.


## 5. License

MIT@[hustcc](https://github.com/hustcc).