
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
mutate-cow
Advanced tools
import mutate from 'mutate-cow';
const animals = deepFreeze({
cats: ['ragamuffin', 'shorthair', 'maine coon'],
});
const newAnimals = mutate(animals)
.set('dogs', ['hound'])
.update('cats', (ctx) => {
ctx.write().push('bobtail');
})
.final();
This module allows you to update an immutable object as if it were mutable. It has copy-on-write semantics, so properties are only changed if you write to them. (In fact, if you perform no writes, the same object is returned back.) This makes it useful in conjuction with libraries like React, where state may be compared by reference.
No cows were harmed in the making of this code.
Returns a "context" object which can modify a copy of source.
const foo = deepFreeze({bar: {baz: []}});
const ctx = mutate(foo);
By default, you can mutate primitves, arrays, and plain objects. However, if strict is set to true, the following features are enabled:
Returns the current working copy of the context's source object, or just source if no changes were made.
ctx.read() === foo; // no changes
ctx.set('bar', 'baz', ['qux']);
ctx.read().bar.baz[0] === 'qux'; // changes
Returns the current working copy of the context's source object. Makes a shallow copy of source first if no changes were made.
You normally don't need to call write. It's mainly useful for accessing methods on copied objects (e.g., array methods).
ctx.get('bar', 'baz').write().push('qux');
ctx.read().bar.baz[0] === 'qux';
Returns a child context object for the given path.
Passing zero arguments returns ctx.
ctx.get() === ctx;
ctx.get('bar').read() === foo.bar;
ctx.get('bar', 'baz').read().length === 0;
Sets the given path to value on the current working copy. Returns ctx.
Passing zero property names (i.e., only a value) sets the current context's value.
const qux = ['qux'];
// these all do the same thing
ctx.set({bar: {baz: qux}});
ctx.set('bar', {baz: qux});
ctx.set('bar', 'baz', qux);
ctx.get('bar').set({baz: qux});
ctx.get('bar').set('baz', qux);
ctx.get('bar', 'baz').set(qux);
Calls updater(ctx.get(...path)) and returns ctx.
const copy = ctx
.update('bar', 'baz', (bazCtx) => {
bazCtx.write().push('qux');
})
.final();
copy.bar.baz[0] === 'qux';
Returns the parent context of ctx.
ctx.parent() === null;
ctx.get('bar').parent() === ctx;
ctx.get('bar', 'baz').parent() === ctx.get('bar');
Returns the root context of ctx.
ctx.root() === ctx;
ctx.get('bar').root() === ctx;
ctx.get('bar', 'baz').root() === ctx;
Revokes ctx so that it can no longer be used. Returns undefined.
Attempting to use any method other than isRevoked on a revoked context will throw an error. This sets all internal properties to null so that there's no longer any reference to the source object or copy.
Returns a boolean indicating whether ctx has been revoked.
This is the same as read, except it also revokes the context, and in strict mode restores all property descriptors and extensibility information. This is what you call to get the final copy.
const copy = mutate(foo, /* strict = */ true).set('bar', 'baz', 'qux').final();
Object.isFrozen(copy) === true; // since `foo` was frozen, `copy` will be too
Returns ctx.root().final().
FAQs
Update immutable objects as if they were mutable with copy-on-write
We found that mutate-cow demonstrated a healthy version release cadence and project activity because the last version was released less than 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's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.

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.