utils-eval

1.0.1 • Public • Published

eval

NPM version Build Status Coverage Status Dependencies

Alias for eval global.

Installation

$ npm install utils-eval

Usage

var evil = require( 'utils-eval' );

evil( str )

Alias for eval global.

var val = evil( '5*4*3*2*1' );
// returns 120 

Notes

  • A reference to 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.

Examples

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,

$ node ./examples/index.js

CLI

Installation

To use the module as a general utility, install the module globally

$ npm install -g utils-eval

Usage

Usage: jseval [options] code
 
Options:
 
  -h,    --help                Print this message.
  -V,    --version             Print the package version.

Examples

$ jseval '5*4*3*2*1'
# => 120

Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

Browser Support

This repository uses Testling for browser testing. To run the tests in a (headless) local web browser, execute the following command in the top-level application directory:

$ make test-browsers

To view the tests in a local web browser,

$ make view-browser-tests

License

MIT license.

Copyright

Copyright © 2015. Athan Reines.

Package Sidebar

Install

npm i utils-eval

Weekly Downloads

11

Version

1.0.1

License

MIT

Last publish

Collaborators

  • kgryte