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

this-curry

v1.0.1

Published

A simple and flexible way to curry functions, preserving this.

Downloads

7

Readme

this-curry

A simple and flexible way to curry functions, preserving this.

Install

Download the package through npm:

$ npm install this-curry

Or download it from the source.

Usage

// In the browser
<script src="/js/this-curry.min.js"></script>
// Use `window.curry`

// In Node
var curry = require('this-curry');

Syntax

curry(f, options)
  • f: The function to curry.

  • options: An options object that specifies the behaviour of the curried function. The available options are:

    • length: The expected length of the function. Usually determines how many arguments need to be passed before calling the function. Defaults to function.length.
    • latest_this: If true, use the context of the last call of the curried function when calling the given function, instead of saving the first. Defaults to false (Use the this that curry was called with).
    • name: Name of the curried function. Defaults to f.name.
    • validator: See customisations.

Examples

var sum = curry(function sum(a, b, c) {
  return a + b + c;
});

sum(1, 2, 3);
sum(1, 2)(3);
sum(1)(2, 3);
sum()(1)()(2)()()(3);  // All 6


var body_event_listener = curry.call(document.body, EventTarget.prototype.addEventListener, {
  length: 3
});


var body_on_click = body_event_listener('click');
var body_on_keydown = body_event_listener('keydown');

body_on_click(function(e) {
    // ...
}, false);

Customisations

options.validator should be a function that is called every time the curried function is called.

It is called with 4 arguments, args, length, f, name.

  • args is an Arguments object of the arguments that have been passed so far.
  • required_length is the length of the function.
  • f is the original (uncurried) function.
  • name is the name of the function.

It is also called with the context (this) that would call f if it needs to be called right now.

options.validator should return either true, false or another object.

If it returns false, the curried function returns another curried function, possible with some more arguments filled in.

If it returns true, the curried function calls the original function with the arguments that were given.

If it returns an object, that object could have the following properties:

  • this: The new value for the context to call the original function with. (Alternatively that for linters that complain about the reserved word this)
  • args: The new value for the arguments to call the original function with. Useful to remove invalid arguments. Should be an Array.
  • length: The new value for the number of arguments before the function should be called. Passed as the length parameter to the validator function.
  • f: The new function to replace the original function. For example, to change which function is called based on an argument.
  • is_valid: A boolean for whether or not the function should be called. It is called if true. Defaults to false.

The default validator is curry.default_validator, which is simply:

function default_validator(args, required_length) {
  return args.length >= required_length;
}

(Which just checks if the length of the arguments is not less than the required length)

Another validator given is curry.limit_args, which will not pass in extra arguments, by returning an object with args sliced to the required length.

function add(a, b) {
  return a + b;
}

function sum() {
  return Array.prototype.reduce.call(arguments, add, 0);
}

var sum_5_or_more = curry(sum, { length: 5 });
var sum_5 = curry(sum, { length: 5, validator: curry.limit_args });

sum_5_or_more(1)(2, 3, 4)(5, 6, 7);  // 28
sum_5        (1)(2, 3, 4)(5, 6, 7);  // 15, as 6 and 7 are ignored.