Makes sure that JavaScript functions are called with the expected number and/or types of parameters.
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 (or types) of parameters.
For example:
function sum(a, b) {
return a + b;
}
sum(1, 2); // returns 3
sum(2); // returns NaN, not too helpful eh?
Depending on the situation, it might be preferred that an error is thrown when the wrong number of parameters are passed in.
A common situation where you might want to enforce this is with a JavaScript library. You may want to make sure that your publically exposed functions are being called in the way you expect. Arity can help users of your library catch errors earlier, and save you support headaches.
First, install the library.
Then, it's as simple as wrapping your function with ar
:
var sum = ar(function (a, b) {
return a + b;
});
sum(1, 2); // returns 3
sum(2); // throws "Wrong number of parameters. Excpected 2, got 1. Params: a, b."
It's even easier in CoffeeScript (isn't everything?):
sum = ar (a, b) -> a + b
sum(1, 2) # returns 3
sum(1, 2, 3) # 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.
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, (nums...) -> nums.reduce (t,s) -> t + s
sum(1, 5) # Returns 6
sum(2) # Throws "Wrong number of parameters. Excpected 2 or more, got 1."
To specify a range:
sum = ar 2, 4, (nums...) -> nums.reduce (t,s) -> t + s
sum(1, 5) # Returns 6
sum(2) # Throws "Wrong number of parameters. Excpected 2..4, got 1."
sum(2, 5, 2, 7, 9) # Throws "Wrong number of parameters. Excpected 2..4, got 5."
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, b) -> a + b
sum(1, '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:
function Lannister(name) {
this.name = name;
}
function Stark(name) {
this.name = name;
}
var lannisterMotto = ar("Lannister", function (person) {
return person.name + " always pays his debts.";
});
lannisterMotto( new Lannister("Tyrion") ); // Returns "Tyrion always pays his debts."
lannisterMotto( new Stark("Ned") ); // Throws "Invalid parameter. Expected parameter 0 to be of type 'Lannister' but got 'Stark'."
Example in CoffeeScript:
class House
constructor: (@name) ->
class Lannister extends House
class Stark extends House
lannisterMotto = ar "Lannister", (person) ->
"#{person.name} always pays his debts."
lannisterMotto( new Lannister("Tyrion") ); # Returns "Tyrion always pays his debts."
lannisterMotto( new Stark("Ned") ); # "Throws Invalid parameter. Expected parameter 0 to be of type 'Lannister' but got 'Stark'."
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 magicNumSchema, magicNumSchema, (a,b) -> i: a.i + b.i, r: a.r + b.r
magic1 = i: 5, r: 3
magic2 = i: 2, r: '7'
iSum(magic1, magic2) # Throws an exception since 'magic[r]' is of the wrong type
# Example 2
magic1 = i: 5, r: 3, comment: "This will be ignored since it isn't in magicNumSchema"
magic2 = i: 2, r: 7
iSum(magic1, magic2) # Returns { i: 7, r: 10 }
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
safeInserter = (obj, key, value) ->
obj[key] = value if key not of obj
safeInserter = ar "object", "string", "*", safeInserter
person = { name: "Jake" }
safeInserter(person, "name", "Fran") # Does nothing
safeInserter(person, "age", 30) # Inserts a new key and value
From the command-line:
npm install arity
From your app:
var ar = require("arity");
Download arity.js
and put it in your js
folder with your project.
You can also install it using bower: bower install arity
Then include it in your page like so:
<script src="js/arity.js"></script>
You now have access to the ar
function.
npm install # Installs mocha
make test
Created by Jon Abrams.
Inspired by the rethinkDB's implementation.
I found out about it from this blog post written by Andrew Berls.
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.