Skip to content

Instantly share code, notes, and snippets.

@robozevel
Created November 16, 2012 18:37
Show Gist options
  • Save robozevel/4089740 to your computer and use it in GitHub Desktop.
Save robozevel/4089740 to your computer and use it in GitHub Desktop.
underscore.js mixin to create an arguments signature
/**
* underscore.js mixin to create an arguments signature
*
* Usage:
* switch(_.signature(arguments)) {
* case "*":
* ...
* case "object":
* case "object *":
* ...
* case "function object":
* ...
* default:
* ...
* }
*/
(function(toString) {
var NULL_TYPE = "*", ARGUMENTS_TYPE = toString.call(arguments);
_.mixin({
type: function(arg, type) {
return arg == null ? NULL_TYPE : (type = toString.call(arg)) && type.substring(8, type.length - 1).toLowerCase();
},
signature: function(args) {
return _(ARGUMENTS_TYPE === toString.call(args) ? args : arguments).map(_.type).join(" ") || NULL_TYPE;
}
});
}(Object.prototype.toString));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment