lookup()
lookup<
A
,B
>(obj
,def?
): <C
>(prop
) =>A
[C
]
Takes an object and an optional fallback function and returns a function that takes a string and returns the lookup value or the result default fallback.
Type Parameters
A
A
extends Record
<string
| number
| symbol
, unknown
>
The type of the record to use as a lookup.
B
B
extends (...args
) => any
The type of the default function.
Parameters
obj
A
The table lookup object.
def?
B
The function used to handle the default value if the lookup returns undefined
.
Returns
<
C
>(prop
):A
[C
]
Type Parameters
C
C
extends string
| number
| symbol
Parameters
prop
string
| number
| symbol
Returns
A
[C
]
Remarks
pure function
Example
const colorTable = {
FOO: [0, 0, 255, 155],
BAR: [255, 0, 255, 155],
FIZZ: [230, 0, 0, 155],
BUZZ: [0, 128, 0, 155],
};
const colorLookup = tableLookup(colorTable, x => x ?? [128, 128, 128, 155]);
colorLookup(data.value);