Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simple-update-in

Package Overview
Dependencies
Maintainers
1
Versions
66
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-update-in

A lightweight `updateIn` for immutable objects.

  • 1.0.0-master.5a2f0ab
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
38K
decreased by-5.84%
Maintainers
1
Weekly downloads
 
Created
Source

simple-update-in Build Status

A lightweight updateIn for immutable objects.

We love ImmutableJS. But sometimes, we want to start something from small. Thus, we created this package with zero dependencies.

Under the cover, we use Rest Operator to do most of the heavylifting.

Install

For latest stable, run npm install simple-update-in.

For active development (master branch), run npm install simple-update-in@master.

How to use

We share similar signature as ImmutableJS.updateIn:

updateIn(target: Array|Map, path: (Number|String)[], updater?: (value: any) => any)

To make updateIn efficient, especially, when paired with React. It will return a mixed deep/shallow clone of the target. It only deep clone on objects that it modified along the path, and shallow clone objects that it did not modify.

Example

Just like ImmutableJS, we want to make both Array and Map a first-class citizen. To work on a map, use a string as key. For arrays, use a number as key.

Map

import updateIn from 'simple-update-in';

const from = { one: 1, two: { number: 2 }, thirty: 3 };
const actual = updateIn(from, ['thirty'], three => three * 10);

expect(actual).toEqual({ one: 1, two: { number: 2 }, thirty: 30 });

expect(actual).not.toBe(from);   // Something under this tree has changed
expect(actual.two).toBe(to.two); // Nothing under this tree has changed
expect(actual.thirty).toBe(30);  // We multiplied it by 10

This is in fact an "upsert" operation.

Array in map

const from = { one: [1.1, 1.2, 1.3], two: [2] };
const actual = updateIn(from, ['one', 1], value => 'one point two');

expect(actual).toEqual({ one: [1.1, 'one point two', 1.3], two: [2] });

Remove a key

You can also use updateIn to remove a key by passing a falsy value to the updater argument.

const from = { one: 1, two: 2 };
const actual = updateIn(from, ['two']);

expect(actual).toEqual({ one: 1 });

expect(actual).not.toBe(from);
expect(actual).not.toHaveProperty('two');

Remove an item in array

const from = ['zero', 'one', 'two'];
const actual = updateIn(from, [1]);

expect(actual).toEqual(['zero', 'two']);

How about adding an item in an array?

Adding an item in an array is different than "upsert"-ing in a map. As we want to keep the learning curve very low, thus, we don't want to introduce syntax to do the insertion.

You can use the following code to insert an item with Rest Operator:

const from = { numbers: ['zero', 'one'] };
const actual = updateIn(from, ['numbers'], array => [...array, 'two']);

expect(actual).toEqual({ numbers: ['zero', 'one', 'two'] });

Contributions

Like us? Star us.

Want to make it better? File us an issue.

Don't like something you see? Submit a pull request.

Keywords

FAQs

Package last updated on 20 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc