adapt()
adapt<
FromIn,FromOut,ToIn,ToOut>(source,mapInput,mapOutput): (input) =>ToOut
Adapts a source function in one shot by mapping both input and output.
Type Parametersβ
FromIn: FromInβ
The source function's input type
FromOut: FromOutβ
The source function's output type
ToIn: ToInβ
The target input type
ToOut: ToOutβ
The target output type
Parametersβ
source: (input) => FromOutβ
The function to adapt
mapInput: (input) => FromInβ
Transforms target input into source input
mapOutput: (output) => ToOutβ
Transforms source output into target output
Returnsβ
The adapted function
Sinceβ
2.4.0
Exampleβ
// Adapt a sorting function to work with strings instead of numbers
const sortNumbers = (nums: number[]) => [...nums].sort((a, b) => a - b);
const sortStrings = adapt(
sortNumbers,
(strs: string[]) => strs.map(Number),
(nums) => nums.map(String),
);
sortStrings(["3", "1", "2"]); // ["1", "2", "3"]
Adapter()<FromIn, FromOut, ToIn, ToOut>β
Adapter<
FromIn,FromOut,ToIn,ToOut> = (source) => (input) =>ToOut
An Adapter transforms a function with one signature into a function with a different signature. Replaces the GoF Adapter class hierarchy.
Type Parametersβ
FromIn: FromInβ
The source function's input type
FromOut: FromOutβ
The source function's output type
ToIn: ToInβ
The target input type
ToOut: ToOutβ
The target output type
Parametersβ
source: (input) => FromOutβ
Returns: (input): ToOutβ
Sinceβ
2.4.0