utilitish - v0.0.10
    Preparing search index...

    Interface Map<K, V>

    interface Map<K, V> {
        toList(type?: "keys"): K[];
        toList(type: "values"): V[];
        toList(type: "object"): Record<string, V>;
        toList(type: "entries"): [K, V][];
        toList(): [K, V][];
        toObject<K extends PropertyKey, V>(this: Map<K, V>): Record<K, V>;
        ensureArray<L extends any[]>(this: Map<K, L>, key: K): L;
    }

    Type Parameters

    • K
    • V
    Index

    Methods

    • Converts the Map into a list or specific structure based on the selected conversion type. Supports multiple output formats: entries, keys, values, or a plain object.

      Parameters

      • Optionaltype: "keys"

        The conversion type ('keys' | 'values' | 'entries' | 'object')

      Returns K[]

      Converted output as specified by the type parameter

      If type is 'object' with non-compatible keys or if type is unknown

      const map = new Map([['a', 1], ['b', 2]]);
      map.toList(); // [['a', 1], ['b', 2]]
      map.toList('keys'); // ['a', 'b']
      map.toList('values'); // [1, 2]
      map.toList('object'); // { a: 1, b: 2 }
      • entries (default): Returns array of [K, V] pairs
      • keys: Returns array of all keys
      • values: Returns array of all values
      • object: Returns Record with string keys (requires keys to be string/number/symbol)
    • Parameters

      • type: "values"

      Returns V[]

    • Parameters

      • type: "object"

      Returns Record<string, V>

    • Parameters

      • type: "entries"

      Returns [K, V][]

    • Returns [K, V][]

    • Converts a Map to a plain JavaScript object. Each key/value pair in the Map becomes a property/value in the resulting object. Validates that all keys are valid PropertyKey types.

      Type Parameters

      • K extends PropertyKey

        Type of keys (must extend PropertyKey: string | number | symbol)

      • V

        Type of values in the Map

      Parameters

      Returns Record<K, V>

      A plain object with Map entries as properties

      If any key is null, undefined, or not a PropertyKey type

      const map = new Map<string, number>([['a', 1], ['b', 2]]);
      map.toObject(); // { a: 1, b: 2 }

      const numKeyMap = new Map<number, string>([[1, 'one'], [2, 'two']]);
      numKeyMap.toObject(); // { 1: 'one', 2: 'two' }
      • Provides validation and error handling for property key compatibility
      • Supports string, number, and symbol keys
      • Returns a new object instance each time (no modification to original)
    • Ensures that the Map has an entry for the given key with an array as value. If the key doesn't exist, initializes it with an empty array. Validates that the existing value (if any) is indeed an array.

      Type Parameters

      • L extends any[]

        The array type stored as values (extends Array)

      Parameters

      • this: Map<K, L>
      • key: K

        The key to look up or initialize

      Returns L

      The array associated with the key (either existing or newly created)

      If key is null/undefined or if the existing value is not an array

      const map = new Map<string, number[]>();
      const arr1 = map.ensureArray('items'); // Returns [], and ['items'] => [] is set in map
      arr1.push(42);

      const arr2 = map.ensureArray('items'); // Returns same array with [42]
      arr1 === arr2; // true (same reference)
      • Modifies the Map in place (lazy initialization pattern)
      • Useful for Maps that accumulate items by key
      • Throws if the existing value for a key is not an array
      • Key must not be null or undefined