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

arguer

v1.0.0

Published

Normalizes arguments for JavaScript functions with optional arguments and provides optional typing.

Downloads

46

Readme

Arguer

Arguer normalizes arguments for JavaScript functions with optional arguments and provides optional typing.

You could think of Arguer as a crude way to help implement function overloading, but it takes a different approach from other function overloading libraries and focuses more on handling optional parameters than traditional overloading.

Installation

npm install arguer

... or npm install --save arguer if you want arguer added to your package.json automatically.

Simple Example

A function's arguments and a format array are passed to the arguer function which returns a normalized object.

var arguer = require('./arguer.js');

var _testFormat = ['a', {name: 'b', optional: true}, 'c'];
function test (a, b, c) {
  var args = arguer.apply(_testFormat, arguments);
  console.log(args);
}

test('hello', 'world');

Outputs:

{ a: 'hello', b: undefined, c: 'world' }

Note that _testFormat was declared outside the function which uses it. This is obviously not necessary, but there is a performance benefit to reusing a format array, not only because there is no overhead incurred in creating and garbage collecting an array, but also because Arguer won't have to re-count how many optional arguments exist every time the function is called.

This would have been roughly equivalent to the following code:

function test (a, b, c) {
  if (arguments.length < 3) {
    c = b;
    b = undefined;
  }
  console.log({a: a, b: b, c: c});
}

With only one or two occasional optional arguments, it is not difficult to handle this with your own code such as above. Much of Arguer's usefulness comes from its ability to handle more complicated scenarios.

For performance reasons, arguer.apply(format, arguments) is used because it is the only way to pass the arguments object to another function which is supported by the V8 optimizing compiler.

Format Array

In the simple example above, we used

['a', {name: 'b', optional: true}, 'c']

as our format array. It says a and c are required, and b is optional. Each element of the format array is either a string representing a required argument of any type, or an object which describes the accepted argument in more detail. These objects must contain the 'name' property. All other properties are optional.

Properties

name : string

The name of the argument.

optional : boolean

If true, the argument will be considered optional.

type : string

If supplied typeof will be used to assess whether an argument is a match.

nType : string

Works opposite of type. In other words, an argument must not match this type in order to match.

instance : Function

Similar to type, except instanceof is used for comparison instead of typeof.

nInstance : Function

Opposite of instance.

default : string

Specifies a default value for an argument. If a default value is provided, optional:true is implied, and can be omitted.

Back-Reference Properties

Each of the following properties accept a string which represents the name of a previous argument. Only previous arguments can referenced. In other words, arg with index 4 can reference any arg 0 through 3, but cannot reference arg 5. If any one or more of the following properties are used, optional:true is implied, and can be omitted.

requires : string|string[]

The names of one or more preceding arguments, all of which must have been fulfilled in order for the current argument to be considered.

requiredBy : string|string[]

The names of one or more preceding arguments which, if all of them are fulfilled, causes the current argument to be required instead of optional.

mutex : string|string[]

Essentially the opposite of requires&requiredBy. If any of the named arguments were fulfilled, then the current argument will not be considered.

Matching Algorithm

It is important to understand that the matching algorithm evaluates arguments in order. It makes no attempt to find the "best" match for a format. If we modified our original simple example to make the c argument optional with type: 'string'

var _testFormat = ['a', {name: 'b', optional: true}, {name: 'c', optional: true, type: 'string'}];
function test (a, b, c) {
  var args = arguer.apply(_testFormat, arguments);
  console.log(args);
}

test('hello', 'world');

we might intuitively think 'world' would be a better fit for argument c since it's a string; however, it will be assigned to argument b because arguments are evaluated in-order and b can match anything. Therefore, this will output:

{ a: 'hello', b: 'world', c: undefined }

If we really want c to handle strings, and b to handle anything else, we could reorder the arguments so c comes first, or we could use nType and apply nType: 'string' to b.

Errors

If the arguments provided cannot be matched according to the rules of the format array, an Error object is returned instead of a normal object. This can be checked using the instanceof operator.

function test (a, b, c) {
   var args = arguer.apply(['a', 'b', 'c'], arguments);
   if (args instanceof Error) {
     console.log(args.message);
     return;
   }
}

test('hello', 'world');

Outputs:

Not enough arguments provided.

If you would like arguer to automatically throw in the event of an error, use arguer.thrower instead. The following example will automatically throw an error.

var arguer = require('arguer').thrower;

function test (a, b, c) {
   var args = arguer.apply(['a', 'b', 'c'], arguments);
}

test('hello', 'world');