ixfx
    Preparing search index...

    Function addValueMutator

    • Returns a function that adds values to a map, using a hashing function to produce a key. Use addValueMutate if you don't need a reusable function.

      const map = new Map(); // Create map
      const mutate = addValueMutator(map, v=>v.name); // Create a mutator using default 'overwrite' policy
      mutate( { name:`Bob`, size:10 }, { name: `Alice`, size: 2 }); // Add values to map
      mutate( {name: `Bob`, size: 11 }); // Change the value stored under key `Bob`.
      map.get(`Bob`); // { name: `Bob`, size: 11 }

      The 'collision policy' determines what to do if the key already exists. The default behaviour is to overwrite the key, just as Map.set would.

      const map = new Map();
      const mutate = addValueMutator(map, v=>v.name, `skip`);
      mutate( { name:`Bob`,size:10 }, { name: `Alice`, size: 2 }); // Add values to map
      mutate( { name:`Bob`, size: 20 }); // This value would be skipped because map already contains 'Bob'
      map.get(`Bob`); // { name: `Bob`, size: 10 }

      Type Parameters

      • V

      Parameters

      • map: Map<string, V>

        Map to modify

      • hasher: ToString<V>

        Hashing function to make a key for a value

      • collisionPolicy: "throw" | "overwrite" | "skip" = ...

        What to do if a value is already stored under a key

      Returns (...values: readonly V[]) => Map<string, V>

      Function