Skip to content

azer/change-object

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

change-object

Clone and change given object paths.

Install

$ npm install change-object

Usage

A simple example:

var change = require('change-object')
var state = {
  loading: true,
  start: 0,
  end: 15
};

var updated = change(state, { loading: false })

updated.loading
// => false

updated
// => { loading: false, start: 0, end: 15 }

It supports changing deep paths:

var state = {
  products: { eggs: [{ kind: 'white egg', amount: 250 }] }
}

var updated = change(state, {
  'products.eggs[0].kind': 'brown egg'
})

updated
// => products: { eggs: [{ kind: 'brown egg', amount: 250 }] }

updated.products.eggs[0].kind
// => brown egg

Custom Clonning

By default, it'll clone your object by stringifying & parsing method, which may not fit your use case if you got functions in your object. It's preferred for performance reasons, and you can of course choose your own clonning function by simply calling it with a clonning function:

var change = require('change-object')(customCloneFn)

function customCloneFn (obj) {
  return {...}
}

About

Clone and change given object paths.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published