The callback should be the last argument. That's the style. So you have to write a bit of logic to test if the second argument
is an object or a function and act appropriately. Ths is cruft code getting in the way of real work.
myAPI.doStuff = function(){ var stuff = arguments[0] var options = 'function' == typeof(arguments[1]) ? {} : arguments[1] var callback = 'function' == typeof(arguments[2]) ? arguments[2] : arguments[1] ...}
With this module, you specify what you want using a simple expression language:
myAPI.doStuff = function(){ var args = norma('so?f',arguments) var stuff = args[0] var options = args[1] || {} var callback = args[2]}
Now your arguments always come back in a well-defined array, and always at the same index.
The expression 'so?f' means match: a string, an optional object, and a function
And of course, if your function is called with arguments that do not match the expression, then an error is thrown.
Support
If you're using this module, feel free to contact me on twitter if you have any questions! :) @rjrodger
Current Version: 0.2.6
Tested on: node 0.10.26
Quick example
var norma = require('norma')function foo() { var args = norma('sf', arguments) var content = args[0] // s => string, required var cb = args[1] // f => function, required cb(null,content+'!')}foo('bar',function(err,out){ console.log(out)})
Install
npm install norma
Function Signature Expression
The expression you use to define the function argument types that you
expect is a string containing individual characters that stand for
JavaScript types. Each type is a single character. These are:
s - string
i - integer
n - number
b - boolean
f - function
a - array
o - object
r - regexp
d - date
g - arguments
e - error
N - null
U - undefined
A - NaN
Y - Infinity
You list the types you expect, in the order you expect. The norma
module will return an array with each position corresponding to the position of each type letter.