decoders

Sets & Maps

Decoders for maps and sets.

mapping

function mapping( decoder: Decoder<T> ): Decoder<Map<string, T>> (source)

Similar to record(), but returns the result as a Map<string, T> (an ES6 Map) instead.

Try it
mapping(number).verify(input)
InputResult
Map(3) { "red" => 1, "blue" => 2, "green" => 3 }
{ "hi": "not a number", ^^^^^^^^^^^^^^ Must be number }

setFromArray

function setFromArray( decoder: Decoder<T> ): Decoder<Set<T>> (source)

Similar to array(), but returns the result as an ES6 Set.

Try it
setFromArray(string).verify(input)
InputResult
Set(2) { "abc", "pqr" }
Set(0) { }
[ 1, ^ Must be string (at index 0) 2, ]

Limiting set sizes

function sized( decoder: Decoder<Set<T>>, options: SizeOptions ): Decoder<Set<T>> (source)

Available since 2.9.

Takes any set decoder and rejects values that don't match the given size constraints. Also works with strings and arrays.

SizeOptions accepts min, max, or size (shorthand for exact length).

Try it
sized(setFromArray(string), { min: 1, max: 3 }).verify(input)
InputResult
Set(2) { "a", "b" }
Set(1) { "a" }
<Set> ^^^^^ Must have at least 1 item
<Set> ^^^^^ Must have at most 3 items

On this page