Skip to content

Instantly share code, notes, and snippets.

@vlastachu
Created July 12, 2014 20:57
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 vlastachu/4eba1e81c6de9caafa6b to your computer and use it in GitHub Desktop.
Save vlastachu/4eba1e81c6de9caafa6b to your computer and use it in GitHub Desktop.
// from https://news.ycombinator.com/item?id=8024116
function checkType(f) {
return function(a) {
var type = f.toString().match(/\/\/(.*)\n/)[1].trim();
if(type !== typeof(a)) throw new Error('Invalid type');
return f(a);
}
}
var halve = checkType(function(n) {
// number
return n / 2;
});
halve(4); // returns 2
halve('string'); // emits error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment