npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

curry-arity

v1.0.0

Published

Tiny curry() and curryN() functions. Resulting functions will have the correct arity (length).

Downloads

6

Readme

curry-arity

MIT license npm version Build Status Coverage Status

Simple, tiny curry() and curryN() functions.

Both can take functions of any arity, there are no limits.

Resulting functions will always have the correct arity (length property).

This library does not use eval(), new Function() or any dynamic code evaluation.

Examples

const f = (a, b, c) => a + b + c;

const add = curry(f);

add.length; // 3
add(10).length; // 2
add(10)(20).length; // 1

add(10)(20)(30); // 60
add(10, 20)(30); // 60
add(10)(20, 30); // 60
add(10, 20, 30); // 60

/* reusable */
const addTen = add(10); 
addTen(20)(30); // 60
addTen(40)(50); // 100

Only the parameters before the first one with a default value are counted:

const add = curry((a, b, c = 30) => a + b + c);

add.length; // 2
add(10)(20); // 60
// add(10)(20)(70) would throw TypeError

/* you can pass additional arguments in the final call */
add(10)(20, 70); // 100

You can also specify arity using curryN() instead of curry():

const add = curryN(3, (a, b, c = 30) => a + b + c);

add.length; // 3
add(10)(20)(70); // 100

/* undefined always triggers the default value */
add(10)(20)(undefined); // 60

Another example with curryN():

const add = (...args) => args.reduce((a, b) => a + b);

curryN(2, add).length;  // 2
curryN(2, add)(10)(20); // 30

curryN(3, add).length;  // 3
curryN(3, add)(10)(20)(70); // 100

Curried functions also correctly handle implicit this:

const obj = {
  a: 10,
  addMe: curry(function (b, c) { return this.a + b + c; })
}  

/* 'this' from the first call (obj.addMe(20)) is always used */
obj.addMe(20)(30); // 60 

How it works

Using Object.defineProperty() on created function objects. The original function object is not changed in any way.

By the ES6 specification, function.length is configurable. It wasn't configurable in ES5.

Supported Environments

This library will not work in old ES5 browsers, namely IE11. By design, functions will throw if the length cannot be set, as the correct arity is a part of the API.

The code is not transpiled to ES5, as the ES6 version is more efficient and the library wouldn't work in all ES5 browsers anyway. So, supported environments are:

  • Node >= 6
  • Browsers that support basic ES6 syntax

Installation and Usage

Node

$ npm install curry-arity
const { curry, curryN } = require('curry-arity');

const f = (a, b, c = 30) => a + b + c;

const fc = curry(f);
console.log(fc(10)(20)); // 60

const fc3 = curryN(3, f);
console.log(fc3(10)(20)(70)); // 100

Browser Script

Unminified (~600B):

<script src="https://unpkg.com/[email protected]/dist/curryarity.js"></script>

or

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/curryarity.js"></script>

Minified (~250B):

<script src="https://unpkg.com/[email protected]/dist/curryarity.min.js"></script>

or

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/curryarity.min.js"></script>

The script creates a global variable CurryArity:

const f = (a, b, c = 30) => a + b + c;

const fc = CurryArity.curry(f);
console.log(fc(10)(20)); // 60

const fc3 = CurryArity.curryN(3, f);
console.log(fc3(10)(20)(70)); // 100

You can also install the package and take the scripts from the dist folder.

ES Module

$ npm install curry-arity

ES module: ./node_modules/curry-arity/src/index.js

If you are using Webpack, Rollup or any tool that reads from pkg.module, then simply:

import { curry, curryN } from 'curry-arity';

const f = (a, b, c = 30) => a + b + c;

const fc = curry(f);
const fc3 = curryN(3, f);

and it will use the ES module directly, instead of the CommonJS version.

API

curry(f)

Returns curried f, with the original arity.

f

Function of any arity.

curryN(n, f)

Returns curried f, with the specified arity.

n

Arity of the resulting function. Should be a non-negative integer.

It can be less, equal or greater than the original arity.

f

Function of any arity.

By design, the functions do not validate provided arguments.

About

Author

Milos Djermanovic

License

MIT License