StringDictionary<T>

This class implement a typical dictionary using a string as key and the generic type T as value. The underlying implementation relies on an associative array to ensure the best performances. The value can be anything including 'null' but except 'undefined'

Type parameters

  • T

Hierarchy

  • StringDictionary

Index

Properties

count

Find examples in Playground
count: number

Gets the current count

Methods

add

Find examples in Playground
  • add(key: string, value: T): boolean
  • Add a new key and its corresponding value

    Parameters

    • key: string

      the key to add

    • value: T

      the value corresponding to the key

    Returns boolean

    true if the operation completed successfully, false if we couldn't insert the key/value because there was already this key in the dictionary

clear

Find examples in Playground
  • clear(): void
  • Clear the whole content of the dictionary

    Returns void

contains

Find examples in Playground
  • contains(key: string): boolean
  • Check if there's a given key in the dictionary

    Parameters

    • key: string

      the key to check for

    Returns boolean

    true if the key is present, false otherwise

copyFrom

Find examples in Playground
  • This will clear this dictionary and copy the content from the 'source' one. If the T value is a custom object, it won't be copied/cloned, the same object will be used

    Parameters

    • source: StringDictionary<T>

      the dictionary to take the content from and copy to this dictionary

    Returns void

first

Find examples in Playground
  • first<TRes>(callback: function): TRes | null
  • Execute a callback on every occurrence of the dictionary until it returns a valid TRes object. If the callback returns null or undefined the method will iterate to the next key/value pair Note that you can remove any element in this dictionary in the callback implementation

    Type parameters

    • TRes

    Parameters

    • callback: function

      the callback to execute, if it return a valid T instanced object the enumeration will stop and the object will be returned

        • (key: string, val: T): TRes
        • Parameters

          • key: string
          • val: T

          Returns TRes

    Returns TRes | null

    the first item

forEach

Find examples in Playground
  • forEach(callback: function): void
  • Execute a callback on each key/val of the dictionary. Note that you can remove any element in this dictionary in the callback implementation

    Parameters

    • callback: function

      the callback to execute on a given key/value pair

        • (key: string, val: T): void
        • Parameters

          • key: string
          • val: T

          Returns void

    Returns void

get

Find examples in Playground
  • get(key: string): T | undefined
  • Get a value based from its key

    Parameters

    • key: string

      the given key to get the matching value from

    Returns T | undefined

    the value if found, otherwise undefined is returned

getAndRemove

Find examples in Playground
  • getAndRemove(key: string): Nullable<T>
  • Get the element of the given key and remove it from the dictionary

    Parameters

    • key: string

      defines the key to search

    Returns Nullable<T>

    the value associated with the key or null if not found

getOrAdd

Find examples in Playground
  • getOrAdd(key: string, val: T): T
  • Get a value from its key if present in the dictionary otherwise add it

    Parameters

    • key: string

      the key to get the value from

    • val: T

      if there's no such key/value pair in the dictionary add it with this value

    Returns T

    the value corresponding to the key

getOrAddWithFactory

Find examples in Playground
  • getOrAddWithFactory(key: string, factory: function): T
  • Get a value from its key or add it if it doesn't exist. This method will ensure you that a given key/data will be present in the dictionary.

    Parameters

    • key: string

      the given key to get the matching value from

    • factory: function

      the factory that will create the value if the key is not present in the dictionary. The factory will only be invoked if there's no data for the given key.

        • (key: string): T
        • Parameters

          • key: string

          Returns T

    Returns T

    the value corresponding to the key.

remove

Find examples in Playground
  • remove(key: string): boolean
  • Remove a key/value from the dictionary.

    Parameters

    • key: string

      the key to remove

    Returns boolean

    true if the item was successfully deleted, false if no item with such key exist in the dictionary

set

Find examples in Playground
  • set(key: string, value: T): boolean
  • Update a specific value associated to a key

    Parameters

    • key: string

      defines the key to use

    • value: T

      defines the value to store

    Returns boolean

    true if the value was updated (or false if the key was not found)

Generated using TypeDoc