Skip to content

Commit

Permalink
* first release of module
Browse files Browse the repository at this point in the history
  • Loading branch information
seyd committed Mar 13, 2017
1 parent 3a13be1 commit a77d07c
Show file tree
Hide file tree
Showing 11 changed files with 838 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vscode
node_modules
coverage
38 changes: 21 additions & 17 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
MIT License
BSD 2-Clause License

Copyright (c) 2017 Johnny Seyd
Copyright (c) 2017, Johnny Seyd
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
136 changes: 135 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,136 @@

# function-from-file
Node module for extracting function from given javascript file (perfect for unit testing).
![platform-image] ![license-image] ![tested-image] ![coverage-image]

Node.js module for extracting function from given javascript file (perfect 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

```js
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*`
```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.
```js
var fns = getFunction('./foo/bar.js', ['foo', 'bar']);
fns.foo();
fns.bar();
```

### Async
As a last parameter use a callback function:
```js
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):
```js
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. Example:
*foo/bar.js*
```js
var koef = 8;
function multiplier(a) {
return a * koef;
}
```
When you extract function (**incorrect**):
```js
var fn = getFunction('./foo/bar.js', 'multiplier');
fn(2);
```
This code throws error: *Uncaught ReferenceError: koef is not defined*.

**Correct** solution is to mock (define localy) all the dependencies:
```js
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:
```js
var getFunction = require("function-from-file");
getFunction.disableCache();
```

**Enable cache**
To turn the caching on call the:
```js
var getFunction = require("function-from-file");
getFunction.enableCache();
```

**Clear cache**
To clear the cache call the:
```js
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](https://github.com/seyd) <[johnnyseyd@gmail.com](mailto:johnnyseyd@gmail.com)>

## Credits
Build on the module [function-extractor](https://github.com/gjtorikian/function-extractor), based on [esprima](https://www.npmjs.com/package/esprima) and other included submodules.

## License

[BSD](LICENSE)

[platform-image]: https://img.shields.io/badge/platform-Node.js-green.svg
[license-image]: https://img.shields.io/badge/license-MIT-orange.svg
[tested-image]: https://img.shields.io/badge/tested-well-yellow.svg
[coverage-image]: https://img.shields.io/badge/coverage-100%25-blue.svg
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*!
* function-from-file
* Copyright(c) 2017 Johnny Seyd <johnnyseyd@gmail.com>
* MIT Licensed
*/

'use strict';

module.exports = require('./lib/function-from-file');
Loading

0 comments on commit a77d07c

Please sign in to comment.