• Stars
    star
    407
  • Rank 106,183 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Modify deep object properties without modifying the original object (immutability). Works great with React and Redux.

build coverage downloads version deps devdeps

object-path-immutable

Tiny JS library to modify deep object properties without modifying the original object (immutability). Works great with React (especially when using setState()) and Redux (inside a reducer).

This can be seen as a simpler and more intuitive alternative to the React Immutability Helpers and Immutable.js.

Changelog

View Changelog

Install

npm install object-path-immutable --save

Quick usage

The following, sets a property without modifying the original object. It will minimize the number of clones down the line. The resulting object is just a plain JS object literal, so be warned that it will not be protected against property mutations (like Immutable.js)

const obj = {
  a: {
    b: 'c',
    c: ['d', 'f']
  }
}

const newObj = immutable.set(obj, 'a.b', 'f')
// {
//   a: {
//     b: 'f',
//     c: ['d', 'f']
//   }
// }

// obj !== newObj
// obj.a !== newObj.a
// obj.a.b !== newObj.a.b

// However:
// obj.a.c === newObj.a.c

Wrap mode

You can also chain the api's and call value() at the end to retrieve the resulting object.

const newObj = immutable.wrap(obj).set('a.b', 'f').del('a.c.0').value()

API

// Premises

const obj = {
  a: {
    b: 'c',
    c: ['d', 'f']
  }
}

import * as immutable from 'object-path-immutable'

set (initialObject, path, value)

Changes an object property.

  • Path can be either a string or an array.
const newObj1 = immutable.set(obj, 'a.b', 'f')
const newObj2 = immutable.set(obj, ['a', 'b'], 'f')

// {
//   a: {
//     b: 'f',
//     c: ['d', 'f']
//   }
// }

// Note that if the path is specified as a string, numbers are automatically interpreted as array indexes.

const newObj = immutable.set(obj, 'a.c.1', 'fooo')
// {
//   a: {
//     b: 'f',
//     c: ['d', 'fooo']
//   }
// }

update (initialObject, path, updater)

Updates an object property.

const obj = {
  a: {
    b: 1
  }
}

const newObj = immutable.update(obj, ['a', 'b'], v => v + 1)

// {
//   a: {
//     b: 2,
//   }
// }

push (initialObject, path, value)

Push into a deep array (it will create intermediate objects/arrays if necessary).

const newObj = immutable.push(obj, 'a.d', 'f')
// {
//   a: {
//     b: 'f',
//     c: ['d', 'f'],
//     d: ['f']
//   }
// }

del (initialObject, path)

Deletes a property.

const newObj = immutable.del(obj, 'a.c')
// {
//   a: {
//     b: 'f'
//   }
// }

Can also delete a deep array item using splice

const newObj = immutable.del(obj, 'a.c.0')
// {
//   a: {
//     b: 'f',
//     c: ['f']
//   }
// }

assign (initialObject, path, payload)

Shallow copy properties.

const newObj = immutable.assign(obj, 'a', { b: 'f', g: 'h' })
// {
//   a: {
//     b: 'f',
//     c: ['d, 'f'],
//     g: 'h'
//   }
// }

insert (initialObject, path, payload, position)

Insert property at the specific array index.

const newObj = immutable.insert(obj, 'a.c', 'k', 1)
// var obj = {
//   a: {
//     b: 'c',
//     c: ['d, 'k' 'f'],
//   }
// }

merge (initialObject, path, value)

Deep merge properties.

const newObj = immutable.merge(obj, 'a.c', {b: 'd'})

Getters (not available in wrap mode)

get (object, path, defaultValue)

Retrieve a deep object property. Imported from object-path for convenience.

Equivalent library with side effects

object-path

More Repositories

1

object-path

A tiny JavaScript utility to access deep properties using a path (for Node and the Browser)
JavaScript
1,037
star
2

scatter

IoC container and out-of-the-box extensibility for Node.js applications
JavaScript
156
star
3

gulp-concat-css

Concatenates css files, bubbling up import statements (as per the standard), and optionally rebasing urls and inlining local import statements.
JavaScript
76
star
4

npm-workspace

A command line utility to ease the `link`-ing of local npm modules
JavaScript
59
star
5

gulp-clone

Clone files in memory in a gulp stream
JavaScript
39
star
6

variations-stream

Streams all the variations (with repetitions) of a set
JavaScript
11
star
7

level-indico

Simple indexing and querying for leveldb
JavaScript
9
star
8

through2-parallel

Parallel Transform stream with an interface lifted from through2
JavaScript
7
star
9

linkemon

Tiny wrapper script around nodemon that will automatically watch all linked modules
Shell
7
star
10

angular-extender

Extend AngularJS applications by injecting module dependencies at build time
JavaScript
6
star
11

gulp-angular-extender

Extend AngularJS applications by injecting module dependencies at build time
JavaScript
5
star
12

minimodel

Minimal, database agnostic Models for Node.js (and the Browser)
JavaScript
4
star
13

benchpress

No fuss benchmarking for Node.js
JavaScript
4
star
14

gulp-multinject

Inject scripts, stylesheets and more into templates and htmls, with support for namespaces
JavaScript
3
star
15

git-workspace

CLI util to keep multiple projects in sync with different remote git repos
JavaScript
3
star
16

spawned

Smart wrapper around child_process.spawn using promises
JavaScript
2
star
17

multistream-merge

Merge multiple streams into one, using Streams2.
JavaScript
2
star
18

powerset-stream

Streams all the possible combinations of subsets of the set given in input
JavaScript
1
star
19

delega

Concise creation of delegated methods for your classes/objects
JavaScript
1
star