interface SortedMap<K,V>
A type-invariant immutable Map of key type K, and value type V. In the Map, each key has exactly one value, and the Map cannot contain duplicate keys. See the Map documentation and the SortedMap API documentation
Companion namespace: SortedMap
Type parameters
| Name | Description |
|---|---|
| K | the key type |
| V | the value type |
- The
SortedMapkeeps the inserted keys in sorted order according to the context'scompCompinstance.
const m1 = SortedMap.empty<number, string>()
const m2 = SortedMap.of([1, 'a'], [2, 'b'])
Methods
drop
Returns a SortedMap containing all but the the first amount of elements of this SortedMap.
dropamount of elements of this SortedMap.Definition
drop(amount: number): SortedMap<K, V>;
Parameters
| Name | Type | Description |
|---|---|---|
amount | number | the amount of elements to drop |
a negative amount drops the last elements instead of the first, e.g. -2 is the last 2 elements
const m = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]).asNormal();
console.log(m.drop(2).toArray())
// => [['c', ], ['d', 4]]
console.log(m.drop(-2).toArray())
// => [['a', 1], ['b', 2]]
findIndex
Returns the index of the given key in the SortedMap, or -1 if the key is not present.
findIndexgetAtIndex
Returns the entry with its key at the given index of the key sort order of the SortedMap, 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 m = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]).asNormal();
console.log(m.getAtIndex(1))
// => ['b', 2]
console.log(m.getAtIndex(-1))
// => ['d', 4]
console.log(m.getAtIndex(10))
// => undefined
console.log(m.getAtIndex(10, 'q'))
// => 'q'
getKeyAtIndex
Returns the key at the given index of the key sort order of the SortedMap, or a fallback value (default: undefined) if the index is out of bounds.
getKeyAtIndexDefinitions
getKeyAtIndex(index: number): K | undefined;
getKeyAtIndex<O>(index: number, otherwise: OptLazy<O>): K | 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 m = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]).asNormal();
console.log(m.getKeyAtIndex(1))
// => 'b'
console.log(m.getKeyAtIndex(-1))
// => 'd'
console.log(m.getKeyAtIndex(10))
// => undefined
console.log(m.getKeyAtIndex(10, 'q'))
// => 'q'
getValueAtIndex
Returns the value associated with the key at the given index of the key sort order of the SortedMap, or a fallback value (default: undefined) if the index is out of bounds.
getValueAtIndexDefinitions
getValueAtIndex(index: number): V | undefined;
getValueAtIndex<O>(index: number, otherwise: OptLazy<O>): 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 m = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]).asNormal();
console.log(m.getValueAtIndex(1))
// => 2
console.log(m.getValueAtIndex(-1))
// => 4
console.log(m.getValueAtIndex(10))
// => undefined
console.log(m.getValueAtIndex(10, 'q'))
// => 'q'
max
Returns the entry with the maximum key of the SortedMap, or a fallback value (default: undefined) if the SortedMap is empty.
maxDefinitions
max(): readonly [K, V] | undefined;
max<O>(otherwise: OptLazy<O>): readonly [K, V] | O;
const m = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]).asNormal();
console.log(m.max())
// => ['d', 4]
console.log(m.max('q'))
// => ['d', 4]
console.log(SortedMap.empty().max())
// => undefined
console.log(SortedMap.empty().max('q'))
// => 'q'
maxKey
Returns the maximum key of the SortedMap, or a fallback value (default: undefined) if the SortedMap is empty.
maxKeyDefinitions
maxKey(): K | undefined;
maxKey<O>(otherwise: OptLazy<O>): K | O;
const m = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]).asNormal();
console.log(m.maxKey())
// => 'a'
console.log(m.maxKey('q'))
// => 'a'
console.log(SortedMap.empty().maxKey())
// => undefined
console.log(SortedMap.empty().maxKey('q'))
// => 'q'
maxValue
Returns the value associated with the maximum key of the SortedMap, or a fallback value (default: undefined) if the SortedMap is empty.
maxValueDefinitions
maxValue(): V | undefined;
maxValue<O>(otherwise: OptLazy<O>): V | O;
const m = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]).asNormal();
console.log(m.maxValue())
// => 4
console.log(m.maxValue('q'))
// => 4
console.log(SortedMap.empty().maxValue())
// => undefined
console.log(SortedMap.empty().maxValue('q'))
// => 'q'
min
Returns the entry with the minimum key of the SortedMap, or a fallback value (default: undefined) if the SortedMap is empty.
minDefinitions
min(): readonly [K, V] | undefined;
min<O>(otherwise: OptLazy<O>): readonly [K, V] | O;
const m = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]).asNormal();
console.log(m.min())
// => ['a', 1]
console.log(m.min('q'))
// => ['a', 1]
console.log(SortedMap.empty().min())
// => undefined
console.log(SortedMap.empty().min('q'))
// => 'q'
minKey
Returns the minimum key of the SortedMap, or a fallback value (default: undefined) if the SortedMap is empty.
minKeyDefinitions
minKey(): K | undefined;
minKey<O>(otherwise: OptLazy<O>): K | O;
const m = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]).asNormal();
console.log(m.minKey())
// => 'a'
console.log(m.minKey('q'))
// => 'a'
console.log(SortedMap.empty().minKey())
// => undefined
console.log(SortedMap.empty().minKey('q'))
// => 'q'
minValue
Returns the value associated with the minimum key of the SortedMap, or a fallback value (default: undefined) if the SortedMap is empty.
minValueDefinitions
minValue(): V | undefined;
minValue<O>(otherwise: OptLazy<O>): V | O;
const m = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]).asNormal();
console.log(m.minValue())
// => 1
console.log(m.minValue('q'))
// => 1
console.log(SortedMap.empty().minValue())
// => undefined
console.log(SortedMap.empty().minValue('q'))
// => 'q'
slice
Returns a SortedMap containing only those entries whose keys are within the given keyRange.
slicekeyRange.sliceIndex
Returns a SortedMap containing only those entries that are within the given range index range of they key sort order.
sliceIndexrange index range of they key sort order.Definition
sliceIndex(range: IndexRange): SortedMap<K, V>;
Parameters
| Name | Type | Description |
|---|---|---|
range | IndexRange | an IndexRange defining the sort order indices to include. |
const m = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]).asNormal();
console.log(m.sliceIndex({ start: 1, amount: 2 }).toArray())
// => [['b', 2], ['c', 3]]
stream
undocumented
streamstreamKeys
undocumented
streamKeysstreamRange
Returns a Stream of sorted entries of this collection within the given keyRange.
streamRangekeyRange.Definition
streamRange(keyRange: Range<K>, options?: {
reversed?: boolean;
}): Stream<readonly [K, V]>;
Parameters
| Name | Type | Description |
|---|---|---|
keyRange | Range<K> | the range of keys to include in the stream |
options | {reversed?: boolean;} | (optional) an object containing the following properties: - reversed: (default: false) when true reverses the stream element order |
const m = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]);
console.log(m.streamRange({ start: 'b', end: 'c' }).toArray())
// => ['b', 'c']
streamSliceIndex
Returns a Stream of sorted entries of this collection within the given range index range.
streamSliceIndexrange index range.Definition
streamSliceIndex(range: IndexRange, options?: {
reversed?: boolean;
}): Stream<readonly [K, V]>;
Parameters
| Name | Type | Description |
|---|---|---|
range | IndexRange | the range of keys to include in the stream |
options | {reversed?: boolean;} |
const m = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]);
console.log(m.streamSliceIndex({ start: 1, amount: 2 }).toArray())
// => [['b', 2], ['c', 3]]
streamValues
undocumented
streamValuestake
Returns a SortedMap containing the the first amount of elements of this SortedMap.
takeamount of elements of this SortedMap.Definition
take(amount: number): SortedMap<K, V>;
Parameters
| Name | Type | Description |
|---|---|---|
amount | number | the amount of elements to keep |
a negative amount takes the last elements instead of the first, e.g. -2 is the last 2 elements
const m = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]).asNormal();
console.log(m.take(2).toArray())
// => [['a', 1], ['b', 2]]
console.log(m.take(-2).toArray())
// => [['c', ], ['d', 4]]