Skip to content

Instantly share code, notes, and snippets.

@gtzilla
Forked from j2labs/arity_check.js
Created September 28, 2011 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gtzilla/1247284 to your computer and use it in GitHub Desktop.
Save gtzilla/1247284 to your computer and use it in GitHub Desktop.
Node.js friendly form of the arity checking decorator for javascript
exports.arity_decorator = function(fun) {
function wrapped() {
if(arguments.length != fun.length)
throw new Error("Y U NO USE RIGHT?!")
fun.apply(this, arguments)
}
return wrapped;
}
exports.foo = function(x, y) {
console.log('X:' + x)
console.log('Y:' + y)
}
exports.foo = exports.arity_decorator(exports.foo)
var arity_check = require("./arity_check");
var bar = function(z) {
console.log('Hi. Z:' + z);
}
bar = arity_check.arity_decorator(bar)
arity_check.foo(3,4) // Works
bar(5) // Works
bar(5,6,7) // Throws error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment