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
Implemented by: SortedMap.NonEmpty<K,V>
, SortedMapEmpty<K,V>
Type parameters
Name | Description |
---|---|
K | the key type |
V | the value type |
- The
SortedMap
keeps the inserted keys in sorted order according to the context'scomp
Comp
instance.
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.
drop
amount
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]]
getAtIndex
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.
getAtIndex
Definitions
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.
getKeyAtIndex
Definitions
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.
getValueAtIndex
Definitions
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.
max
Definitions
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.
maxKey
Definitions
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.
maxValue
Definitions
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.
min
Definitions
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.
minKey
Definitions
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.
minValue
Definitions
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
.
slice
keyRange
.sliceIndex
Returns a SortedMap containing only those entries that are within the given range
index range of they key sort order.
sliceIndex
range
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
stream
streamKeys
undocumented
streamKeys
streamRange
Returns a Stream of sorted entries of this collection within the given keyRange
.
streamRange
keyRange
.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.
streamSliceIndex
range
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
streamValues
take
Returns a SortedMap containing the the first amount
of elements of this SortedMap.
take
amount
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]]