Arity
Makes sure that JavaScript functions are called with the expected number and/or types of parameters.
Background
Just like in any programming language JavaScript functions take parameters. Unlike many other programming languages, JavaScript does not require functions to be called with the defined number of parameters.
For example:
{ return a + b;}; // returns 3; // returns NaN
Depending on the situation, it might be preferred that an error is thrown when the wrong number of parameters are passed in.
Usage
First, install the library.
Then, it's as simple as wrapping your function with ar
:
var sum = ;; // returns 3; // throws "Wrong number of parameters. Excpected 2, got 1. Params: a, b."
It's even easier in CoffeeScript (isn't everything?):
sum = ar a + bsum12 # returns 3 sum123 # throws "Wrong number of parameters. Excpected 2, got 3. Params: a, b."
By default, ar
will detect the number of parameters your function is expecting an enforce it by throwing an error when it is called with a different number of parameters.
Variable Number of Parameters
If you want your function to accept a range of number of parameters (e.g. it can take 2 to 4 parameters), the pass in a min or max value as the 1st and/or 2nd value.
To specify just a minimum number (note that this is in CoffeeScript, it was too long to write in JS):
sum = ar 2 numsreduce t + ssum15 # Returns 6 sum2 # Throws "Wrong number of parameters. Excpected 2 or more, got 1."
To specify a range:
sum = ar 24 numsreduce t + ssum15 # Returns 6 sum2 # Throws "Wrong number of parameters. Excpected 2..4, got 1." sum25279 # Throws "Wrong number of parameters. Excpected 2..4, got 5."
Specifying Parameter Types
If you want, you can easily enforce the type of variables that are passed into your function.
Just pass in the types as strings into ar
before passing in your function:
sum = ar "number""number" a + bsum1'2' # Throws "Invalid parameter. Expected parameter 1 to be of type 'Number' but got 'String'."
In addition to supporting native JavaScript types like "number", "string", "boolean", "function", and "object", it also supports user defined types/classes).
Example in JavaScript:
{ thisname = name;} { thisname = name;} var lannisterMotto = ; ; // Returns "Tyrion always pays his debts."; // Throws "Invalid parameter. Expected parameter 0 to be of type 'Lannister' but got 'Stark'."
Example in CoffeeScript:
: lannisterMotto = ar "Lannister" " always pays his debts." lannisterMotto "Tyrion" ; # Returns "Tyrion always pays his debts." lannisterMotto "Ned" ; # "Throws Invalid parameter. Expected parameter 0 to be of type 'Lannister' but got 'Stark'."
Enforcing object structure
In JS, you'll often have a bunch of parameters passed in via an object literal. The contents of these can also be checked for type.
Example in CoffeeScript:
# Example 1 magicNumSchema = i: "number"r: "number"# iSum is a function that takes two imaginary numbers and sums them iSum = ar magicNumSchemamagicNumSchema i: ai + bir: ar + brmagic1 = i: 5r: 3magic2 = i: 2r: '7'iSummagic1magic2 # Throws an exception since 'magic[r]' is of the wrong type # Example 2 magic1 = i: 5r: 3comment: "This will be ignored since it isn't in magicNumSchema"magic2 = i: 2r: 7iSummagic1magic2 # Returns { i: 7, r: 10 }
Wildcard
If you want to specify the type that some parameters should be, but don't care about others, you can specify them by passing in "*"
.
# Assign key/value if it doesn't exist = objkey= value if key not of objsafeInserter = ar "object""string""*"safeInserter person = name: "Jake" safeInserterperson"name""Fran" # Does nothing safeInserterperson"age"30 # Inserts a new key and value
Installing for Node.js
From the command-line:
npm install arity
From your app:
var ar = require("arity");
Installing for the browser
Download arity.js
and put it in your js
folder with your project. Then include it like so:
<script src="js/arity.js"></script>
You now have access to the ar
function.
Tests
npm install # Installs mocha
make test
Credit
Created by Jon Abrams.
Inspired by the rethinkDB's implementation.
I found out about it from this blog post written by Andrew Berls.
License
The MIT License (MIT)
Copyright (c) 2013 Jonathan Abrams
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.