Options
All
  • Public
  • Public/Protected
  • All
Menu

Class SmartMap<K, V>

### SmartMap

Like WeakMap but can also store values using primitive keys


Example usage:

import { SmartMap } from "@thalesrc/js-utils";

const aMap = new SmartMap();

aMap.set("foo", "foo");
aMap.set(1, "thales rocks");

console.log(aMap.size) // 2

aMap.set({}, "thales rocks again");
console.log(aMap.size) // 2

const anObject = {};
aMap.set(anObject, "thales rocks again and again");
console.log(aMap.size) // 3
console.log(aMap.get(anObject)) // "thales rocks again and again"

template

K Typeof Key

template

V Typeof Value

Type parameters

  • K

  • V

Hierarchy

  • Map<K, V>
    • SmartMap

Index

Properties

Protected [KEY__WEAKMAP]

[KEY__WEAKMAP]: WeakMap<Object, V> = new WeakMap<Object, V>()

The WeakMap object to store values with associated non-primitive (object) keys

[Symbol.toStringTag]

[Symbol.toStringTag]: string

size

size: number

Static Protected KEY__WEAKMAP

KEY__WEAKMAP: symbol = KEY__WEAKMAP

Symbol refference of weakmap property

Static Map

Map: MapConstructor

Static TYPES_TO_STORE_IN_WEAKMAP

TYPES_TO_STORE_IN_WEAKMAP: ReadonlyArray<string> = Object.freeze(['object', 'function'])

Allowed key types to store them in a weakmap instance

Methods

[Symbol.iterator]

  • [Symbol.iterator](): IterableIterator<[K, V]>
  • Returns an iterable of entries in the map.

    Returns IterableIterator<[K, V]>

clear

  • clear(): void
  • Returns void

delete

  • delete(key: K): boolean
  • Removes the associated value from the SmartMap object,

    Parameters

    • key: K

      The key of the element to remove from the SmartMap object.

    Returns boolean

    true if an element in the SmartMap object existed and has been removed, or false if the element does not exist. SmartMap.prototype.has(key) will return false afterwards.

entries

  • entries(): IterableIterator<[K, V]>
  • Returns an iterable of key, value pairs for every entry in the map.

    Returns IterableIterator<[K, V]>

forEach

  • forEach(callbackfn: function, thisArg?: any): void
  • Parameters

    • callbackfn: function
        • (value: V, key: K, map: Map<K, V>): void
        • Parameters

          • value: V
          • key: K
          • map: Map<K, V>

          Returns void

    • Optional thisArg: any

    Returns void

get

  • get(key: K): V
  • Returns the value associated to the key, or undefined if there is none.

    Parameters

    • key: K

      The key of the element to return from the SmartMap object.

    Returns V

    The value associated to the key

has

  • has(key: K): boolean
  • Returns a boolean asserting whether a value has been associated to the key in the SmartMap object or not.

    Parameters

    • key: K

      The key of the element to test for presence in the SmartMap object.

    Returns boolean

    A boolean asserting whether a value has been associated to the key in the SmartMap object or not.

Protected isStorableInWeakMap

  • isStorableInWeakMap(key: K): boolean
  • Returns a boolean whether if the key is storable in weakmap or not

    Parameters

    • key: K

      Key to check if it can be stored in weakmap

    Returns boolean

    A boolean whether if the key is storable in weakmap or not

keys

  • keys(): IterableIterator<K>
  • Returns an iterable of keys in the map

    Returns IterableIterator<K>

set

  • set(key: K, value: V): this
  • Sets the value for the key in the SmartMap object.

    Parameters

    • key: K

      The key of the element to add to the SmartMap object.

    • value: V

      The value of the element to add to the SmartMap object.

    Returns this

    Returns the SmartMap object.

values

  • values(): IterableIterator<V>
  • Returns an iterable of values in the map

    Returns IterableIterator<V>

Generated using TypeDoc