• Take the value decoded by the given decoder and transform it.

    Type Parameters

    • A

      Input to the mapping function

    • B

      The mapping function returns a value of this type

    Parameters

    • fn: ((a) => B)

      Your mapping function

        • (a): B
        • Parameters

          • a: A

          Returns B

    • child: Decoder<A>

      The decoder to run before calling your function

    Returns Decoder<B>

    Example

    // Add 42 to a number
    const add_42_decoder = Decode.map(value => value + 42, Decode.number);

    decode(add_42_decoder, 0) === 42
    decode(add_42_decoder, -42) === 0
    decode(add_42_decoder, 42) === 84
    decode(add_42_decoder, 'text') // Fails

    // Convert any value to its string representation.
    const to_string_decoder = Decode.map(value => String(value), Decode.unknown);

    decode(to_string_decoder, 42) === '42'
    decode(to_string_decoder, {}) === '[object Object]'
    decode(to_string_decoder, 'text') === 'text'
    decode(to_string_decoder, Symbol('my_symbol')) === 'Symbol(my_symbol)'

Generated using TypeDoc