CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-immutability-helper

Mutate a copy of data without changing the original source

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

map-set-operations.mddocs/

Map and Set Operations

Specialized operations for ES6 Map and Set data structures. These operations provide type-safe ways to add and remove entries while maintaining immutability.

Capabilities

Add Operation

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 bob

Remove Operation

Removes 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'])

Complex Examples

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'] }
);

Error Handling

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
  • For Maps, $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

Performance Notes

  • Operations preserve reference equality when no changes occur
  • $add only creates a new Map/Set if values are actually different
  • $remove only creates a new Map/Set if keys/values actually exist
  • For Maps, existing entries are compared by value for change detection
  • For Sets, duplicate values in $add are handled naturally (Sets ignore duplicates)
  • Uses shallow copying of the Map/Set structure for optimal performance

docs

array-operations.md

extension-system.md

function-operations.md

index.md

map-set-operations.md

object-operations.md

tile.json