Have ideas to improve npm?Join in the discussion! »

    @piranna/caller-callsite
    TypeScript icon, indicating that this package has built-in type declarations

    4.1.0 • Public • Published

    @piranna/caller-callsite

    Build Status

    Get the callsite of the caller function

    This project is a fork of sindresorhus/caller-callsite with a different, more intuitive algorythm and returned data structure.

    Install

    npm install caller-callsite

    Usage

    // foo.js
    const callerCallsite = require('caller-callsite');
    
    module.exports = () => {
      console.log(callerCallsite().getFileName());
      //=> '/Users/piranna/dev/unicorn/bar.js'
    }
    // bar.js
    const foo = require('./foo');
    foo();

    API

    callerCallsite(options?)

    Returns a callsite object.

    options

    Type: object

    depth

    Type: number
    Default: 0

    The callsite depth, meaning how many levels we follow back on the stack trace.

    For example:

    // foo.js
    const callerCallsite = require('caller-callsite');
    
    module.exports = () => {
      console.log(callerCallsite().getFileName());
      //=> '/Users/piranna/dev/unicorn/foobar.js'
      console.log(callerCallsite({depth: 1}).getFileName());
      //=> '/Users/piranna/dev/unicorn/bar.js'
      console.log(callerCallsite({depth: 2}).getFileName());
      //=> '/Users/piranna/dev/unicorn/foo.js'
    }
    // bar.js
    const foo = require('./foo');
    
    module.exports = () => {
      foo();
    }
    // foobar.js
    const bar = require('./bar');
    bar();

    Install

    npm i @piranna/caller-callsite

    DownloadsWeekly Downloads

    3

    Version

    4.1.0

    License

    MIT

    Unpacked Size

    5.17 kB

    Total Files

    5

    Last publish

    Collaborators

    • avatar