Skip to content

Instantly share code, notes, and snippets.

@copenhas
Created December 19, 2012 15:21
Show Gist options
  • Save copenhas/4337456 to your computer and use it in GitHub Desktop.
Save copenhas/4337456 to your computer and use it in GitHub Desktop.
function builder library idea
// requires at least 1 object parameter and optionally a function callback
var foo = sani(['object+|array', 'function?'], function (objs, callback) {
if (callback) callback();
return objs.length;
});
foo(1, 2);
// throws error
foo({}, {});
// 2
foo();
// throws error
var cb = function () { console.log('hello'); };
foo([{}], cb);
// 1
// LOG: 'hello'
foo(cb);
// throws error
foo.overload(['function'], function (callback) {
console.log('overloaded!')
if (callback) callback();
});
foo(cb);
// LOG: 'overloaded!
// LOG: 'hello'
//optional many numbers and requires a function callback
var bar = sani(['number*', 'function'], function (objs, callback) {
console.log(objs.length);
callback();
});
bar(cb);
// LOG: 0
// LOG: 'hello'
bar();
// throws error
bar(1, 2, 3, cb);
// LOG: 3
// LOG: 'hello'
// arg spec didn't specific it could also be an array
bar([1, 2, 3], cb);
// throws error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment