Options
All
  • Public
  • Public/Protected
  • All
Menu

External module @corpuscule/utils/lib/setters

This module provides tools to change complex values (like objects and arrays) in the Map or WeakMap store without explicit check for their existence. If the value does not exist, it will be created.

Usage

import {setArray, setObject} from '@corpuscule/utils/lib/setters'

Index

Functions

Functions

setArray

  • setArray<K, V>(store: Map<K, V[]>, key: K, array: ReadonlyArray<V>): void
  • setArray<K, V>(store: WeakMap<K, V[]>, key: K, array: ReadonlyArray<V>): void
  • Adds new elements listed in the array parameter to the array in the store under the key. If the array does not exist yet, it will be created.

    overload

    This overload accepts Map instance as a store.

    Type parameters

    • K

    • V

    Parameters

    • store: Map<K, V[]>

      a Map instance which contains or should contain the target array.

    • key: K

      a key to access the target array.

    • array: ReadonlyArray<V>

      an array with new elements to add to the target array.

      Example

      const store = new Map<string, string[]>();
      
      setArray(store, 'foo', ['bar']);
      store.get('foo'); // ['bar']
      
      setArray(store, 'foo', ['baz']);
      store.get('foo'); // ['bar', 'baz']

    Returns void

  • overload

    This overload accepts WeakMap instance as a store.

    example
    const store = new WeakMap<object, string[]>();
    const key = {};
    
    setArray(store, key, ['bar']);
    store.get(key); // ['bar']
    
    setArray(store, key, ['baz']);
    store.get(key); // ['bar', 'baz']

    Type parameters

    • K: object

    • V

    Parameters

    • store: WeakMap<K, V[]>

      a WeakMap instance.

    • key: K
    • array: ReadonlyArray<V>

    Returns void

setObject

  • setObject<K, V>(store: Map<K, V>, key: K, object: Readonly<Partial<V>>): void
  • setObject<K, V>(store: WeakMap<K, V>, key: K, object: Readonly<Partial<V>>): void
  • Adds new properties listed in the object parameter to the object in the store under the key. If the object does not exist yet, it will be created.

    overload

    This overload accepts Map instance as a store.

    example
    const store = new Map<string, Record<string, string>>();
    
    setArray(store, 'foo', {bar: 1});
    store.get('foo'); // {bar: 1}
    
    setArray(store, 'foo', {baz: 2});
    store.get('foo'); // {bar: 1, baz: 2}

    Type parameters

    • K

    • V: object

    Parameters

    • store: Map<K, V>

      a Map instance which contains or should contain the target object.

    • key: K

      a key to access the target object.

    • object: Readonly<Partial<V>>

      an object with new properties to add to the target object.

    Returns void

  • overload

    This overload accepts WeakMap instance as a store.

    example
    const store = new WeakMap<object, Record<string, string>>();
    const key = {};
    
    setArray(store, key, {bar: 1});
    store.get(key); // {bar: 1}
    
    setArray(store, key, {baz: 2});
    store.get(key); // {bar: 1, baz: 2}

    Type parameters

    • K: object

    • V: object

    Parameters

    • store: WeakMap<K, V>

      a WeakMap instance.

    • key: K
    • object: Readonly<Partial<V>>

    Returns void

Generated using TypeDoc