@fgv/ts-utils
Advanced tools
Comparing version 1.4.1 to 1.5.0
@@ -620,2 +620,60 @@ import { BaseConverter, Converter, ConverterTraits } from './converter'; | ||
/** | ||
* Per-property converters and configuration for each field in the destination object of | ||
* a {@link Converters.transformObject} call. | ||
* @public | ||
*/ | ||
export type FieldTransformers<TSRC, TDEST, TC = unknown> = { | ||
[key in keyof TDEST]: { | ||
/** | ||
* The name of the property in the source object to be converter. | ||
*/ | ||
from: keyof TSRC; | ||
/** | ||
* The converter used to convert the property. | ||
*/ | ||
converter: Converter<TDEST[key], TC>; | ||
/** | ||
* If `true` then a missing source property is ignored. If `false` or omitted | ||
* then a missing source property causes an error. | ||
*/ | ||
optional?: boolean; | ||
}; | ||
}; | ||
/** | ||
* Options for a {@link Converters.transformObject} call. | ||
*/ | ||
export interface TransformObjectOptions<TSRC> { | ||
/** | ||
* If `strict` is `true` then unused properties in the source object cause | ||
* an error, otherwise they are ignored. | ||
*/ | ||
strict: true; | ||
/** | ||
* An optional list of source properties to be ignored when strict mode | ||
* is enabled. | ||
*/ | ||
ignore?: (keyof TSRC)[]; | ||
} | ||
/** | ||
* Helper to create a strongly-typed {@link Converter} which converts a source object to a | ||
* new object with a different shape. | ||
* | ||
* @remarks | ||
* On successful conversion, the resulting {@link Converter} returns {@link Success} with a new | ||
* object, which contains the converted values under the key names specified at initialization time. | ||
* | ||
* It returns {@link Failure} with an error message if any fields to be extracted do not exist | ||
* or cannot be converted. | ||
* | ||
* @param destinationFields - An object with key names that correspond to the target object and an | ||
* appropriate {@link Converters.FieldTransformers | FieldTransformers} which specifies the name | ||
* of the corresponding property in the source object, the converter used to convert the source | ||
* property and any configuration to guide the conversion. | ||
* @param options - Options which affect the transformation. | ||
* | ||
* @returns A {@link Converter} with the specified conversion behavior. | ||
* @public | ||
*/ | ||
export declare function transformObject<TSRC, TDEST, TC = unknown>(destinationFields: FieldTransformers<TSRC, TDEST, TC>, options?: TransformObjectOptions<TSRC>): Converter<TDEST, TC>; | ||
/** | ||
* A helper wrapper to construct a {@link Converter} which converts to an arbitrary strongly-typed | ||
@@ -622,0 +680,0 @@ * range of some comparable type. |
{ | ||
"name": "@fgv/ts-utils", | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"description": "Assorted Typescript Utilities", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
656537
5989