This project is moving to Codeberg.org.
If you wish to check out the most recent source code, file an issue or contribute to this project, please head to the new repository on Codeberg.org:
https://codeberg.org/lordgizmo/CallerModule
Provides information about the caller of a method and its module
This module provides the ability to determine what module performed the most recent function call. It additionally provides information about the module such as:
- Its name
- Its root-path
- The path to the file that performed the function call
- The CallSite-object of the function call (More about the CallSite class...)
You can install this package using npm:
npm install --save @manuth/caller-module
TypeScript-example:
import { GetCallerModule } from "@manuth/caller-module";
console.log(GetCallerModule().name); // Logs the name of your module.
const { GetCallerModule } = require("@manuth/caller-module");
console.log(GetCallerModule().name);
GetCallerModule([method: function], [level: number])
method
:
The method whose caller is to be determined.
Defaults to the GetCallerModule-method.level
: The number of levels above themethod
whose caller is to be determined.
import { GetCallerModule } from "@manuth/caller-module";
function test
{
test1();
}
function test1()
{
test2();
}
function test2()
{
last();
}
function last()
{
console.log(GetCallerModule().name); // Your module's name.
console.log(GetCallerModule().callSite.getFunctionName()); // last
console.log(GetCallerModule(2).callSite.getFunctionName()); // test2
console.log(GetCallerModule(last).callSite.getFunctionName()); // test2
console.log(GetCallerModule(last, 2).callSite.getFunctionName()); // test1
}