Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
object-path-immutable
Advanced tools
Modify deep object properties without modifying the original object (immutability). Works great with React and Redux.
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.
set(src, path, value)
: value
is returnedupdate(src, path, updater)
: value
will be passed to updater()
and the result returnedset(src, path, ...values)
: values
will be concatenated to src
if src
is an array, otherwise values
will be returnedinsert(src, path, value, at)
: if src
is an array then it will be cloned and value
will be inserted at at
, otherwise [value]
will be returneddel(src, path)
: returns undefined
assign(src, path, target)
: Target is assigned to a clone of src
and returnednpm install object-path-immutable --save
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.b !== newObj.b
// However:
// obj.c === newObj.c
Note that you can also chain the api's and call value()
at the end to retrieve the resulting object.
const newObj = immutable(obj).set('a.b', 'f').del('a.c.0').value()
// Premises
const obj = {
a: {
b: 'c',
c: ['d', 'f']
}
}
import immutable from 'object-path-immutable'
Changes an object property.
const newObj1 = immutable.set(obj, 'a.b', 'f')
const newObj2 = immutable.set(obj, ['a', 'b'], 'f')
const newObj3 = immutable.set(obj, ['a', 'b'], v => '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']
// }
// }
Updates an object property.
const obj = {
a: {
b: 1,
},
}
const newObj = immutable.update(obj, ['a', 'b'], v => v + 1)
// {
// a: {
// b: 2,
// }
// }
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']
// }
// }
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']
// }
// }
Shallow copy properties.
const newObj = immutable.assign(obj, 'a', { b: 'f', g: 'h' })
// {
// a: {
// b: 'f',
// c: ['d, 'f'],
// g: 'h'
// }
// }
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'],
// }
// }
FAQs
Modify deep object properties without modifying the original object (immutability). Works great with React and Redux.
The npm package object-path-immutable receives a total of 41,019 weekly downloads. As such, object-path-immutable popularity was classified as popular.
We found that object-path-immutable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.