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

ary

v0.1.0

Published

Clips function argument lengths.

Downloads

21

Readme

Synopsis

ary is a JavaScript function for restricting a function to a given number of arguments (i.e. capping its arity).

stability 5 - locked license - Unlicense Flattr this

browser support

Build Status Coverage Status Dependencies

Why?

It's trivial to implement, but in order to keep code DRY (and avoid silly mistakes) it makes sense to define this function only once per project. This library is the logical consequence of that.

Install

Node.js

With NPM

npm install ary

From source

git clone https://github.com/pluma/ary.git
cd ary
npm install
make

Browser

With component

component install pluma/ary

Learn more about component.

With bower

bower install ary

Learn more about bower.

With a CommonJS module loader

Download the latest minified CommonJS release and add it to your project.

Learn more about CommonJS modules.

With an AMD module loader

Download the latest minified AMD release and add it to your project.

Learn more about AMD modules.

As a standalone library

Download the latest minified standalone release and add it to your project.

<script src="/your/js/path/ary.globals.min.js"></script>

This makes the ary function available in the global namespace.

Basic usage example

var ary = require('ary');
var array = [1, 2, 3, 4];
var log = console.log.bind(console);

array.forEach(log); // passes in three arguments: item, index, array
/* Console output:
1, 0, [1, 2, 3, 4]
2, 1, [1, 2, 3, 4]
3, 2, [1, 2, 3, 4]
4, 3, [1, 2, 3, 4]
*/

array.forEach(ary(1, log)); // only logs the first argument: item
/* Console output:
1
2
3
4
*/

Advanced usage example

var ary = require('ary');

// Our example function: takes any number of arguments
// and returns them as a dash-separated string
function dashed() {
  return Array.prototype.slice.call(arguments).join('-');
}
dashed('a', 'b', 'c', 'd'); // 'a-b-c-d';

// We can create re-usable decorators
var nullary = ary(0);
var unary = ary(1);
var binary = ary(2);

var dashed0 = nullary(dashed);
dashed0('a', 'b', 'c', 'd'); // ''

var dashed1 = unary(dashed);
dashed1('a', 'b', 'c', 'd'); // 'a'

// Named arity functions for 0 to 3 are included out of the box:
var dashed2 = ary.binary(dashed);
dashed2('a', 'b', 'c', 'd'); // 'a-b'

// Or pass both arguments at once!
var dashed3 = ary(3, dashed);
dashed3('a', 'b', 'c', 'd'); // 'a-b-c'

// If a wrapped function is called with less arguments,
// they will be set to `undefined`:

dashed2('a'); // 'a-'
dashed3('a'); // 'a--'

// Although the original function has no length ...
dashed.length; // 0

// ... the wrapped functions always have the expected length!
dashed0.length; // 0
dashed1.length; // 1
dashed2.length; // 2
dashed3.length; // 3

// If the function is named, the wrapped function retains the name!
dashed.name; // 'dashed'
dashed0.name; // 'dashed'
dashed0.name === dashed.name; // true
dashed1.name === dashed.name; // true
dashed2.name === dashed.name; // true
dashed3.name === dashed.name; // true

API

ary(arity:Number, func:Function):Function

Wraps the given function in a new function of the given arity.

If the wrapped function is called with more than arity arguments, any additional arguments will be dropped.

If the wrapped function is called with less than arity arguments, any missing arguments will be set to undefined.

ary(arity:Number):Function

Returns a function that will wrap a function in a new function of the given arity.

ary.nullary(func:Function):Function

Shorthand for ary(0, func).

ary.unary(func:Function):Function

Shorthand for ary(1, func).

ary.binary(func:Function):Function

Shorthand for ary(2, func).

ary.ternary(func:Function):Function

Shorthand for ary(3, func).

Unlicense

This is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying UNLICENSE file.