Mutate a copy of data without changing the original source
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Specialized operations for ES6 Map and Set data structures. These operations provide type-safe ways to add and remove entries while maintaining immutability.
Adds entries to a Map or Set. For Maps, provide key-value pairs. For Sets, provide values to add.
/**
* Add entries to a Map or Set
* For Map: Array of [key, value] pairs
* For Set: Array of values to add
* @param values - Entries to add
* @returns New Map or Set with added entries
*/
{ $add: ReadonlyArray<[K, V]> | ReadonlyArray<T> }Usage Examples:
import update from "immutability-helper";
// Add to Map
const userMap = new Map([
['alice', { age: 25, role: 'user' }],
['bob', { age: 30, role: 'admin' }]
]);
const updatedMap = update(userMap, {
$add: [
['charlie', { age: 28, role: 'user' }],
['diana', { age: 32, role: 'admin' }]
]
});
// Result: Map with 4 entries including charlie and diana
// Update existing Map entry
const modified = update(userMap, {
$add: [['alice', { age: 26, role: 'admin' }]]
});
// Result: alice's entry is updated with new values
// Add to Set
const tagSet = new Set(['frontend', 'javascript']);
const updatedSet = update(tagSet, {
$add: ['typescript', 'react']
});
// Result: Set(['frontend', 'javascript', 'typescript', 'react'])
// Nested Map operation
const data = {
users: new Map([['alice', { active: true }]]),
config: { theme: 'dark' }
};
const result = update(data, {
users: { $add: [['bob', { active: false }]] }
});
// Result: data.users now has both alice and bobRemoves entries from a Map or Set by key (for Maps) or value (for Sets).
/**
* Remove entries from a Map or Set
* @param keys - Keys (for Map) or values (for Set) to remove
* @returns New Map or Set with specified entries removed
*/
{ $remove: ReadonlyArray<K> }Usage Examples:
import update from "immutability-helper";
// Remove from Map
const userMap = new Map([
['alice', { age: 25 }],
['bob', { age: 30 }],
['charlie', { age: 28 }]
]);
const filtered = update(userMap, { $remove: ['bob', 'charlie'] });
// Result: Map with only alice entry
// Remove from Set
const tagSet = new Set(['frontend', 'backend', 'mobile', 'web']);
const reduced = update(tagSet, { $remove: ['mobile', 'backend'] });
// Result: Set(['frontend', 'web'])
// Remove non-existent entries (no effect)
const unchanged = update(userMap, { $remove: ['nonexistent'] });
// Result: Same Map (reference preserved if no changes)
// Nested Set operation
const data = {
tags: new Set(['js', 'ts', 'react', 'vue']),
meta: { count: 4 }
};
const result = update(data, {
tags: { $remove: ['vue'] }
});
// Result: data.tags = Set(['js', 'ts', 'react'])import update from "immutability-helper";
// Working with nested Maps
const cache = new Map([
['user:1', { name: 'Alice', permissions: new Set(['read', 'write']) }],
['user:2', { name: 'Bob', permissions: new Set(['read']) }]
]);
// Add permission to nested Set
const updated = update(cache, {
$add: [[
'user:2',
{
name: 'Bob',
permissions: update(
cache.get('user:2')?.permissions || new Set(),
{ $add: ['write', 'admin'] }
)
}
]]
});
// Multiple operations on same Map
const multiOp = update(userMap, {
$add: [['newUser', { age: 35 }]],
// Note: Cannot combine $add and $remove in same spec
// Use separate update calls or nested structure
});
// Correct way to add and remove in sequence
const sequential = update(
update(userMap, { $add: [['newUser', { age: 35 }]] }),
{ $remove: ['oldUser'] }
);Map and Set operations validate their inputs and throw descriptive errors:
$add and $remove can only be used on Map or Set targets$add requires the spec value to be an array$remove requires the spec value to be an array$add values must be [key, value] pairs// These will throw errors:
update({}, { $add: [['key', 'value']] }); // Target must be Map or Set
update(new Map(), { $add: 'value' }); // Spec must be array
update(new Set(), { $remove: 'value' }); // Spec must be array$add only creates a new Map/Set if values are actually different$remove only creates a new Map/Set if keys/values actually exist$add are handled naturally (Sets ignore duplicates)