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

opt-args

v1.0.1

Published

Simple function overloads for optional arguments

Downloads

3

Readme

Simple and unobtrusive JS function overloads for optional arguments

Motivation

There are plenty of npm modules out there that let you do really complex, typed function overloading, but 90% of the time all that's needed is to create convenience methods with less parameters that 'drop' optional arguments.

For example, you might want a function with convenience overloads as follows:

var connect = function(host, port, timeout, handler) { }

connect('npmjs.com', 80, 30000, callback);

connect('npmjs.com', 80, callback);

connect('npmjs.com', callback);

connect('npmjs.com');

Implementing the above using the existing modules requires you to define multiple function bodies and add at least half a dozen lines of dead code.

I wanted to achieve this with concise syntax and minimal fuss based purely on the number of arguments passed in.

Approach

First let's deconstruct the overloading in the example above:

  • 4 args: connect(host, port, timeout, callback)
  • 3 args: connect(host, port, callback)
  • 2 args: connect(host, callback)
  • 1 args: connect(host)

You can see two clear groups of arguments here - those in italics are "dropped" before those in bold.

This can be represented by assigning a numerical priority to each parameter:

  • host: 1
  • port: 2
  • timeout: 2
  • callback: 1

Installation

npm install opt-args

Usage

opt-args allows you to assign a priority to each argument, controlling which arguments To achieve the overloading in the example above with opt-args, define your function as follows:

var opt = require('opt-args');

var connect = opt(1,2,2,1, function(host, port, timeout, handler) { });

That's it! Note that only a single funciton body is needed. opt-args ensures that values are routed to the correct parameters.

The four numbers (1,2,2,1) assign a priority to each of the four arguments, with lower values being of higher priority.

Lower priority arguments (higher numerical value) will be "dropped" first, and the right-most of two arguments with the same priority will be dropped first.

Any number of priority groups are supported.

License

ISC