Skip to content

Instantly share code, notes, and snippets.

@j2labs
Created September 28, 2011 07:17
Show Gist options
  • Select an option

  • Save j2labs/1247213 to your computer and use it in GitHub Desktop.

Select an option

Save j2labs/1247213 to your computer and use it in GitHub Desktop.

Revisions

  1. James Dennis revised this gist Sep 28, 2011. No changes.
  2. James Dennis created this gist Sep 28, 2011.
    17 changes: 17 additions & 0 deletions arity_check.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    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)
    14 changes: 14 additions & 0 deletions wrap_it.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    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