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"