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

argshim

v0.0.6

Published

JavaScript module for handling properly typed optional function arguments

Downloads

2

Readme

argShim

JavaScript module for handling properly typed optional function arguments.

The problem

Parsing the optional arguments on a JavaScript function call can be a complex process when number of arguments increases. Usually the code ends up looking something like this:

function functionWithOptionalArgs(optString, optNumber, optFunction) {
	if(typeof optNumber === 'function') {
		optFunction = optNumber;
		optNumber = undefined;
	}
	if(typeof optString === 'function') {
		optFunction = optString;
		optString = undefined;
	}
	if(typeof optString === 'number') {
		optNumber = optString;
		optString = undefined;
	}
	...
}

Solution

The argShim provides a simple function wrapper that can be used for encapsulating a function call in a way that provides a consistent way for parsing and passing optional and required arguments. For example:

var argShim = require('argShim');

function functionWithOptionalArgs(optString, optNumber, optFunction) {
	...
}

var argSpecs = [
  {optional:'String'},
  {optional:'Number'},
  {optional:'Function'}
];
var fn = argShim(argSpecs, functionWithOptionalArgs);
fn('str');                   // function called with 'str', undefined, undefined
fn(42);                      // function called with undefined, 42, undefined
fn(function(){});            // function called with undefined, undefined, function
fn(42, function(){});        // function called with undefined, 42, function
fn('str', 42, function(){}); // function called with 'str', 42, function

Basically argShim ensures that the wrapped function is always called with full list of arguments and all missing arguments will be passed as undefined.

Required arguments can be specified by using required attribute on the argument specification objects:

...
var argSpecs = [
  {optional:'String'},
  {required:'Number'},
  {optional:'Function'}
];
var fn = argShim(argSpecs, functionWithOptionalArgs);
fn('str');                   // throws an error, required number is missing
fn(42);                      // function called with undefined, 42, undefined
fn(function(){});            // throws an error, required number is missing
fn(42, function(){});        // function called with undefined, 42, function
fn('str', 42, function(){}); // function called with 'str', 42, function

Default values for optional arguments can be specified with the default attribute:

...
var argSpecs = [
  {optional:'String', default:'foo'},
  {required:'Number'},
  {optional:'Function'}
];
var fn = argShim(argSpecs, functionWithOptionalArgs);
fn('str');                   // throws an error, required number is missing
fn(42);                      // function called with 'foo', 42, undefined
fn(function(){});            // throws an error, required number is missing
fn(42, function(){});        // function called with 'foo', 42, function
fn('str', 42, function(){}); // function called with 'str', 42, function

Module API

The argShim module exports a single function that has the following signature:

argShim(argSpecs, wrappedFunction)

Parameters

argSpecs: An array of objects that define the arguments for the resulting function. Each object must have either 1) required or 2) optional property and the property value must be 1) a string that defines the name of the class for the object or 2) an array of strings that defines all types for the object that can be accepted as the parameter at the given slot. If argument is optional it can specify an additional property called default that will be passed instead of the undefined when optional argument is missing from the call. The value must have the same type that was specified in the optional string or string array.

wrappedFunction: Function that will be called with the parsed arguments.

Description

The return value of the argShim call is a new function that will accept all calls that specify the required and optional parameters. Issued a valid call the function will call the wrapped function with a value for each parameter. Missing parameters will have value undefined.

The resulting function will throw an Error if the function is called with arguments that do not match with the specified argument array.

If argShim is called with invalid parameters (e.g. with a non-array as the first parameter or without the wrapped function) the function will throw an Error.