![Maven Central Adds Sigstore Signature Validation](https://cdn.sanity.io/images/cgdhsj6q/production/7da3bc8a946cfb5df15d7fcf49767faedc72b483-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
function-from-file
Advanced tools
Get one or more functions from given file. Extract also private functions, sync or async. Great for unit testing.
Node.js module for extracting function from given javascript file (great for unit testing).
One condition only: "function must have a name".
Keywords: functions
, private function
, unit tests
, unittest
, testing
, get function
, extract function
, parse function
, find function
.
var getFunction = require("function-from-file");
var privateFunction = getFunction('./foo/bar.js', 'someFunction');
// you can use imported function now
privateFunction();
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.
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
This example extracts a function addNumbers from script ./foo/bar.js
var add = getFunction('./foo/bar.js', 'addNumbers');
var result = add(100, 50);
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();
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();
});
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.
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();
To run the tests, at first install the dependencies: $ npm install
Running unit tests: $ npm test
Running coverage tests: $ npm run test-cov
Johnny Seyd at GitHub <johnnyseyd@gmail.com>
Build on the module function-extractor, based on esprima and other included submodules.
FAQs
Get one or more functions from given file. Extract also private functions, sync or async. Great for unit testing.
We found that function-from-file demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.