Skip to main content

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​

NameDescription
Kthe key type
Vthe value type
note
  • The SortedMap keeps the inserted keys in sorted order according to the context's comp Comp instance.
example
const m1 = SortedMap.empty<number, string>()
const m2 = SortedMap.of([1, 'a'], [2, 'b'])

Methods​

drop​

Returns a SortedMap containing all but the first amount of elements of this SortedMap.

Definition​

drop(amount: number): SortedMap<K, V>;

Parameters​

NameTypeDescription
amountnumberthe amount of elements to drop
note

a negative amount drops the last elements instead of the first, e.g. -2 is the last 2 elements

example
const m = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]).asNormal();
console.log(m.drop(2).toArray())
// => [['c', 3], ['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.

Definition​

findIndex(key: K): number;

Parameters​

NameTypeDescription
keyKthe key to find the index for
example
const m = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]);
console.log(m.findIndex('c'))
// => 2
console.log(m.findIndex('q'))
// => -1

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.

Definitions​

getAtIndex(index: number): readonly [K, V] | undefined;

getAtIndex<O>(index: number, otherwise: OptLazy<O>): readonly [K, V] | O;

Parameters​

NameTypeDescription
indexnumberthe index in the key sort order
note

negative index values will retrieve the values from the end of the sort order, e.g. -1 is the last value

example
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.

Definitions​

getKeyAtIndex(index: number): K | undefined;

getKeyAtIndex<O>(index: number, otherwise: OptLazy<O>): K | O;

Parameters​

NameTypeDescription
indexnumberthe index in the key sort order
note

negative index values will retrieve the values from the end of the sort order, e.g. -1 is the last value

example
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.

Definitions​

getValueAtIndex(index: number): V | undefined;

getValueAtIndex<O>(index: number, otherwise: OptLazy<O>): V | O;

Parameters​

NameTypeDescription
indexnumberthe index in the key sort order
note

negative index values will retrieve the values from the end of the sort order, e.g. -1 is the last value

example
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.

Definitions​

max(): readonly [K, V] | undefined;

max<O>(otherwise: OptLazy<O>): readonly [K, V] | O;

example
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.

Definitions​

maxKey(): K | undefined;

maxKey<O>(otherwise: OptLazy<O>): K | O;

example
const m = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]).asNormal();
console.log(m.maxKey())
// => 'd'
console.log(m.maxKey('q'))
// => 'd'
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.

Definitions​

maxValue(): V | undefined;

maxValue<O>(otherwise: OptLazy<O>): V | O;

example
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.

Definitions​

min(): readonly [K, V] | undefined;

min<O>(otherwise: OptLazy<O>): readonly [K, V] | O;

example
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.

Definitions​

minKey(): K | undefined;

minKey<O>(otherwise: OptLazy<O>): K | O;

example
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.

Definitions​

minValue(): V | undefined;

minValue<O>(otherwise: OptLazy<O>): V | O;

example
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.

Definition​

slice(keyRange: Range<K>): SortedMap<K, V>;

Parameters​

NameTypeDescription
keyRangeRange<K>a Range defining the keys to include
example
const m = SortedMap.of(['b', 2], ['d', 4], ['a', 1], ['c', 3]).asNormal();
console.log(m.slice({ start: 'b', end: 'c' }).toArray())
// => [['b', 2], ['c', 3]]

sliceIndex​

Returns a SortedMap containing only those entries that are within the given range index range of they key sort order.

Definition​

sliceIndex(range: IndexRange): SortedMap<K, V>;

Parameters​

NameTypeDescription
rangeIndexRangean IndexRange defining the sort order indices to include.
example
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

Definition​

stream(options?: {
    reversed?: boolean;
  }): Stream<readonly [K, V]>;

Parameters​

NameTypeDescription
options{
    reversed?: boolean;
  }

streamKeys​

undocumented

Definition​

streamKeys(options?: {
    reversed?: boolean;
  }): Stream<K>;

Parameters​

NameTypeDescription
options{
    reversed?: boolean;
  }

streamRange​

Returns a Stream of sorted entries of this collection within the given keyRange.

Definition​

streamRange(keyRange: Range<K>, options?: {
    reversed?: boolean;
  }): Stream<readonly [K, V]>;

Parameters​

NameTypeDescription
keyRangeRange<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
example
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.

Definition​

streamSliceIndex(range: IndexRange, options?: {
    reversed?: boolean;
  }): Stream<readonly [K, V]>;

Parameters​

NameTypeDescription
rangeIndexRangethe range of keys to include in the stream
options{
    reversed?: boolean;
  }
example
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

Definition​

streamValues(options?: {
    reversed?: boolean;
  }): Stream<V>;

Parameters​

NameTypeDescription
options{
    reversed?: boolean;
  }

take​

Returns a SortedMap containing the the first amount of elements of this SortedMap.

Definition​

take(amount: number): SortedMap<K, V>;

Parameters​

NameTypeDescription
amountnumberthe amount of elements to keep
note

a negative amount takes the last elements instead of the first, e.g. -2 is the last 2 elements

example
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', 3], ['d', 4]]