Aller au contenu principal

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>

Type

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