Skip to content

partial

Partially applies arguments to a function, returning a new function that accepts the remaining arguments.

Available since v2.0.0

import { partial } from '@helpers4/function';
// or, from the all-in-one package (same code, one install):
import { partial } from 'helpers4/function';
partial<A, R>(fn: function, a: A): function
ParameterTypeDescription
fnfunctionThe function to partially apply
aA

function — A function waiting for the remaining arguments

Pre-fill the first argument to derive a specialised function.

const multiply = (a: number, b: number) => a * b;
const double = partial(multiply, 2);
const triple = partial(multiply, 3);

double(5); // => 10
triple(5); // => 15

Supply several arguments up front, leaving only the last one open.

const format = (prefix: string, sep: string, value: string) =>
  `${prefix}${sep}${value}`;

const withLabel = partial(format, 'Status', ': ');
withLabel('passing'); // => 'Status: passing'

View source on GitHub