npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

function-from-file

v1.0.2

Published

Get one or more functions from given file. Extract also private functions, sync or async. Great for unit testing.

Downloads

8

Readme

function-from-file

platform-image license-image tested-image coverage-image  npm-version-image

Node.js module for extracting function from given javascript file (great for unit testing).

  • both sync or async options
  • reach private functions also
  • simple way of mocking inner dependecies

One condition only: "function must have a name".

Keywords: functions, private function, unit tests, unittest, testing, get function, extract function, parse function, find function.

Example

var getFunction = require("function-from-file");
var privateFunction = getFunction('./foo/bar.js', 'someFunction');

// you can use imported function now
privateFunction();

Motivation

When writing unit tests, private functions inside your module are not accessible from outside (via require statement). With this tool you can get and test any function by its name.

Installation

Use --save-dev parameter if you are going use this module just for development (e.g. testing) or --save if your application is using it.

$ npm install function-from-file --save-dev

Features / API

Sync

This example extracts a function addNumbers from script ./foo/bar.js

var add = getFunction('./foo/bar.js', 'addNumbers');
var result = add(100, 50);

Get more functions at once

If array of function names is given, returns an object where keys are those functions.

var fns = getFunction('./foo/bar.js', ['foo', 'bar']);
fns.foo();
fns.bar();

Async

To get a function asynchronous add the last parameter as a callback function:

getFunction('./foo/bar.js', 'addNumbers', function(err, result) {
    if (err) throw err;
    console.log( result(100, 50) );
});

It works with more function names also (like the sync variant):

getFunction('./foo/bar.js', ['foo', 'bar'], function(err, result) {
    if (err) throw err;
    result.foo();
    result.bar();
});

Mock dependecies

Sometimes you extract function that has some dependencies on other functions or variables. See this example:

// File foo/bar.js

var koef = 8;
function multiplier(a) {
    return a * koef;
}

When you extract function (not working solution):

var fn = getFunction('./foo/bar.js', 'multiplier');
fn(2);

This code throws error: Uncaught ReferenceError: koef is not defined.

Working solution is to mock (define localy) all the dependencies:

var fn = getFunction('./foo/bar.js', 'multiplier');
var koef = 8;
eval('var mockedFn = '+fn);
mockedFn(4);

The function fn in eval statement was converted to string so it evals:

eval("var mockedFn = function multiplier(a) { return a * koef; }");

Now it uses the local variable koef.

Caching

The most expensive operation is reading a file and parsing a source code and looking for a functions. So this is why caching is implemented. When you are getting function from the same file, cached result is used. Caching is default turned on, but you can turn it off.

Disable cache

To turn the caching off call the:

var getFunction = require("function-from-file");
getFunction.disableCache();

Enable cache

To turn the caching on call the:

var getFunction = require("function-from-file");
getFunction.enableCache();

Clear cache

To clear the cache call the:

var getFunction = require("function-from-file");
getFunction.clearCache();

Tests

To run the tests, at first install the dependencies: $ npm install

Running unit tests: $ npm test

Running coverage tests: $ npm run test-cov

Author

Johnny Seyd at GitHub <[email protected]>

Credits

Build on the module function-extractor, based on esprima and other included submodules.

License

BSD