tread
v1.2.0
Published
Update an object with another object containing just the new key-value pairs. Objects can be infinitely nested. Perfect for updating with PUT requests in your REST API.
Downloads
2
Maintainers
Readme
Installation
npm install --save tread
Usage
// user from your database
var user = {
details: {
name: {
first: 'Jane',
last: 'Smith'
},
address: {
street: {
number: '5',
name: 'Oak Lane'
},
coordinates: {
lat: 50,
lng: 50
}
}
},
gender: 'm'
}
// userUpdates from your PUT request
var userUpdates = {
details: {
name: {
first: 'John'
},
address: {
coordinates: {
lat: 100
}
}
}
}
// perform the update
user = tread(user, userUpdates, strict)
// save the updated user in the database
user.save(...)
Strict mode
strict (Boolean, default = false): only updates object if the key already existed. For example:
tread({ name: 'Jane' }, { name: 'John', age: 20 }, true)
// outputs
{ name: 'John' }
tread({ name: 'Jane' }, { name: 'John', age: 20 }, false)
// OR
tread({ name: 'Jane' }, { name: 'John', age: 20 })
// outputs
{ name: 'John', age: 20 }