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

lodash-redux-immutability

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lodash-redux-immutability

Lean immutable wrappers around lodash get/set/update/delete, especially helpful for redux reducers.

  • 0.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
64
decreased by-52.59%
Maintainers
1
Weekly downloads
 
Created
Source

lodash-redux-immutability

Lean (just 24 lines of code!) immutable wrappers around lodash get/set/update/delete, especially helpful for redux reducers.

Install

npm install lodash-redux-immutability --save

Dependencies

lodash.clone;
lodash.get;
lodash.isempty;
lodash.isnumber;
lodash.isstring;
lodash.setwith;
lodash.unset;

Test coverage

✅ 100%

Gotchas

When setting/updating, passing a number (ie, 3) in your path will look for an array at index 3 , but passing a "string-number" (ie, '3') will look for an object with a key of '3'.
Source | Tests

API

updateIn(state:Object, path:Array<string|number>, valueToUpdate:Object)

Merges (spreads) valueToUpdate at path inside of state with the current value found at this path, creating any keys in path that do not exist, returning a new object where affected objects in the path have a new reference.

Source | Tests | Example:

import { updateIn } from 'lodash-redux-immutability';
const state = {
  stores: {
    '3': {
      name: 'three'
    '4': {
      name: 'four'
    }
  }
};
const storeThreeUpdates = { location: 'Third street' };
const newState = updateIn(state, ['stores', '3'], storeThreeUpdates);
newState.stores['3']; // { name: 'three', 'location: 'Third street' };
newState.stores === state.stores; // false
newState.stores['3'] === state.stores['3']; // false
newState.stores['4'] === state.stores['4']; // true

setIn(state:Object, path:Array<string|number>, valueToSet:any)

Replaces valueToSet at path inside of state, creating any keys in path that do not exist, returning a new object where affected objects in the path have a new reference.

Source | Tests | Example:

import { setIn } from 'lodash-redux-immutability';
const state = {
  stores: {
    '3': {
      name: 'three'
    }
    '4': {
      name: 'four'
    }
  }
};
const newState = setIn(state, ['stores', '3', 'name'], 'tres');
newState.stores['3']; // { name: 'tres' }
newState.stores === state.stores; // false
newState.stores['3'] === state.stores['3']; // false
newState.stores['4'] === state.stores['4']; // true

deleteIn(state:Object, path:Array<string|number>)

Deletes the value at path inside of state, returning a new object where affected objects in the path have a new reference.

Source | Tests | Example:

import { deleteIn } from 'lodash-redux-immutability';
const state = {
  '3': {
    name: 'three'
  },
  '4': {
    name: 'four'
  }
};
const newState = deleteIn(state, ['stores', '3'];
newState.stores['3']; // undefined
newState.stores === state.stores; // false
newState.stores['4'] === state.stores['4']; // true

getIn(state:Object, path:Array<string|number>, defaultValue?:any)

Get a value from an object by path. Returns the value, unless path is an empty [], in which case returns the original object

Source | Tests | Example:

import { getIn } from 'lodash-redux-immutability';
const state = {
  stores: {
    '3': {
      name: 'three'
    }
  }
};
const storeThreeName = getIn(state, ['stores', '3', 'name']); // 'three'
const entireState = getIn(state, []); // the entire 'state' object (this is what makes `getIn` different from the traditional `_.get`)

License

MIT

Keywords

FAQs

Package last updated on 24 Apr 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