class AsyncObjectMapper

Convert from one type of object to another.

Instantiate an instance with a AsyncObjectMapperSchema. You must pass two type parameters:

  • TInput: the type of the input object
  • TOutput: the type of the desired output object

You can pass an optional third type parameter:

  • TContext: an object with any additional data or functions. Useful when mapping multiple objects with some shared state, like a "now" timestamp.

Invoke the mapper with AsyncObjectMapper#map.

If you want a plain (unbound) function, you can call AsyncObjectMapper#toFunction.

There is a convenience method AsyncObjectMapper#array, for mapping some iterable to an array.

Constructors

new
AsyncObjectMapper(schema: AsyncObjectMapperSchema<TInput, TOutput, TContext>)

Type Parameters

TInput extends object
TOutput extends object
TContext extends object | undefined = undefined

Properties

protected
readonly
schemaMap: Map<keyof TOutput, AsyncMapperSchemaValue<TInput, TOutput, TContext>>

For faster runtime performance, the object mapper schema is converted to a Map instance.

Methods

array(
input: Iterable<TInput>,
context: OptionalArgIfUndefined<TContext>,
): Promise<TOutput[]>

Map multiple input objects from some iterable, and return an array of output objects.

array(
input: Iterable<TInput> | null,
context: OptionalArgIfUndefined<TContext>,
): Promise<TOutput[] | null>

Map multiple input objects from some iterable, and return an array of output objects.

If the input is null, it will be returned as-is.

array(
input: Iterable<TInput> | undefined,
context: OptionalArgIfUndefined<TContext>,
): Promise<TOutput[] | undefined>

Map multiple input objects from some iterable, and return an array of output objects.

If the input is undefined, it will be returned as-is.

array(
input:
Iterable<TInput>
| null
| undefined
,
context: OptionalArgIfUndefined<TContext>,
): Promise<
TOutput[]
| null
| undefined
>

Map multiple input objects from some iterable, and return an array of output objects.

If the input is null or undefined, it will be returned as-is.

map(
input: TInput,
context: OptionalArgIfUndefined<TContext>,
): Promise<TOutput>

Maps an input object to an output object.

It does so by iterating each property in the object schema, and invoking the property's mapping function, passing the input and context.

map(
input: TInput | null,
context: OptionalArgIfUndefined<TContext>,
): Promise<TOutput | null>

Maps an input object to an output object.

It does so by iterating each property in the object schema, and invoking the property's mapping function, passing the input and context.

If input is null, it will be returned as-is.

map(
input: TInput | undefined,
context: OptionalArgIfUndefined<TContext>,
): Promise<TOutput | undefined>

Maps an input object to an output object.

It does so by iterating each property in the object schema, and invoking the property's mapping function, passing the input and context.

If input is undefined, it will be returned as-is.

map(
input:
TInput
| null
| undefined
,
context: OptionalArgIfUndefined<TContext>,
): Promise<
TOutput
| null
| undefined
>

Maps an input object to an output object.

It does so by iterating each property in the object schema, and invoking the property's mapping function, passing the input and context.

If input is null or undefined, it will be returned as-is.

toFunction(): AsyncObjectMapperFunction<TInput, TOutput, TContext>

Wrap this instance in a function, with a schema property.

Static Methods

create<
TInput extends object,
TOutput extends object,
TContext extends object | undefined = undefined,
>
()

Create an AsyncObjectMapper factory function. Invoke it immediately, with an object mapper schema, to create an AsyncObjectMapper instance.

We use this approach to trick TypeScript into requiring an exact type to be passed in. That is, the object mapper schema must only have properties that exist in the output type, and no additional properties.