Skip to content

Instantly share code, notes, and snippets.

@ruzz311
Created August 31, 2018 20:32
Show Gist options
  • Save ruzz311/77e8315bfaa8249e5718790bc7f4bcb7 to your computer and use it in GitHub Desktop.
Save ruzz311/77e8315bfaa8249e5718790bc7f4bcb7 to your computer and use it in GitHub Desktop.
call this function to get a string of method params with their values.
/**
* Print the names and values for arguments of the supplied method. The example
* should create a log containing the line: ```[foo:"bar", spam:"eggs"]```
* ```JavaScript
* function myFunction(foo, spam){
* _argsToString(...arguments)
* }
* myFunction("bar", "eggs")
* ```
* @param {...*} - apply all arguments to your calling method
* @return {*}
* @private
*/
function _argsToString() {
const args = arguments;
const results = (args.callee.caller || 'function(){}').toString()
.match(/function\s.*?\(([^)]*)\)/)[1]
.split(',')
// remove inline comments / trim whitespace.
.map(arg => arg.replace(/\/\*.*\*\//, '').trim())
// remove undefined values.
.filter(arg => arg)
// create string with argName:argValue
.map((name, i) => `${name}:${args[i]}`);
return `[${results}]`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment