Giter VIP home page Giter VIP logo

pfa's Introduction

pfa

Crazy-simple Partial Function Application library

Build Status npm version

About

Partial function application is way cool! Imagine having a function that you use in multiple places, but its parameters are often repeated due to the configuration not changing. You could wrap the function yourself, or you could use a library like pfa to partially apply arguments to the function.

For instance, the npm library clone takes 3 parameters: clone(obj, isCircular, depth) - imagine that we always use the same values for the latter 2 arguments:

const result = clone(myObj, false, 50);

It'd be much nicer to just call clone(myObj), so we could in turn do something like the following:

const { partialApply, _ } = require("pfa");
const _clone = require("clone");

const clone = partialApply(
    _clone,     // The function to partially apply arguments
    _,          // An argument we will provide later
    false,      // An argument to always provide at the second position
    50          // An argument to always provide at the third position
);

const myObj = { key: "value" };
const clonedObj = clone(myObj); // equiv: clone(myObj, false, 50);

Usage

pfa exports 3 items:

  • partialApply: The partial application function
  • _: Placeholder for unknown arguments
  • partialApplyRight: Partial application to the right side of a function

It also exports the partialApply function as the default, so CommonJS and ES6 imports can both be used neatly:

const { partialApply, _ } = require("pfa");

Or:

import partialApply, { _ } from "pfa";

You can also apply to the right-hand side of a function using partialApplyRight, like so:

import { partialApplyRight } from "pfa";

function myMethod(target, defaultValue) {
    // some functionality
}

const shorthandMethod = partialApplyRight(myMethod, {});
shorthandMethod({ some: "argument" }); // calls myMethod with 2 arguments

pfa supports NodeJS 6.10 onwards. For browser usage you should transpile to ES5 using something like BabelJS.

Consult the API documentation for more information.

Installation

Simply install as a dependency:

npm install pfa --save

pfa's People

Contributors

perry-mitchell avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.