import type { ExactReturn, MapperSchemaValue, ObjectMapperFunction, ObjectMapperSchema, OptionalArgIfUndefined } from "./types.js"; /** * Convert from one type of object to another. * * Instantiate an instance with a {@linkcode 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 {@linkcode ObjectMapper#map}. * * If you want a plain (unbound) function, you can call {@linkcode ObjectMapper#toFunction}. * * There is a convenience method {@linkcode ObjectMapper#array}, for mapping some iterable to an array. * * @example ```ts * interface Input { * inString: string; * inNumber: number; * inOptionalString: string | undefined; * } * * interface Output { * outString: string; * outNumber: number; * } * * const objectMapper = ObjectMapper.create()({ * 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()({ * 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); * ``` * * @group runtime */ export declare class ObjectMapper { /** * An object, where each property is named after a property on the output type, * and each value is a mapper function, or a string of a property name from * input type. */ readonly schema: ObjectMapperSchema; /** * 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. * * @example ```ts * const mapper1 = ObjectMapper.create<{ in1: string }, { out1: string; out2: string }>()({ * out1: "in1", * out2: "in1", * }); * * // const mapper2 = ObjectMapper.create<{ in1: string }, { out1: string; out3: string }>()({ * // ...mapper1.schema, // error, "out2" is present but shouldn't be * // out3: "in1", * // }); * ``` */ static create(): & { [P in Exclude]: never; }>(schema: TActualSchema) => ObjectMapper; /** * For faster runtime performance, the object mapper schema is converted to * a Map instance. * @private */ protected readonly schemaMap: Map>; protected constructor( /** * An object, where each property is named after a property on the output type, * and each value is a mapper function, or a string of a property name from * input type. */ schema: ObjectMapperSchema); /** * Map multiple input objects from some iterable, and return an * array of output objects. */ array(input: Iterable, context: OptionalArgIfUndefined): ExactReturn; /** * 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 | null, context: OptionalArgIfUndefined): ExactReturn | null; /** * 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 | undefined, context: OptionalArgIfUndefined): ExactReturn | 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. */ array(input: Iterable | null | undefined, context: OptionalArgIfUndefined): ExactReturn | 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. */ map(input: TInput, context: OptionalArgIfUndefined): ExactReturn; /** * 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 {@linkcode input} is `null`, it will be returned as-is. */ map(input: TInput | null, context: OptionalArgIfUndefined): ExactReturn | 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 {@linkcode input} is `undefined`, it will be returned as-is. */ map(input: TInput | undefined, context: OptionalArgIfUndefined): ExactReturn | 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 {@linkcode input} is `null` or `undefined`, it will be returned as-is. */ map(input: TInput | null | undefined, context: OptionalArgIfUndefined): ExactReturn | null | undefined; /** * Wrap this instance in a function, with a `schema` property. * * @example ```ts * interface Input { * in: string; * } * * interface Output { * out: string; * } * * const mapObject = (ObjectMapper.create()({ * out: "in", * })).toFunction(); * * const output = mapObject({ * in: 'Hello world!' * }); * ``` */ toFunction(): ObjectMapperFunction; } //# sourceMappingURL=object-mapper.d.ts.map