cjs-expose

cjs-expose private commonJS module functions easily


Keywords
cjs-expose, test, mocha, jasmine, karma, private, function, testing, debugging, accessing, retrieving, rewired, exposed, functions, javascript, methods, tool
License
ISC
Install
npm install cjs-expose@1.2.0

Documentation

cjs-expose Build Status

cjs-expose is a simple utility tool for exposing private variables and functions from within a commonJS module.

installation

npm install --save cjs-expose

or

npm install --save-dev cjs-expose

depending on use case.

usage

    //====== MODULE mymodule.js ======

    const myPrivateVar = 1;


    function myPrivateFunction() {

    }

    let myOtherPrivateFunction = function() {

    }
    
    //==========END MODULE================

    //====== MODULE myothermodule.js ======

    const expose = require('cjs-expose');
    const myAccessor = expose('./mymodule.js');

    console.log(myAccessor('myPrivateFunction'));
    //output: [function myPrivateFunction]

    console.log(myAccessor('myOtherPrivateFunction'));
    //output: [function myOtherPrivateFunction]

    console.log(myAccessor('myPrivateVar'));
    //output: 1

    console.log(myAccessor(['myOtherPrivateFunction']));
    //output: [function myOtherPrivateFunction]

    console.log(['myPrivateFunction','myOtherPrivateFunction'])
    /*
        output: {
            myPrivateFunction:[function myPrivateFunction],
            myOtherPrivateFunction:[function myOtherPrivateFunction]
        }
    */

   //==========END MODULE================