package
Immutably update deeply nested data structures with ease.
npm install immutable-deep-updateGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Immutably update deeply nested data structures with ease.
const { over, set, view } = require('./src')
const user = {
firstname: 'Han',
location: { city: 'Paris' }
friends: [
{ fisrtname: 'Luke', location: { city: 'New York' } }
{ fisrtname: 'Darth Vador', location: { city: 'Dark star' } }
]
}
console.log(
over('friends[0].location.city', city => city + ' City', user)
)
/* => {
firstname: 'Han',
location: { city: 'Paris' }
friends: [
{ fisrtname: 'Luke', location: { city: 'New York City' } },
{ fisrtname: 'Darth Vador', location: { city: 'Dark star' } }
]
} */
console.log(
over('friends', friends => friends.concat({
firstname: 'Chewi',
location: { city: '?' }
}), user)
)
/* => {
firstname: 'Han',
location: { city: 'Paris' }
friends: [
{ fisrtname: 'Luke', location: { city: 'New York' } },
{ fisrtname: 'Darth Vador', location: { city: 'Dark star' } },
{ fisrtname: 'Chewi' location: { city: '?' } }
]
} */console.log(
view('friends[..].location.city', user)
)
// => [ 'New York', 'Dark star' ]
console.log(
set('friends[..].location.city', 'Tokyo', user)
)
/* => {
firstname: 'Han',
location: { city: 'Paris' }
friends: [
{ fisrtname: 'Luke', location: { city: 'Tokyo' } },
{ fisrtname: 'Darth Vador', location: { city: 'Tokyo' } }
]
} */const state = {
usersById: {
'a': { firstname: 'Rick', id: 'a' },
'b': { firstname: 'Morty', id: 'b' }
}
}
console.log(
view('usersById{..}.firstname', state)
)
// => { a: 'Rick', b: 'Morty' }
console.log(
over('usersById{..}', user => `${user.firstname} - ${user.id}`, state)
)
// => { usersById: { a: 'Rick - a', b: 'Morty - b' } }'azerty' => prop 'azerty''aze.rty' => compose (prop 'aze') (prop 'rty')'aze["rty"]' => compose (prop 'aze') (prop 'rty')'0' => index 0'[0]' => index 0'aze[0].rty' => compose (prop 'aze') (index 0) (prop 'rty')'aze.0.rty' => compose (prop 'aze') (index 0) (prop 'rty')'[..]' => map over all the items of an array'{..}' => map over all values of an objectEstimated unpacked size across 2 packages.
1.3 MB
Includes dependencies.