Skip to content

dizmo/functions-partial

Repository files navigation

NPM version Build Status Coverage Status

@dizmo/functions-partial

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.

Usage

Install

npm install @dizmo/functions-partial --save

Require

const { partial } = require('@dizmo/functions-partial');

Examples

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;

Development

Clean

npm run clean

Build

npm run build

without linting and cleaning:

npm run -- build --no-lint --no-clean

with UMD bundling (incl. minimization):

npm run -- build --prepack

with UMD bundling (excl. minimization):

npm run -- build --prepack --no-minify

Lint

npm run lint

with auto-fixing:

npm run -- lint --fix

Test

npm run test

without linting, cleaning and (re-)building:

npm run -- test --no-lint --no-clean --no-build

Cover

npm run cover

without linting, cleaning and (re-)building:

npm run -- cover --no-lint --no-clean --no-build

Documentation

npm run docs

Publish

npm publish

initially (if public):

npm publish --access=public

Copyright

© 2020 dizmo AG, Switzerland