Created
February 12, 2024 13:13
-
-
Save sasin91/c261bae11a065b0b93d88f2a4a58ae67 to your computer and use it in GitHub Desktop.
getCalleeFromStackTrace.js
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
export const getCalleeFromStackTrace = () => { | |
const e = new Error(); | |
const frame = e.stack.split(`\n`)[2]; // change to 3 for grandparent func | |
// const lineNumber = frame.split(`:`).reverse()[1]; | |
// const functionName = frame.split(` `)[5]; | |
const regex = /(\S+)\s+\(([^:]+):(\d+):(\d+)\)/; | |
const match = frame.trim().match(regex); | |
if (!match) { | |
return null; | |
} | |
const [, functionName, fileName, lineNumber] = match; | |
return { | |
functionName, | |
fileName, | |
lineNumber: parseInt(lineNumber) | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment