Skip to content

Instantly share code, notes, and snippets.

@zeekay
Last active December 17, 2015 17:28
Show Gist options
  • Save zeekay/5645754 to your computer and use it in GitHub Desktop.
Save zeekay/5645754 to your computer and use it in GitHub Desktop.
Get containing function's name.
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()
$ 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