class ObjectMapper

Convert from one type of object to another.

Instantiate an instance with a ObjectMapperSchema. 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 ObjectMapper#map.

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

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

Examples

Example 1

interface Input {
  inString: string;
  inNumber: number;
  inOptionalString: string | undefined;
}

interface Output {
  outString: string;
  outNumber: number;
}

const objectMapper = ObjectMapper.create<Input, Output>()({
  outString: "inString",
  outNumber: (input) => input.inNumber * 100,
});

const input: Input = {
  inString: 'foo',
  inNumber: 123,
  inOptionalString: undefined,
};

const output = objectMapper.map(input);
console.log(output);
// --> { outString: 'foo', outNumber: 12300 }

// Using a context
interface Context {
  multiplier: number,
}

const objectMapperWithContext = ObjectMapper.create<Input, Output, Context>()({
  outString: "inString",
  outNumber: (input, context) => input.inNumber * context.multiplier,
});

const output2 = objectMapperWithContext.map(input, { multiplier: 200 });
console.log(output2);
// --> { outString: 'foo', outNumber: 24600 }

// This will error; if `TContext` is not undefined, the `context` argument is required.
// objectMapperWithContext(input);

Constructors

new
ObjectMapper(schema: ObjectMapperSchema<TInput, TOutput, TContext>)

Type Parameters

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

Properties

protected
readonly
schemaMap: Map<keyof TOutput, MapperSchemaValue<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>,
): TOutput[]

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

array(
input: Iterable<TInput> | null,
context: OptionalArgIfUndefined<TContext>,
): 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>,
): 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>,
):
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>,
): 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>,
): 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>,
): 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>,
):
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(): ObjectMapperFunction<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 ObjectMapper 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.