UNPKG

3.71 kBTypeScriptView Raw
1/**
2 * A set of instructions for multiple keys.
3 * The aim is to provide a concise yet readable way to map and filter values
4 * onto a target object.
5 *
6 * @example
7 * ```javascript
8 * const example: ObjectMappingInstructions = {
9 * lazyValue1: [, () => 1],
10 * lazyValue2: [, () => 2],
11 * lazyValue3: [, () => 3],
12 * lazyConditionalValue1: [() => true, () => 4],
13 * lazyConditionalValue2: [() => true, () => 5],
14 * lazyConditionalValue3: [true, () => 6],
15 * lazyConditionalValue4: [false, () => 44],
16 * lazyConditionalValue5: [() => false, () => 55],
17 * lazyConditionalValue6: ["", () => 66],
18 * simpleValue1: [, 7],
19 * simpleValue2: [, 8],
20 * simpleValue3: [, 9],
21 * conditionalValue1: [() => true, 10],
22 * conditionalValue2: [() => true, 11],
23 * conditionalValue3: [{}, 12],
24 * conditionalValue4: [false, 110],
25 * conditionalValue5: [() => false, 121],
26 * conditionalValue6: ["", 132],
27 * };
28 *
29 * const exampleResult: Record<string, any> = {
30 * lazyValue1: 1,
31 * lazyValue2: 2,
32 * lazyValue3: 3,
33 * lazyConditionalValue1: 4,
34 * lazyConditionalValue2: 5,
35 * lazyConditionalValue3: 6,
36 * simpleValue1: 7,
37 * simpleValue2: 8,
38 * simpleValue3: 9,
39 * conditionalValue1: 10,
40 * conditionalValue2: 11,
41 * conditionalValue3: 12,
42 * };
43 * ```
44 */
45export declare type ObjectMappingInstructions = Record<string, ObjectMappingInstruction>;
46/**
47 * An instruction set for assigning a value to a target object.
48 */
49export declare type ObjectMappingInstruction = LazyValueInstruction | ConditionalLazyValueInstruction | SimpleValueInstruction | ConditionalValueInstruction | UnfilteredValue;
50/**
51 * non-array
52 */
53export declare type UnfilteredValue = any;
54export declare type LazyValueInstruction = [FilterStatus, ValueSupplier];
55export declare type ConditionalLazyValueInstruction = [FilterStatusSupplier, ValueSupplier];
56export declare type SimpleValueInstruction = [FilterStatus, Value];
57export declare type ConditionalValueInstruction = [ValueFilteringFunction, Value];
58/**
59 * Filter is considered passed if
60 * 1. It is a boolean true.
61 * 2. It is not undefined and is itself truthy.
62 * 3. It is undefined and the corresponding _value_ is neither null nor undefined.
63 */
64export declare type FilterStatus = boolean | unknown | void;
65/**
66 * Supplies the filter check but not against any value as input.
67 */
68export declare type FilterStatusSupplier = () => boolean;
69/**
70 * Filter check with the given value.
71 */
72export declare type ValueFilteringFunction = (value: any) => boolean;
73/**
74 * Supplies the value for lazy evaluation.
75 */
76export declare type ValueSupplier = () => any;
77/**
78 * A non-function value.
79 */
80export declare type Value = any;
81/**
82 * Internal/Private, for codegen use only.
83 *
84 * Transfer a set of keys from [instructions] to [target].
85 *
86 * For each instruction in the record, the target key will be the instruction key.
87 * The target assignment will be conditional on the instruction's filter.
88 * The target assigned value will be supplied by the instructions as an evaluable function or non-function value.
89 *
90 * @see ObjectMappingInstructions for an example.
91 * @private
92 * @internal
93 */
94export declare function map(target: any, filter: (value: any) => boolean, instructions: Record<string, ValueSupplier | Value>): typeof target;
95export declare function map(instructions: Record<string, ObjectMappingInstruction>): any;
96export declare function map(target: any, instructions: Record<string, ObjectMappingInstruction>): typeof target;
97/**
98 * Convert a regular object { k: v } to { k: [, v] } mapping instruction set with default
99 * filter.
100 *
101 * @private
102 * @internal
103 */
104export declare const convertMap: (target: any) => Record<string, any>;