mappingutils
Version:
Lightweight JSON transformation utility.
88 lines (87 loc) • 5.61 kB
TypeScript
type mapping = {
[key: string]: any;
};
/**
* Add a `key` property to the object `obj` with the value `value`.
* @param obj - The input object.
* @param key - The path in the resulting object to set the value.
* @param value - The value to set.
* @returns A deep copy of the object with the new property added.
*/
export declare function addProp(obj: object, key: string, value: any): object;
/**
* Merges the values of the `prop` property from each object within the `objArr` array.
* @param objArr - An array of objects.
* @param prop - The path to the array property to merge.
* @returns A deep copy of the first object in `objArr`, with its `prop` array containing the concatenated values from all objects in the `objArr`.
*/
export declare function mergeObjArr(objArr: object[], prop: string): object;
/**
* Transforms the `source` object based on the provided `mapping` transformation.
*
* @param source - A source object to transform.
* @param mapping - A mapping object defining the transformation rules. Each mapping object's key-value pair should use JSONPath syntax:
* - The key represents the target field path in the transformed object.
* - The value represents the source field path(s) in the source object.
* - The value can be a literal.
* - If a single source field is required, the value should be a JSONPath string pointing to that field.
* - If a default value is needed, the value can be any valid JSON value.
* - If multiple source fields are required for applying a function, provide an array where:
* - Each element before the last is a JSONPath string pointing to a source field.
* - The last element is a function that takes the resolved source values as arguments and computes the target field value.
*
* @returns An array of transformed objects, with fields derived from applying the `mapping` to the `source` object.
*/
export declare function mapObj(source: object, mapping: mapping): object[];
/**
* Transforms the `source` object based on the provided `mapping` transformation.
*
* @param source - A source object to transform.
* @param mapping - A mapping object defining the transformation rules. Each mapping object's key-value pair should use JSONPath syntax:
* - The key represents the target field path in the transformed object.
* - The value represents the source field path(s) in the source object.
* - The value can be a literal.
* - If a single source field is required, the value should be a JSONPath string pointing to that field.
* - If a default value is needed, the value can be any valid JSON value.
* - If multiple source fields are required for applying a sync or async function, provide an array where:
* - Each element before the last is a JSONPath string pointing to a source field.
* - The last element is a function that takes the resolved source values as arguments and computes the target field value.
*
* @returns An array of transformed objects, with fields derived from applying the `mapping` to the `source` object.
*/
export declare function mapObjAsync(source: object, mapping: mapping): Promise<object[]>;
/**
* Transforms each object in the `source` array based on the provided `mapping` transformation.
*
* @param source - An array of source objects to transform.
* @param mapping - A mapping object defining the transformation rules. Each mapping object's key-value pair should use JSONPath syntax:
* - The key represents the target field path in the transformed object.
* - The value represents the source field path(s) in the source object.
* - The value can be a literal.
* - If a single source field is required, the value should be a JSONPath string pointing to that field.
* - If a default value is needed, the value can be any valid JSON value.
* - If multiple source fields are required for applying a function, provide an array where:
* - Each element before the last is a JSONPath string pointing to a source field.
* - The last element is a function that takes the resolved source values as arguments and computes the target field value.
*
* @returns An array of transformed objects, with fields derived from applying the `mapping` to each `source` object.
*/
export declare function mapObjArr(source: object[], mapping: mapping): object[];
/**
* Transforms each object in the `source` array based on the provided `mapping` transformation.
*
* @param source - An array of source objects to transform.
* @param mapping - A mapping object defining the transformation rules. Each mapping object's key-value pair should use JSONPath syntax:
* - The key represents the target field path in the transformed object.
* - The value represents the source field path(s) in the source object.
* - The value can be a literal.
* - If a single source field is required, the value should be a JSONPath string pointing to that field.
* - If a default value is needed, the value can be any valid JSON value.
* - If multiple source fields are required for applying a sync or async function, provide an array where:
* - Each element before the last is a JSONPath string pointing to a source field.
* - The last element is a function that takes the resolved source values as arguments and computes the target field value.
*
* @returns An array of transformed objects, with fields derived from applying the `mapping` to each `source` object.
*/
export declare function mapObjArrAsync(source: object[], mapping: mapping): Promise<object[]>;
export {};