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

argumentor

v0.5.1

Published

function tool that takes care of parameters

Downloads

8

Readme

Argumentor Build Status

NPM

NPM

Argumentor is a little utility tool that brings types, defaults and arbitrary combinations to your function's arguments.

Installation

npm install argumentor

Types

Have you ever been doing this?

function foo(a, b) {
    a = +a;         //Number cast
    b = b + '';     //String cast
    //...
}

Argumentor wraps your function and takes care of types:

var argumentor = require('argumentor');

var fooWrapped = argumentor(foo)
    .a('a').number()
    .a('b').string();

fooWrapped('1', 123); //arguments would be [1, '123']

Defaults

Have you ever been doing this?

function foo(a, b) {
    a = a || 'foo';
    b = b || null;
    //...
}

Argumentor wraps your function and takes care of defaults:

var argumentor = require('argumentor');

var fooWrapped = argumentor(foo)
    .a('a').default('foo')
    .a('b').default(null);

fooWrapped(); //arguments would be ['foo', null]

Combinations

Have you ever been doing this?

function foo(name, options, callback) {
    if(typeof name === 'function') {
        callback = name;
        name = 'defaultName';
        options = {};
    } else if (typeof options === 'function') {
        callback = options;
        options = {};
    }
    //maintenance nightmare!
}

Argumentor wraps your function and recognizes possibile argument combinations:

var argumentor = require('argumentor');

var fooWrapped = argumentor(foo)
    .a('name')
    .a('options')
    .a('callback')
    .combinations([['name', 'options', 'callback'], ['name', 'callback'], ['callback']]);

fooWrapped(function cb() {});                       //arguments would be [undefined, undefined, function cb() {}]
fooWrapped('barName', function cb() {});            //arguments would be ['barName', undefined, function cb() {}]
fooWrapped('barName', {a: 'b'}, function cb() {});  //arguments would be ['barName', {a: 'b'}, function cb() {}]

Combined usage

The real power of argumentor comes into play when using this three abilities of argumentor in combination:

var argumentor = require('argumentor');

var fooWrapped = argumentor(foo)
    .a('name').string().default('nameDefault')
    .a('options').object().default(function() { return {}; })
    .a('callback').func().default(function() { return function() {}; })
    .combinations([['name', 'options', 'callback'], ['name', 'callback'], ['callback']]);

API

argumentor(fn)

Creates a wrapped version of fn. Features a list of configuration methods:

.a(name)

Opens a context, the following configuration methods are invoked on that context. name denotes a handle for the argument. The order of argument contexts does matter!

.bool()

Denotes that the current argument context should be casted to Boolean.

.func()

Denotes that the current argument context should be checked to be a function. If this is not the case, an error is thrown.

.number()

Denotes that the current argument context should be casted to Number.

.object()

Denotes that the current argument context should be checked to be a object. If this is not the case, an error is thrown.

.string()

Denotes that the current argument context should be casted to String.

.default(value|fn)

Sets a default value for the argument of the current execution context. If the assigned value is callable it will be invoked to retrieve a default value on run time.

Let's say your default value is an empty object of type Foo:

var argumentor = require('argumentor');

var fooWrapped = argumentor(foo)
    .a('foo').object().default(function() {
        return new Foo();
    });

.combinations(combinations)

Specifies possible argument combinations. Has to be an array of arrays of Strings. The names in the combinations list refer to the names of the argument execution contexts.