Created
August 18, 2016 15:30
-
-
Save g-i-o-/ca668f0968c6f57e96cc677205f31b73 to your computer and use it in GitHub Desktop.
Returns the call stack of the function calling this function.
This file contains hidden or 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
| /** Returns the call stack of the function calling this function. | |
| * @return {Array} of function calls up to the function calling this function | |
| */ | |
| function getCallStack(){ | |
| try{ | |
| throw new Error('! ... Snake!, SNAKE!!!'); | |
| } catch(e){ | |
| return (e.stack || '').split('\n').filter(function(_){ | |
| return /^\s*at\s+.+/.test(_); | |
| }).map(function(_){ | |
| return /^\s*at\s+(.+)/.exec(_)[1]; | |
| }).filter(function(_, i){ | |
| return i > 1; // ignore both the error throw, and the call to this function. | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment