utils-eval
Version:
Alias for eval global.
201 lines (130 loc) • 4.37 kB
Markdown
eval
===
[![NPM version][npm-image]][npm-url] [![Build Status][build-image]][build-url] [![Coverage Status][coverage-image]][coverage-url] [![Dependencies][dependencies-image]][dependencies-url]
> Alias for [`eval`][eval] global.
``` bash
$ npm install utils-eval
```
``` javascript
var evil = require( 'utils-eval' );
```
Alias for [`eval`][eval] global.
``` javascript
var val = evil( '5*4*3*2*1' );
// returns 120
```
* A reference to [`eval`][eval] __is__ treated differently by the compiler. For example, when evaluating code containing block-scoped declarations (e.g., `let`, `const`, `function`, `class`), the compiler may throw an `error` complaining that block-scoped declarations are __not__ yet supported outside of `strict mode`. One possible workaround is to include `"use strict";` in the evaluated code, as done in the example below.
``` javascript
var evil = require( 'utils-eval' );
function compile( ctor ) {
var name;
var str;
name = ctor.match( /^(\w*)Array$/ )[ 1 ];
name = name + 'DataArray';
str = '';
str += '(function create(){';
str += '"use strict";';
str += 'class '+name+' extends '+ctor+'{';
str += 'constructor(x){';
str += 'super(x);';
str += '}';
str += '}';
str += 'return '+name+';';
str += '})();';
return str;
}
var ctors = [
'Int8Array',
'Uint8Array',
'Uint8ClampedArray',
'Int16Array',
'Uint16Array',
'Int32Array',
'Uint32Array',
'Float32Array',
'Float64Array',
'Array'
];
var fcn;
for ( var i = 0; i < ctors.length; i++ ) {
fcn = evil( compile( ctors[i] ) );
console.log( fcn.toString() );
}
```
To run the example code from the top-level application directory,
``` bash
$ node ./examples/index.js
```
---
To use the module as a general utility, install the module globally
``` bash
$ npm install -g utils-eval
```
``` bash
Usage: jseval [options] code
Options:
-h, --help Print this message.
-V, --version Print the package version.
```
``` bash
$ jseval '5*4*3*2*1'
```
---
Unit tests use the [Mocha][mocha] test framework with [Chai][chai] assertions. To run the tests, execute the following command in the top-level application directory:
``` bash
$ make test
```
All new feature development should have corresponding unit tests to validate correct functionality.
This repository uses [Istanbul][istanbul] as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
``` bash
$ make test-cov
```
Istanbul creates a `./reports/coverage` directory. To access an HTML version of the report,
``` bash
$ make view-cov
```
This repository uses [Testling][testling] for browser testing. To run the tests in a (headless) local web browser, execute the following command in the top-level application directory:
``` bash
$ make test-browsers
```
To view the tests in a local web browser,
``` bash
$ make view-browser-tests
```
<!-- [![browser support][browsers-image]][browsers-url] -->
---
## License
[MIT license](http://opensource.org/licenses/MIT).
## Copyright
Copyright © 2015. Athan Reines.
[]: http://img.shields.io/npm/v/utils-eval.svg
[]: https://npmjs.org/package/utils-eval
[]: http://img.shields.io/travis/kgryte/utils-eval/master.svg
[]: https://travis-ci.org/kgryte/utils-eval
[]: https://img.shields.io/codecov/c/github/kgryte/utils-eval/master.svg
[]: https://codecov.io/github/kgryte/utils-eval?branch=master
[]: http://img.shields.io/david/kgryte/utils-eval.svg
[]: https://david-dm.org/kgryte/utils-eval
[]: http://img.shields.io/david/dev/kgryte/utils-eval.svg
[]: https://david-dm.org/dev/kgryte/utils-eval
[]: http://img.shields.io/github/issues/kgryte/utils-eval.svg
[]: https://github.com/kgryte/utils-eval/issues
[]: http://mochajs.org/
[]: http://chaijs.com
[]: https://github.com/gotwarlost/istanbul
[]: https://ci.testling.com
[]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval