Aller au contenu principal

createAdapter()

createAdapter<FromIn, FromOut, ToIn, ToOut>(mapInput, mapOutput): Adapter<FromIn, FromOut, ToIn, ToOut>

Creates a reusable adapter from input/output mappers. The adapter can then be applied to any source function with the matching signature.


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

mapInput: (input) => FromIn

Transforms target input into source input

mapOutput: (output) => ToOut

Transforms source output into target output


Returns: Adapter<FromIn, FromOut, ToIn, ToOut>

A reusable Adapter


Since

2.4.0


Example

// Adapter from Celsius to Fahrenheit API
const celsiusToFahrenheit = createAdapter<number, string, number, string>(
(fahrenheit) => (fahrenheit - 32) * 5 / 9, // °F -> °C input
(label) => label.replace("°C", "°F"), // output label
);

const describeCelsius = (temp: number) => `${temp.toFixed(1)}°C`;
const describeFahrenheit = celsiusToFahrenheit(describeCelsius);

describeFahrenheit(212); // "100.0°F"