Allows to bind any argument of a function using their names rather than their positions. This approach is more flexible if the initial arguments are to be left unbound. For example, from a function fn
fn(arg_0, arg_1, .., arg_[n-3], arg_[n-2], arg_[n-1])we can create a new function gn, which requires all arguments but the last and the third last parameter by applying the
gn = fn.partial({arg_[n-3]: val_[n-3}, arg_[n-1]: val_[n-1]})partial operation. The invocation of gn would look like gn(val_0, val_1, .., val_[n-2]). Notice that the relative position of the unbound arguments is left intact.
npm install @dizmo/functions-partial --saveconst { partial } = require('@dizmo/functions-partial');import { partial } from "@dizmo/functions-partial";const add = (lhs: number, rhs: number): number => {
return lhs + rhs;
};
const expect_inc = partial(add, {lhs: +1})(0) === +1;
const expect_dec = partial(add, {rhs: -1})(0) === -1;const add = (lhs: number, rhs: number): number => {
return lhs + rhs;
};
const inc = add.partial({lhs: +1});
const expect_inc = inc(0) === 1;
const nil = inc.partial({rhs: -1});
const expect_nil = nil( ) === 0;npm run cleannpm run buildnpm run -- build --no-lint --no-cleannpm run -- build --prepacknpm run -- build --prepack --no-minifynpm run lintnpm run -- lint --fixnpm run testnpm run -- test --no-lint --no-clean --no-buildnpm run covernpm run -- cover --no-lint --no-clean --no-buildnpm run docsnpm publishnpm publish --access=public© 2020 dizmo AG, Switzerland