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

named-parameters

v0.0.3

Published

A small and simple utility for working with named parameters. (I.e., for using maps as function arguments, rather than arrays.) Inspired by optimist.

Downloads

24

Readme

Named Parameters

Named Parameters is a small and simple utility for working with named parameters in JavaScript/CoffeeScript. That is, named-parameters is a Node.js module intended to make it easier to write functions that use map (i.e. associative arrays or, more accurately, JavaScript Object) parameters that use property names to define the role of each argument rather than relying on their ordinal position.

For example, using named-parameters, you can more easily replace a method signature like this:

        util.inspect( myobject, null, 4, true )

with one like this:

        inspect( myobject, { depth:4, colors:true } )

or (in CoffeeScript):

        inspect myobject, depth:4, colors:true

Installation

To install named-parameters with npm, use:

        > npm install named-parameters

or add a dependency such as:

        "dependencies" : {
          "named-parameters" : "latest"
        }

to your package.json file and run npm install.

Quick Start

Named Parameters provides a simple DSL that offers utility functions for working with maps-as-arguments.

For example, given:

        > var args = { a: 1, c: "no" }

and

        > var np = require('named-parameters')

You can specify default values:

        > params = np.parse(args).default('b','foo').values()
        { a: 1, c: 'no', b: 'foo' }

or convert them in a sane way:

        > params = np.parse(args).coerce('c','boolean').values()
        { a: 1, c: false }

or valdidate them:

        > params = np.parse(args).require('a','positive integer').values()
        { a: 1, c: 'no' }

or do all three at once:

        > params = np.parse(args).default('b','foo').coerce('c','boolean').
        ... require('a','positive integer').values()
        { a: 1, c: false, b: 'foo' }

Methods

General

The named-parameters module exports a parse function that returns an object against which you can chain the various method calls. At the end of the chain, you invoke the values() method to obtain the final results.

        args = np.parse( args ).values()

In the simplest case, named-parameters doesn't do much at all (although it does "clone" the input Object so that one can modify the output Object without changing the original).

Specifying Default Values

To specify a default value you can add the default method to the chain:

        args = np.parse( args ).default('foo','bar').values()

You can add multiple default calls to the chain to specify multiple default values:

        args = np.parse( args ).default('foo','bar').default('height',170).values()

or simply pass a map to a single default (or defaults) method call:

        args = np.parse( args ).defaults({foo:'bar',height:170}).values()

In CoffeeScript this yields a fairly clean and concise syntax:

        args = np.parse( args ).defaults( foo:'bar', height:170 ).values()

Validating Parameters

You can use named-parameters to enforce conditions on the input parameters using the require method.

For example:

        args = np.parse( args ).require('bar').values()

Here, when the input args['bar'] is missing (undefined) or null, an exception will be thrown.

There are several such validations available.

To ensure that a given input value is a non-null String:

        args = np.parse( args ).require('bar','string').values()

To ensure that a given input value is a non-null, non-blank String:

        args = np.parse( args ).require('bar','non-empty string').values()

...a non-null Array:

        args = np.parse( args ).require('bar','array').values()

...a non-empty Array:

        args = np.parse( args ).require('bar','non-empty array').values()

...a boolean value:

        args = np.parse( args ).require('bar','boolean').values()

...a function:

        args = np.parse( args ).require('bar','function').values()

...a number:

        args = np.parse( args ).require('bar','number').values()

...a positive integer:

        args = np.parse( args ).require('bar','positive integer').values()

...etc.

There are many more validation types available (see the detailed documentation for more examples), and if you are so inclined you can even pass in your own validation method:

        function is_odd(num) { return num%2 == 1; }
        args = np.parse( args ).require('bar',is_odd).values()

(Following the example of optimist, the function demand can be used as an alias for require.)

Coercing Types

The coerce function provides a mechanism for automatically converting a parameter into given type (if possible).

For instance, the chain:

        args = np.parse( { bar: '17' } ).coerce('bar','integer').values()

yields:

        { bar: 17 }

since the coerce method will convert the string "17" to the number 17.

Conversely, the chain:

        args = np.parse( { bar: 17 } ).coerce('bar','string').values()

yields:

        { bar: '17' }

since the coerce method converts the number 17 into the string "17".

Several coerce options are available.

To a numeric type:

        > args = np.parse( { bar: 17 } ).coerce('bar','number').values()
        { bar: 17 }
        
        > args = np.parse( { bar: '17' } ).coerce('bar','number').values()
        { bar: 17 }

        > args = np.parse( { bar: 17.23 } ).coerce('bar','number').values()
        { bar: 17.23 }

        > args = np.parse( { bar: '17.23' } ).coerce('bar','number').values()
        { bar: 17.23 }

To an integer:

        > args = np.parse( { bar: 17 } ).coerce('bar','number').values()
        { bar: 17 }
        
        > args = np.parse( { bar: '17' } ).coerce('bar','number').values()
        { bar: 17 }

        > args = np.parse( { bar: 17.23 } ).coerce('bar','number').values()
        { bar: 17 }

        > args = np.parse( { bar: '17.23' } ).coerce('bar','number').values()
        { bar: 17 }

To a boolean:

        > args = np.parse( { bar: true } ).coerce('bar','boolean').values()
        { bar: true }
    
        > args = np.parse( { bar: "true" } ).coerce('bar','boolean').values()
        { bar: true }
            
        > args = np.parse( { bar: "yes" } ).coerce('bar','boolean').values()
        { bar: true }
            
        > args = np.parse( { bar: "on" } ).coerce('bar','boolean').values()
        { bar: true }
            
        > args = np.parse( { bar: "1" } ).coerce('bar','boolean').values()
        { bar: true }
            
        > args = np.parse( { bar: "3" } ).coerce('bar','boolean').values()
        { bar: false }
            
        > args = np.parse( { bar: "yellow" } ).coerce('bar','boolean').values()
        { bar: false }

        > args = np.parse( { bar: 6 } ).coerce('bar','boolean').values()
        { bar: true }
                
        > args = np.parse( { bar: 0 } ).coerce('bar','boolean').values()
        { bar: false }
                
        > args = np.parse( { bar: -14 } ).coerce('bar','boolean').values()
        { bar: true }

To an array:

        > args = np.parse( { bar: [] } ).coerce('bar','array').values()
        { bar: [] }

        > args = np.parse( { bar: [ 7 ] } ).coerce('bar','array').values()
        { bar: [ 7 ] }

        > args = np.parse( { bar: 7 } ).coerce('bar','array').values()
        { bar: [ 7 ] }

etc.

See the detailed documentation for more examples.

Note

This module is developed following the git-flow workflow/branching model.

The default master branch only contains the released versions of the code and hence may seem relatively stagnant (or stable, depending upon your point of view).

Most of the action happens on the develop branch or in feature branches.