0.1.0 • Published 8 years ago

nametag v0.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

node-nametag

npm version Build Status Dependency Status

Examine the calling code.

Installation

$ npm install nametag

Usage

Here's a quick example:

const nametag = require("nametag");

function foo() {
    return nametag();
}

foo(); // 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 = require("nametag");

function Foo() {
    // Stuff goes here
}

Foo.prototype.bar = function() {
    // FYI, nametag() will evaluate to "bar" at this point

    // Get type name
    return nametag.type();
};

let foo = new Foo();
foo.bar(); // returns "Foo"

Get path to file containing the calling function:

// File: "/home/me/file.js"

const nametag = require("nametag");

function foo() {
    return nametag.file();
}

foo(); // returns "/home/me/file.js"

And, for my last trick, line and column numbers:

const nametag = require("nametag");

nametag.line(); // returns 3
nametag.col(); // 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