Last active
December 17, 2015 17:28
-
-
Save zeekay/5645754 to your computer and use it in GitHub Desktop.
Get containing function's name.
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
require('source-map-support').install handleUncaughtExceptions: false | |
constructorRegex = /^new / | |
nameRegex = /at (.*) \(/ | |
objectRegex = /^Object\./ | |
log = (message) -> | |
line = (new Error()).stack.split('\n')[2] | |
name = (nameRegex.exec line)[1].replace objectRegex, '' | |
if constructorRegex.test name | |
name = name.replace(constructorRegex, '') + '.constructor' | |
name = name.replace /^new / | |
console.log "[#{name}]", message | |
foo = -> | |
log 'inside foo' | |
obj = | |
bar: -> | |
log 'inside obj.bar' | |
class Baz | |
constructor: -> | |
log 'inside Baz.constructor' | |
qux: -> | |
log 'inside Baz.qux' | |
foo() | |
obj.bar() | |
baz = new Baz() | |
baz.qux() |
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
$ node func-name.js | |
[foo] inside foo | |
[obj.bar] inside obj.bar | |
[Baz.constructor] inside Baz.constructor | |
[Baz.qux] inside Baz.qux |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment