Last active
August 29, 2015 14:25
-
-
Save fakenickels/0243188ba81c75936eeb to your computer and use it in GitHub Desktop.
Schema for arguments, say goodbye to if's crazyness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ArgumentsSchema = function(args, schema, extra){ | |
var orderedArgs = {}; | |
for(var arg in args){ | |
for(var field in schema){ | |
if(schema.hasOwnProperty(field)){ | |
if(args[arg].constructor === schema[field]){ | |
orderedArgs[field] = args[arg]; | |
} | |
} | |
} | |
} | |
return orderedArgs; | |
} | |
function msgNFn(){ | |
var args = ArgumentsSchema(arguments, {msg: String, fn: Function}); | |
args.fn(args.msg); | |
} | |
msgNFn(console.log.bind(console), "hey") | |
msgNFn("hey", console.log.bind(console)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment