node-nametag
Examine the calling code.
Installation
$ npm install nametag
Usage
Here's a quick example:
const nametag = ; { return ;} ; // returns "foo"
This module also offers several similar functions to return the calling function's type, file, and line/column numbers.
Get the name of an object's type from within that object:
const nametag = ; { // Stuff goes here} Fooprototype { // FYI, nametag() will evaluate to "bar" at this point // Get type name return nametagtype;}; let foo = ;foo; // returns "Foo"
Get path to file containing the calling function:
// File: "/home/me/file.js" const nametag = ; { return nametag;} ; // returns "/home/me/file.js"
And, for my last trick, line and column numbers:
const nametag = ; nametag; // returns 3nametag; // returns 9
The latter's pretty useless, in my opinion, but why not.
Explanation
This module uses V8's Stack Trace API to look up the name of the calling function. It uses similar methods for the other information. As such, this code will only function under V8-powered JavaScript contexts, which includes Node, Google Chrome, etc.
License
MIT