Skip to main content

functions/map

Helper functions that have to do with maps.

Functions

mapFilter()

function mapFilter<K, V>(map, predicate): V[]

Helper function to get the values in a Map that match an arbitrary condition. Similar to the Array.map method, but works for maps.

This is efficient such that it avoids converting the map values into an array.

If you want to perform a filter and a map at the same time on an array, use the filterMap helper function instead.

Type Parameters

Type Parameter
K
V

Parameters

ParameterType
mapReadonlyMap<K, V>
predicate(value) => boolean

Returns

V[]

Defined in

functions/map.ts:18


mapFind()

function mapFind<K, V>(map, predicate): V | undefined

Helper function to find a value in a Map. Similar to the Array.find method, but works for maps.

This is efficient such that it avoids converting the map values into an array.

Type Parameters

Type Parameter
K
V

Parameters

ParameterType
mapReadonlyMap<K, V>
predicate(value) => boolean

Returns

V | undefined

Defined in

functions/map.ts:40


objectToMap()

function objectToMap<K, V>(object): Map<K, V>

Helper function to convert an object to a map.

This is useful when you need to construct a type safe object with the satisfies operator, but then later on you need to query it in a way where you expect the return value to be T or undefined. In this situation, by converting the object to a map, you can avoid unsafe type assertions.

Note that the map values will be inserted in a random order, due to how pairs works under the hood.

Also see the objectToReadonlyMap function.

Type Parameters

Type Parameter
K extends string | number | symbol
V

Parameters

ParameterType
objectRecord<K, V>

Returns

Map<K, V>

Defined in

functions/map.ts:68


objectToReadonlyMap()

function objectToReadonlyMap<K, V>(object): ReadonlyMap<K, V>

Helper function to convert an object to a read-only map.

This is useful when you need to construct a type safe object with the satisfies operator, but then later on you need to query it in a way where you expect the return value to be T or undefined. In this situation, by converting the object to a map, you can avoid unsafe type assertions.

Note that the map values will be inserted in a random order, due to how pairs works under the hood.

Also see the objectToMap function.

Type Parameters

Type Parameter
K extends string | number | symbol
V

Parameters

ParameterType
objectRecord<K, V>

Returns

ReadonlyMap<K, V>

Defined in

functions/map.ts:93