@abdelrahman-elkady/partial-function-builder

1.0.0 • Public • Published

Partial Function Builder

Partial function builder is a simple utility that allows having functions with partially applied arguments to their named arguments using object destructuring.

Note

This utility only supports -till now- supplying named partial arguments to one single argument that the original function accepts. You can check lodash's partial to apply partial arguments to positional arguments.

See usage for more details.

Installation

npm i @abdelrahman-elkady/partial-function-builder

Usage

Simple Usage

 const { toPartialBuilder } = require('@abdelrahman-elkady/partial-function-builder');

 const addAndMultiply = ({ n1, n2, n3 }) => (n1 + n2) * n3;
 const addPartialBuilder = toPartialBuilder(addAndMultiply);
 const multiplyWithThree = addPartialBuilder({ n3: 3 });
 multiplyWithThree({ n1: 1, n2: 1 }); // 6 .. expression evaluated is (1 + 1) * 3

Composition

 const { toPartialBuilder } = require('@abdelrahman-elkady/partial-function-builder');

 const add = ({ n1, n2, n3 }) => n1 + n2 + n3;

 const addPBuilder = toPartialBuilder(add);
 const partialOne = addPBuilder({ n1: 5 })

 partialOne({ n2: 2, n3: 3 }); // 10

 // Create a partial builder for the partial function `partialOne`
 const partialOnePBuilder = toPartialBuilder(partialOne);
 const partialTwo = partialOnePBuilder({ n2: 3 });
 partialTwo({ n3: 1 }); // 9

Overriding

 const { toPartialBuilder } = require('@abdelrahman-elkady/partial-function-builder');

 const add = ({ n1, n2, n3 }) => n1 + n2 + n3;

 const addPBuilder = toPartialBuilder(add);
 const partialOne = addPBuilder({ n1: 5 })

 partialOne({ n2: 2, n3: 3, n1: 10 }); // 15

Logo

Credits: font awesome

Package Sidebar

Install

npm i @abdelrahman-elkady/partial-function-builder

Weekly Downloads

0

Version

1.0.0

License

ISC

Unpacked Size

24 kB

Total Files

9

Last publish

Collaborators

  • abdelrahman-elkady