Skip to content

abdelrahman-elkady/partial-function-builder

Repository files navigation

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published