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
with one like this:
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 =
You can specify default values:
> params = np a: 1 c: 'no' b: 'foo'
or convert them in a sane way:
> params = np a: 1 c: false
or valdidate them:
> params = np a: 1 c: 'no'
or do all three at once:
> params = np ... 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
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
You can add multiple default
calls to the chain to specify multiple default values:
args = np
or simply pass a map to a single default
(or defaults
) method call:
args = np
In CoffeeScript this yields a fairly clean and concise syntax:
args = np
Validating Parameters
You can use named-parameters
to enforce conditions on the input parameters using the require
method.
For example:
args = np
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
To ensure that a given input value is a non-null
, non-blank String:
args = np
...a non-null
Array:
args = np
...a non-empty Array:
args = np
...a boolean value:
args = np
...a function:
args = np
...a number:
args = np
...a positive integer:
args = np
...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:
{ return num%2 == 1; } args = np
(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
yields:
bar: 17
since the coerce
method will convert the string "17"
to the number 17
.
Conversely, the chain:
args = np
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 bar: 17 > args = np bar: 17 > args = np bar: 1723 > args = np bar: 1723
To an integer:
> args = np bar: 17 > args = np bar: 17 > args = np bar: 17 > args = np bar: 17
To a boolean:
> args = np bar: true > args = np bar: true > args = np bar: true > args = np bar: true > args = np bar: true > args = np bar: false > args = np bar: false > args = np bar: true > args = np bar: false > args = np bar: true
To an array:
> args = np bar: > args = np bar: 7 > args = np 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.