
Product
A New Overview in our Dashboard
We redesigned Socket's first logged-in page to display rich and insightful visualizations about your repositories protected against supply chain threats.
Curry any function with placeholder support
curriable
provides a curry
method that is highly performant with a small footprint (551 bytes minified+gzipped). You can call the method with any combination of parameters (one at a time, all at once, or any number in between), and placeholders are supported.
If fn
is the curried function, the following are all equivalent:
fn(1)(2)(3)
fn(1)(2, 3)
fn(1, 2)(3)
fn(1, 2, 3)
fn(_, 2, 3)(1)
fn(_, _, 3)(1)(2)
fn(_, _, 3)(1, 2)
fn(_, 2)(1)(3)
fn(_, 2)(1, 3)
fn(_, 2)(_, 3)(1)
You can use the default import:
import curry from "curriable";
const fn = curry((a, b, c) => [a, b, c]);
console.log(fn("a", curry.__, "c")("b")); // ["a","b","c"]
Or the named imports:
import { __, curry } from "curriable";
const fn = curry((a, b, c) => [a, b, c]);
console.log(fn("a", __, "c")("b")); // ["a","b","c"]
The curry
method has the following signature:
function curry(fn: function, arity: number = fn.length) => function;
arity
defaults to be the length provided by fn.length
, but be aware this can cause unusual behavior with default parameters or use of rest parameters. See the documentation on Function.length for more details.
console.log(function(...args) {}.length); // 0 arity computed
When using rest with curried functions, you should pass a second parameter to explicitly declare the correct arity
:
const fn = (...args) => [a, b, c];
const curried = curry(fn, 3);
console.log(curried("a")("b")("c")); // ["a","b","c"]
console.log(function(a, b = 1, c) {}.length); // 1 arity computed
Default parameters are very rare use-case with curried functions, but it is possible to trigger them if you declare an explicit arity
and explicitly pass undefined
for that parameter:
const fn = (a, b = 1, c) => [a, b, c];
const curried = curry(fn, 3);
console.log(curried("a")(undefined)("c")); // ["a",1,"c"]
Yes, this is weird, but it is very difficult (impossible?) to distinguish between a parameter being undefined through not being called yet in the curry chain vs being undefined by not being provided an explicit value. Explicitly passing undefined
provides that distinction.
All values provided are the number of operations per second (ops/sec) calculated by the Benchmark suite. The same function was curried and tested passing each parameter individually, passing all at once, and using placeholders.
Benchmarks were performed on an i7 8-core Arch Linux laptop with 16GB of memory using NodeJS version 8.9.4
.
Library | Operations / second | Relative margin of error |
---|---|---|
curriable | 1,673,501 | 1.62% |
ramda | 1,032,968 | 0.74% |
lodash | 153,464 | 0.95% |
Library | Operations / second | Relative margin of error |
---|---|---|
curriable | 21,851,199 | 1.09% |
ramda | 8,256,763 | 1.19% |
lodash | 6,953,740 | 1.06% |
Library | Operations / second | Relative margin of error |
---|---|---|
curriable | 2,488,499 | 0.68% |
ramda | 1,317,015 | 0.97% |
lodash | 202,201 | 0.63% |
Standard stuff, clone the repo and npm install
dependencies. The npm scripts available:
benchmark
=> run the benchmark suite pitting curriable
against other libraries in common use-casesclean
=> run clean:lib
, clean:es
, and clean:dist
clean:dist
=> run rimraf
on the dist
folderclean:es
=> run rimraf
on the es
folderclean:lib
=> run rimraf
on the lib
folderdev
=> run webpack dev server to run example app (playground!)dist
=> runs clean:dist
and build
lint
=> runs ESLint against all files in the src
folderlint:fix
=> runs `lint``, fixing any errors if possibleprepublish
=> runs compile-for-publish
prepublish:compile
=> run lint
, flow
, test:coverage
, transpile:lib
, transpile:es
, and dist
test
=> run AVA test functions with NODE_ENV=test
test:coverage
=> run test
but with nyc
for coverage checkertest:watch
=> run test
, but with persistent watchertranspile:es
=> run babel against all files in src
to create files in es
, preserving ES2015 modules (for pkg.module
)transpile:lib
=> run babel against all files in src
to create files in lib
1.0.0
FAQs
Convert any method to be curriable with placeholder support
The npm package curriable receives a total of 11,388 weekly downloads. As such, curriable popularity was classified as popular.
We found that curriable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We redesigned Socket's first logged-in page to display rich and insightful visualizations about your repositories protected against supply chain threats.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.