Skip to main content

interface BiMap.Builder<K,V>

A mutable BiMap builder used to efficiently create new immutable instances. See the BiMap documentation and the BiMap.Builder API documentation

Implemented by: BiMapBuilder<K,V>

Type parameters

NameDescription
Kthe key type
Vthe value type

Properties

isEmpty

Returns true if there are no entries in the builder.

Definition

readonly isEmpty: boolean;

example
BiMap.of([[1, 'a'], [2, 'b']]).toBuilder().isEmpty
// => false

size

Returns the amount of entries in the builder.

Definition

readonly size: number;

example
BiMap.of([[1, 'a'], [2, 'b']]).toBuilder().size
// => 2

Methods

addEntries

Adds given entries to the builder.

Definition

addEntries(entries: StreamSource<readonly [K, V]>): boolean;

Parameters

NameTypeDescription
entriesStreamSource<readonly [K, V]>
example
const m = BiMap.of([1, 'a'], [2, 'b']).toBuilder()
m.addEntries([1, 'a'], [3, 'c']]) // => true
m.addEntries([]) // => false

addEntry

Adds the given entry to the builder, where the entry key is associated with the entry value.

Definition

addEntry(entry: readonly [K, V]): boolean;

Parameters

NameTypeDescription
entryreadonly [K, V]
example
const m = BiMap.of([1, 'a'], [2, 'b']).toBuilder()
m.addEntry([1, 'a']) // => false
m.addEntry([1, 'b']) // => true

build

Returns an immutable collection instance containing the entries in this builder.

Definition

build(): BiMap<K, V>;

example
const m = BiMap.of([1, 'a'], [2, 'b']).toBuilder()
const m2: BiMap<number, string> = m.build()

forEach

Performs given function f for each entry of the builder.

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 element, receiving:
- entry: the next key-value entry
- 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 traverse state
throws

RibuError.ModifiedBuilderWhileLoopingOverItError if the builder is modified while looping over it

example
BiMap.of([1, 'a'], [2, 'b'], [3, 'c']).toBuilder().forEach((entry, i, halt) => {
console.log([entry[1], entry[0]]);
if (i >= 1) halt();
})
// => logs ['a', 1] ['b', 2]
note

O(N)

getKey

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

Definitions

getKey<UV = V>(value: RelatedTo<V, UV>): K | undefined;

getKey<UV, O>(value: RelatedTo<V, UV>, otherwise: OptLazy<O>): K | O;

Type parameters

NameDefaultDescription
UVV

Parameters

NameTypeDescription
valueRelatedTo<V, UV>the value to look for
example
const m = BiMap.of([1, 'a'], [2, 'b']).toBuilder()
m.getKey('b') // => 2
m.getKey('z') // => undefined
m.getKey('b', 'none') // => 2
m.getKey('z', 'none') // => 'none'

getValue

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

Definitions

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

getValue<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 = BiMap.of([1, 'a'], [2, 'b']).toBuilder()
m.getValue(2) // => 'b'
m.getValue(3) // => undefined
m.getValue(2, 'none') // => 'b'
m.getValue(3, 'none') // => 'none'

hasKey

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

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 = BiMap.of([1, 'a'], [2, 'b']).toBuilder()
m.hasKey(2) // => true
m.hasKey(3) // => false

hasValue

Returns true if the given value is present in the builder.

Definition

hasValue<UV = V>(value: RelatedTo<V, UV>): boolean;

Type parameters

NameDefaultDescription
UVV

Parameters

NameTypeDescription
valueRelatedTo<V, UV>the value to look for
example
const m = BiMap.of([1, 'a'], [2, 'b']).toBuilder()
m.hasValue('a') // => true
m.hasValue('z') // => false

removeKey

Removes the entries related to given key from the builder.

Definitions

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

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

Type parameters

NameDefaultDescription
UKK

Parameters

NameTypeDescription
keyRelatedTo<K, UK>the key to remove
example
const m = BiMap.of([1, 'a'], [2, 'b']).toBuilder()
m.removeKey(2) // => 'b'
m.removeKey(3) // => undefined
m.removeKey(3, 'c') // => 'c'

removeKeys

Removes the entries related to the given keys StreamSource from the builder.

Definition

removeKeys<UK = K>(keys: StreamSource<RelatedTo<K, UK>>): boolean;

Type parameters

NameDefaultDescription
UKK

Parameters

NameTypeDescription
keysStreamSource<RelatedTo<K, UK>>
example
const m = BiMap.of([1, 'a'], [2, 'b']).toBuilder()
m.removeKeys([3, 4, 5]) // => false
m.removeKeys([1, 10]) // => true

removeValue

Removes the entries related to given value from the builder.

Definitions

removeValue<UV = V>(value: RelatedTo<V, UV>): K | undefined;

removeValue<UV, O>(value: RelatedTo<V, UV>, otherwise: OptLazy<O>): K | O;

Type parameters

NameDefaultDescription
UVV

Parameters

NameTypeDescription
valueRelatedTo<V, UV>the value to remove
example
const m = BiMap.of([1, 'a'], [2, 'b']).toBuilder()
m.removeValue('b') // => 2
m.removeValue('c') // => undefined
m.removeValue('c', 0) // => 0

removeValues

Removes the entries related to the given values StreamSource from the builder.

Definition

removeValues<UV = V>(values: StreamSource<RelatedTo<V, UV>>): boolean;

Type parameters

NameDefaultDescription
UVV

Parameters

NameTypeDescription
valuesStreamSource<RelatedTo<V, UV>>
example
const m = BiMap.of([1, 'a'], [2, 'b']).toBuilder()
m.removeValues(['c', 'd', 'e']) // => false
m.removeValues(['a', 'e']) // => true

set

Associates given key with given value in the builder.

Definition

set(key: K, value: V): boolean;

Parameters

NameTypeDescription
keyKthe entry key
valueV
example
const m = BiMap.of([1, 'a'], [2, 'b']).toBuilder()
m.set(1, 'a') // => false
m.set(1, 'b') // => true