Created
August 31, 2018 20:32
-
-
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.
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
/** | |
* 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