Skip to main content

interface VariantMapBase<K,V,Tp>

undocumented

Companion namespace: VariantMapBase

Extends: FastIterable<T>

Implemented by: RMapBase<K,V,Tp>, VariantMap<K,V>, VariantMapBase.NonEmpty<K,V,Tp>

Type parameters

NameConstraintsDefaultDescription
Kundocumented
Vundocumented
TpVariantMapBase.TypesVariantMapBase.Typesundocumented

Properties

isEmpty

Returns true if the collection is empty.

Definition

readonly isEmpty: boolean;

example
HashMap.empty<number, number>().isEmpty    // => true
HashMap.of([1, 1], [2, 2]).isEmpty // => false

size

Returns the number of entries

Definition

readonly size: number;

example
HashMap.of([1, 1], [2, 2]).size       // => 2

Methods

[Symbol.iterator]

Returns a FastIterator instance used to iterate over the values of this Iterable.

Definition

[Symbol.iterator](): FastIterator<T>;

Overrides

FastIterable.[Symbol.iterator]

assumeNonEmpty

Returns the collection as a .NonEmpty type

Definition

assumeNonEmpty(): WithKeyValue<Tp, K, V>['nonEmpty'];

throws

RimbuError.EmptyCollectionAssumedNonEmptyError if the collection is empty

example
HashMap.empty<number, number>().assumeNonEmpty()   // => throws
const m: HashMap<number, number> = HashMap.of([1, 1], [2, 2])
const m2: HashMap.NonEmpty<number, number> = m // => compiler error
const m3: HashMap.NonEmpty<number, number> = m.assumeNonEmpty()
note

returns reference to this collection

filter

Returns a collection containing only those entries that satisfy given pred predicate.

Definition

filter(pred: (entry: readonly [K, V], index: number, halt: () => void) => boolean, options?: {
    negate?: boolean;
  }): WithKeyValue<Tp, K, V>['normal'];

Parameters

NameTypeDescription
pred(entry: readonly [K, V], index: number, halt: () => void) => booleana predicate function receiving:
- entry: the next entry
- index: the entry index
- halt: a function that, when called, ensures no next elements are passed
options{
    negate?: boolean;
  }
(optional) an object containing the following properties:
- negate: (default: false) when true will negate the predicate
example
HashMap.of([1, 'a'], [2, 'b'], [3, 'c']).filter(entry => entry[0] === 2 || entry[1] === 'c').toArray()
// => [[2, 'b'], [3, 'c']]

forEach

Performs given function f for each entry of the collection, using given state as initial traversal state.

Definition

forEach(f: (entry: readonly [K, V], index: number, halt: () => void) => void, options?: {
    state?: TraverseState;
  }): void;

Parameters

NameTypeDescription
f(entry: readonly [K, V], index: number, halt: () => void) => voidthe function to perform for each entry, receiving:
- entry: the next tuple of a key and value
- index: the index of the element
- halt: a function that, if called, ensures that no new elements are passed
options{
    state?: TraverseState;
  }
(optional) an object containing the following properties:
- state:: (optional) the traversal state
example
HashMap.of([1, 'a'], [2, 'b'], [3, 'c']).forEach((entry, i, halt) => {
console.log([entry[1], entry[0]]);
if (i >= 1) halt();
})
// => logs ['a', 1] ['b', 2]
note

O(N)

get

Returns the value associated with the given key, or given otherwise value if the key is not in the collection.

Definitions

get<UK = K>(key: RelatedTo<K, UK>): V | undefined;

get<UK, O>(key: RelatedTo<K, UK>, otherwise: OptLazy<O>): V | O;

Type parameters

NameDefaultDescription
UKK

Parameters

NameTypeDescription
keyRelatedTo<K, UK>the key to look for
example
const m = HashMap.of([1, 'a'], [2, 'b'])
m.get(2) // => 'b'
m.get(3) // => undefined
m.get(2, 'none') // => 'b'
m.get(3, 'none') // => 'none'

hasKey

Returns true if the given key is present in the collection.

Definition

hasKey<UK = K>(key: RelatedTo<K, UK>): boolean;

Type parameters

NameDefaultDescription
UKK

Parameters

NameTypeDescription
keyRelatedTo<K, UK>the key to look for
example
const m = HashMap.of([1, 'a'], [2, 'b'])
m.hasKey(2) // => true
m.hasKey(3) // => false

mapValues

Returns a collection with the same keys, but where the given mapFun function is applied to each entry value.

Definition

mapValues<V2>(mapFun: (value: V, key: K) => V2): (Tp & KeyValue<K, V2>)['normal'];

Type parameters

NameDescription
V2

Parameters

NameTypeDescription
mapFun(value: V, key: K) => V2a function taking a value and a key, and returning a new value
example
HashMap.of([1, 'a'], [2, 'abc']).mapValues(v => v.length).toArray()
// => [[1, 1], [2, 3]]

nonEmpty

Returns true if there is at least one entry in the collection, and instructs the compiler to treat the collection as a .NonEmpty type.

Definition

nonEmpty(): this is WithKeyValue<Tp, K, V>['nonEmpty'];

example
const m: HashMap<number, number> = HashMap.of([1, 1], [2, 2])
m.stream().first(0) // compiler allows fallback value since the Stream may be empty
if (m.nonEmpty()) {
m.stream().first(0) // compiler error: fallback value not allowed since Stream is not empty
}

removeKey

Returns the collection where the entry associated with given key is removed if it was part of the collection.

Definition

removeKey<UK = K>(key: RelatedTo<K, UK>): WithKeyValue<Tp, K, V>['normal'];

Type parameters

NameDefaultDescription
UKK

Parameters

NameTypeDescription
keyRelatedTo<K, UK>the key of the entry to remove
example
const m = HashMap.of([1, 'a'], [2, 'b'])
m.removeKey(2).toArray() // => [[1, 'a']]
m.removeKey(3) === m // true
note

guarantees same object reference if the key is not present

removeKeyAndGet

Returns a tuple containing the collection of which the entry associated with given key is removed, and the value that is associated with that key. If the key is not present, it will return undefined instead.

Definition

removeKeyAndGet<UK = K>(key: RelatedTo<K, UK>): [WithKeyValue<Tp, K, V>['normal'], V] | undefined;

Type parameters

NameDefaultDescription
UKK

Parameters

NameTypeDescription
keyRelatedTo<K, UK>the key of the entry to remove
example
const m = HashMap.of([1, 'a'], [2, 'b'])
const result = m.removeKeyAndGet(2)
if (result !== undefined) console.log([result[0].toString(), result[1]]) // => logs [HashMap(1 => 'a'), 'b']
console.log(m.removeKeyAndGet(3)) // => logs undefined

removeKeys

Returns the collection where the entries associated with each key in given keys are removed if they were present.

Definition

removeKeys<UK = K>(keys: StreamSource<RelatedTo<K, UK>>): WithKeyValue<Tp, K, V>['normal'];

Type parameters

NameDefaultDescription
UKK

Parameters

NameTypeDescription
keysStreamSource<RelatedTo<K, UK>>a StreamSource of keys to remove
example
const m = HashMap.of([1, 'a'], [2, 'b'])
m.removeKeys([1, 3]).toArray() // => [[2, 'b']]
m.removeKeys([1, 3, 2]).toArray() // => []
m.removeKeys([3, 4, 5]) === m // => true
note

guarantees same object reference if none of the keys are present

stream

Returns a Stream containing all entries of this collection as tuples of key and value.

Definition

stream(): Stream<readonly [K, V]>;

example
HashMap.of([1, 1], [2, 2]).stream().toArray()  // => [[1, 1], [2, 2]]

streamKeys

Returns a Stream containing all keys of this collection.

Definition

streamKeys(): Stream<K>;

example
HashMap.of([[1, 'a'], [2, 'b']]).streamKeys().toArray()   // => [1, 2]

streamValues

Returns a Stream containing all values of this collection.

Definition

streamValues(): Stream<V>;

example
HashMap.of([[1, 'a'], [2, 'b']]).streamValues().toArray()   // => ['a', 'b']

toArray

Returns an array containing all entries in this collection.

Definition

toArray(): (readonly [K, V])[];

example
HashMap.of([1, 'a'], [2, 'b']).toArray()   // => [[1, 'a'], [2, 'b']]
note

O(log(N)) @note it is safe to mutate the returned array, however, the array elements are not copied, thus should be treated as read-only

toJSON

Returns a JSON representation of this collection.

Definition

toJSON(): ToJSON<(readonly [K, V])[]>;

example
HashMap.of([1, 'a'], [2, 'b']).toJSON()   // => { dataType: 'HashMap', value: [[1, 'a'], [2, 'b']] }

toString

Returns a string representation of this collection.

Definition

toString(): string;

example
HashMap.of([1, 'a'], [2, 'b']).toString()   // => HashMap(1 => 'a', 2 => 'b')