
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Simple utility for changing object using dot notation string without mutate it.
npm install immutee --save
const Immutee = require('immutee');
// import Immutee from 'immutee';
const object = {
inner: {
deeper: {
deepest: "got it!"
}
},
shallow: [
{
zero: "shallow zero"
}, {
one: "shallow one"
}, {
two: [
"deep zero",
[
"deeper zero",
"deeper one",
"deeper two", {
deep: {
deepest: "It Works!"
}
},
],
]
},
],
};
const newObject = Immutee(object)
.set('inner.deeper.deepest', 'nice!')
.set('shallow.0.zero', 'shallow zero modified') // Usage with array
.set('shallow.2.two.1.1', (result) => {
return `${result} modified`;
})
.merge('shallow', [{ three: 'shallow three' }])
.delete('shallow.1') // Remove array key 1 of shallow
.done(); // always call done() in order to return the new object
console.log(newObject);
It will create new object like this:
{
inner: {
deeper: {
deepest: 'nice!'
}
},
shallow: [
{
zero: 'shallow zero modified'
}, {
two: [
'deep zero',
[
'deeper zero',
'deeper one modified',
'deeper two', {
deep: {
deepest: 'It Works!'
}
},
],
]
}, {
three: 'shallow three'
},
],
}
Modify a nested property by a dot path
// Setter
var obj = {foo: {bar: 'a'}};
var obj1 = Immutee(obj).set('foo.bar', 'b').done();
//obj1 => {foo: {bar: 'b'}}
var obj2 = Immutee(obj).set('foo.baz', 'x').done();
//obj2 => {foo: {bar: 'b', baz: 'x'}}
where obj
, obj1
, obj2
, obj3
all are different objects.
Use a function to modify the selected property, where first argument is the old value.
// Setter
var obj = {foo: {bar: 'a'}};
// Setter where value is a function (get and set current value)
Immutee(obj).set('foo.bar', (v) => v + 'bc').done();
//=> {foo: {bar: 'abc'}}
Modify a nested array
var obj = {foo: [{ bar: 'gold-unicorn'}, 'white-unicorn', 'silver-unicorn']};
// Index into array
Immutee(obj).set('foo.1', 'platin-unicorn').done();
//=> {foo: [{bar: 'gold-unicorn'}, 'platin-unicorn', 'silver-unicorn']}
Immutee(obj).set('foo.0.bar', 'platin-unicorn').done();
//=> {foo: [{bar: 'platin-unicorn'}, 'white-unicorn', 'silver-unicorn']}
// Index into array with $end
Immutee(obj).set('foo.$end', 'platin-unicorn').done();
//=> {foo: [{ bar: 'gold-unicorn'}, 'white-unicorn', 'platin-unicorn']}
Delete a nested property/array by a dot path
var obj = {foo: [{ bar: 'gold-unicorn'}, 'white-unicorn', 'silver-unicorn']};
// delete
Immutee(obj).delete('foo.$end').done();
//=> {foo: [{ bar: 'gold-unicorn'}, 'white-unicorn']}
Immutee(obj).delete('foo.0.bar').done();
//=> {foo: [{}, 'white-unicorn', 'silver-unicorn']}
Toggle a boolean a value by a dot path.
var obj = {foo: { bar: true } };
// toggle
Immutee(obj).toggle('foo.bar').done();
//=> {foo: { bar: false } }
Merge a value by a dot path.
The target value must be an object, array, null, or undefined.
var obj = {foo: { bar: {a:1, b:2 } };
// merge object
Immutee(obj).merge('foo.bar', {c:3}).done();
//=> {foo: { bar:{ a:1, b:2, c:3} } }
var arr = {foo: { bar: [1, 2] } };
// merge array
Immutee(arr).merge('foo.bar', [3, 4]).done();
//=> {foo: { bar:[1, 2, 3, 4 ] }
FAQs
Simple utility for changing object without mutate it.
The npm package immutee receives a total of 67 weekly downloads. As such, immutee popularity was classified as not popular.
We found that immutee 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.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.