interface SortedMap.Builder<K,V>
A mutable SortedMap builder used to efficiently create new immutable instances. See the Map documentation and the SortedMap.Builder API documentation
Extends: RMapBase.Builder<K,V,Tp>
Type parameters
| Name | Description |
|---|---|
| K | the key type |
| V | the value type |
Properties
context
Returns the context associated to this collection instance.
contextcontext associated to this collection instance.isEmpty
Returns true if there are no entries in the builder.
isEmptysize
Returns the amount of entries in the builder.
sizeMethods
addEntries
Adds given entries to the builder.
addEntriesentries to the builder.Definition
addEntries(entries: StreamSource<readonly [K, V]>): boolean;
Parameters
| Name | Type | Description |
|---|---|---|
entries | StreamSource<readonly [K, V]> |
const m = HashMap.of([1, 'a'], [2, 'b']).toBuilder()
m.addEntries([1, 'a'], [3, 'c']]) // => true
m.addEntries([]) // => false
Overrides
addEntry
Adds given entry to the builder.
addEntryentry to the builder.build
Returns an immutable collection instance containing the entries in this builder.
buildDefinition
build(): WithKeyValue<Tp, K, V>['normal'];
const m = HashMap.of([1, 'a'], [2, 'b']).toBuilder()
const m2: HashMap<number, string> = m.build()
Overrides
buildMapValues
Returns an immutable instance of the entries in this builder, with given mapValues function applied to all the values in the entries.
buildMapValuesmapValues function applied to all the values in the entries.Definition
buildMapValues<V2>(mapFun: (value: V, key: K) => V2): (Tp & KeyValue<K, V2>)['normal'];
Type parameters
| Name | Description |
|---|---|
| V2 |
Parameters
| Name | Type | Description |
|---|---|---|
mapFun | (value: V, key: K) => V2 | a function that takes an entry value and its key, and returns a new value |
const m = HashMap.of([1, 'a'], [2, 'b']).toBuilder()
const m2: HashMap<number, number> = m.buildMapValues(value => value.length)
Overrides
forEach
Performs given function f for each entry of the builder.
forEachf for each entry of the builder.Definition
forEach(f: (entry: readonly [K, V], index: number, halt: () => void) => void, options?: {
state?: TraverseState;
}): void;
Parameters
| Name | Type | Description |
|---|---|---|
f | (entry: readonly [K, V], index: number, halt: () => void) => void | the 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;} |
RibuError.ModifiedBuilderWhileLoopingOverItError if the builder is modified while looping over it
HashMap.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]
O(N)
Overrides
get
Returns the value associated with the given key, or given otherwise value if the key is not in the collection.
getkey, 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
| Name | Default | Description |
|---|---|---|
| UK | K |
Parameters
| Name | Type | Description |
|---|---|---|
key | RelatedTo<K, UK> | the key to look for |
const m = HashMap.of([1, 'a'], [2, 'b']).toBuilder()
m.get(2) // => 'b'
m.get(3) // => undefined
m.get(2, 'none') // => 'b'
m.get(3, 'none') // => 'none'
Overrides
getAtIndex
Returns the entry with its key at the given index of the key sort order of the SortedMap builder, or a fallback value (default: undefined) if the index is out of bounds.
getAtIndexDefinitions
getAtIndex(index: number): readonly [K, V] | undefined;
getAtIndex<O>(index: number, otherwise: OptLazy<O>): readonly [K, V] | O;
Parameters
| Name | Type | Description |
|---|---|---|
index | number | the index in the key sort order |
negative index values will retrieve the values from the end of the sort order, e.g. -1 is the last value
const b = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]).toBuilder();
console.log(b.getAtIndex(1))
// => ['b', 2]
console.log(b.getAtIndex(-1))
// => ['d', 4]
console.log(b.getAtIndex(10))
// => undefined
console.log(b.getAtIndex(10, 'q'))
// => 'q'
hasKey
Returns true if the given key is present in the builder.
hasKeykey is present in the builder.max
Returns the entry with the maximum key of the SortedMap Builder, or a fallback value (default: undefined) if the builder is empty.
maxDefinitions
max(): readonly [K, V] | undefined;
max<O>(otherwise: OptLazy<O>): readonly [K, V] | O;
const b = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]).toBuilder();
console.log(b.max())
// => ['a', 1]
console.log(b.max('q'))
// => ['a', 1]
console.log(SortedMap.builder().max())
// => undefined
console.log(SortedMap.builder().max('q'))
// => 'q'
min
Returns the entry with the minimum key of the SortedMap Builder, or a fallback value (default: undefined) if the builder is empty.
minDefinitions
min(): readonly [K, V] | undefined;
min<O>(otherwise: OptLazy<O>): readonly [K, V] | O;
const b = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]).toBuilder();
console.log(b.min())
// => ['a', 1]
console.log(b.min('q'))
// => ['a', 1]
console.log(SortedMap.builder().min())
// => undefined
console.log(SortedMap.builder().min('q'))
// => 'q'
modifyAt
Modifies or creates the builder entry with given atKey as its key according to given options.
modifyAtatKey as its key according to given options.Definition
modifyAt(key: K, options: {
ifNew?: OptLazyOr<V, Token>;
ifExists?: (<V2 extends V = V>(currentValue: V & V2, remove: Token) => V |Token)| V;
}): boolean;
Parameters
| Name | Type | Description |
|---|---|---|
key | K | |
options | {ifNew?: OptLazyOr<V, Token>;ifExists?: (<V2 extends V = V>(currentValue: V & V2, remove: Token) => V |Token)| V;} |
const m = HashMap.of([1, 'a'], [2, 'b']).toBuilder()
m.modifyAt(1, { ifNew: 'd' })
// => false
m.modifyAt(3, { ifNew: 'c' })
// => true
m.modifyAt(3, { ifNew: (none) => 1 < 2 ? none : 'c' })
// => false
m.modifyAt(2, { ifExists: () => 'c' })
// => true
m.modifyAt(1, { ifExists: (v) => v + 'z' })
// => true
m.modifyAt(2, { ifExists: (v, remove) => v === 'a' ? v : remove })
// => true
Overrides
removeKey
Removes the entry with given key from the builder.
removeKeykey 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
| Name | Default | Description |
|---|---|---|
| UK | K |
Parameters
| Name | Type | Description |
|---|---|---|
key | RelatedTo<K, UK> | the key of the entry to remove |
const m = HashMap.of([1, 'a'], [2, 'b']).toBuilder()
m.removeKey(2) // => 'b'
m.removeKey(3) // => undefined
m.removeKey(3, 'c') // => 'c'
Overrides
removeKeys
Removes the entries in the given keys StreamSource from the builder.
removeKeyskeys StreamSource from the builder.Definition
removeKeys<UK = K>(keys: StreamSource<RelatedTo<K, UK>>): boolean;
Type parameters
| Name | Default | Description |
|---|---|---|
| UK | K |
Parameters
| Name | Type | Description |
|---|---|---|
keys | StreamSource<RelatedTo<K, UK>> |
const m = HashMap.of([1, 'a'], [2, 'b']).toBuilder()
m.removeKeys([3, 4, 5]) // => false
m.removeKeys([1, 10]) // => true
Overrides
set
Associates given key with given value in the builder.
setkey with given value in the builder.updateAt
Updates the value in the builder associated with given key according to given update value or function.
updateAtkey according to given update value or function.Definitions
updateAt(key: K, update: RMapBase.Update<V>): V | undefined;
updateAt<O>(key: K, update: RMapBase.Update<V>, otherwise: OptLazy<O>): V | O;
Parameters
| Name | Type | Description |
|---|---|---|
key | K | the key of the entry to update |
update | RMapBase.Update<V> | a new value or function taking the previous value and returning a new value |
const m = HashMap.of([1, 'a'], [2, 'b']).toBuilder()
m.updateAt(1, 'a') // => 'a'
m.updateAt(1, 'b') // => 'b'
m.updateAt(2, v => v + 'z') // => 'b'