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.
setimmutable
Advanced tools
An alternative to lodash.set when your object necessary working with immutable objects.
An alternative to lodash.set when your object necessary working with immutable objects.
Using npm:
npm install --save setimmutable
In Node.js:
const set = require('setimmutable');
In a simple object when do you use _.set
the data is updated if it is frozen nothing happens. The SetImmutable update the object tree until the final element to be replaced.
// const setLodash = require('lodash.set')
// const setImmutable = require('setimmutable')
// With mutable object
const nextObjMutable = setLodash(originalObj, path, 3) // Update the element and return the original object.
nextObjMutable === originalObj // true
// With immutable object
const nextObjImmutable = setImmutable(originalObj, path, 3) // Update the tree element and return a new object.
nextObjImmutable === originalObj // false
To update the object tree is used the reference constructor. This makes a new object and assigns all old properties to the new object. But there are times when the constructor is complex and requires special properties to be declared.
// Simple Constructor
class SimpleConstructor {
constructor() { /* ... */ }
}
// Complex Constructor
class ComplexConstructor {
constructor(requiredArg, especialArg) { /* ... */ }
}
SetImmutable load the custom Clone to make a new object.
Example:
// const clone = require('setimmutable/clone')
function customClone (objValue, srcValue) {
switch (objValue.constructor) {
// My custom class
case MyClass: return MyClass.parse(objValue) // Return new object instance of MyClass
// My second custom class
case MySecondClass: return new MySecondClass(...myArgs) // Return new object instance of MySecondClass
// Set default clone
default: return clone(objValue)
}
}
setImmutable(originalObject, path, newValue, customClone)
set(object, path, value, [customClone])
Sets the value at path of object. If a portion of path doesn't exist, it's created.
Note: This not method mutates object. It re-create the object defined on the path.
Arguments
Returns
Example 1 (on RunKit)
const object = {}
set(object, '[0][1][2]', 'a')
// => { '0': { '1': {'2': 'a' } } }
Example 2 (on RunKit)
const object = []
function customClone (objValue, srcValue) {
switch (objValue.constructor) {
case Person: return Person.clone(objValue)
/* ... */
/* default: return require('setimmutable/clone')(objValue) */
}
}
set(object, '[0].people.[1].firstName', 'Lucky', customClone)
// => [ { 'people': [..., Person { 'firstName': 'Lucky' } ] } ]
With SetImmutable:
const set = require('setimmutable')
function Reducer (state = initialState, action) {
switch (action.type) {
case 'UPDATE_PERSON': {
return set(state, ['people', action.id, 'firstName'], action.firstName)
}
/* ... */
}
}
Without SetImmutable:
function Reducer (state = initialState, action) {
switch (action.type) {
case 'UPDATE_PERSON': {
return {
...state,
people: state.people.map((person, index) => {
if (person.id === action.id) {
return {
...state.people[index],
firstName: action.firstName
}
} else {
return person
}
})
}
}
/* ... */
}
}
FAQs
An alternative to lodash.set when your object necessary working with immutable objects.
The npm package setimmutable receives a total of 320 weekly downloads. As such, setimmutable popularity was classified as not popular.
We found that setimmutable 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.