Giter VIP home page Giter VIP logo

file-eval's Introduction

node-eval

Eval JS-expression, CommonJS modules and JSON with ease.

NPM Status Travis Status Coverage Status Dependency Status

Install

$ npm install --save node-eval

Usage

JS-expression

const nodeEval = require('node-eval');

nodeEval('42 * 42'); // 1764

CommonJS

const nodeEval = require('node-eval');
const moduleContents =
`
    const package = require('./package.json');

    module.exports = {
        name: package.name
    };
`;

nodeEval(moduleContents, './index.js'); // filename need to provide required info to resolve relative paths inside evaluating code

// ➜ { name: 'node-eval' }

JSON

const nodeEval = require('node-eval');
const jsonContents = '{ "name": "node-eval" }';

nodeEval(jsonContents, 'my.json'); // filename need to `node-eval` determinate json format by extention

// ➜ { name: 'node-eval' }

API

nodeEval(contents[, filename, context])

contents

Type: string

The JS-expression, CommonJS module contents or JSON contents.

filename

Type: string

The path to file which contents we execute.

The node-eval determinate format by extension. If filename ends with .json extention, its contents will be parsing with JSON.parse. If filename ends with .js, its contents will be evaluating with vm.

By default expected JS-expression or CommonJS module contents.

const nodeEval = require('node-eval');

nodeEval('42 * 42'/* js by default */); // 1764
nodeEval('42 * 42', 'my.js'); // 1764
nodeEval('{ "name": "node-eval" }', 'my.json'); // '{ name: 'node-eval' }'

To evaluating CommonJS module contents filename is required to resolve relative paths inside evaluating code.

const nodeEval = require('node-eval');
const moduleContents =
`
    const package = require('./package.json'); // to resolve this require need to know the path of current module (./index.js)

    module.exports = {
        name: package.name
    };
`;

nodeEval(moduleContents, './index.js'); // filename need to provide required info to resolve relative paths inside evaluating code

Internally node-eval will resolve passed relative paths using the place it's called (like require do).

It may spend additional processor's time on it, so better to pass in absolute path.

const fs = require('fs');
const nodeEval = require('node-eval');

// For example, current path is "/repos/project/lib/file.js".
const modulePath = '../files/another.js';
const moduleContents = fs.readFileSync(modulePath, 'utf-8');

// '../files/another.js' will be resolved to '/repos/project/files/another.js'
nodeEval(moduleContents, modulePath);

context

Type: Object

The object to provide into execute method.

If context is specified, then module contents will be evaluating with vm.runInNewContext.

If context is not specified, then module contents will be evaluating with vm.runInThisContext.

With context you can provide some like-a-global variables into node-eval.

const nodeEval = require('node-eval');

const secretKey = '^___^';
const contents = 'module.exports = secretKey;';

nodeEval(content, { secretKey }); // '^___^'

Related

file-eval's People

Contributors

blond avatar greenkeeper[bot] avatar greenkeeperio-bot avatar qfox avatar yeti-or avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

file-eval's Issues

An in-range update of sinon is breaking the build 🚨

Version 4.0.2 of sinon was just published.

Branch Build failing 🚨
Dependency sinon
Current Version 4.0.1
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

sinon is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • coverage/coveralls Coverage pending from Coveralls.io Details
  • continuous-integration/travis-ci/push The Travis CI build failed Details

Commits

The new version differs by 21 commits.

  • 39e5737 Update docs/changelog.md and set new release id in docs/_config.yml
  • c2bd7b1 Add release documentation for v4.0.2
  • da24e94 4.0.2
  • cea60e8 Update History.md and AUTHORS for new release
  • d4dcfca Document the stub.reset breaking change (#1590)
  • aab1917 Merge pull request #1596 from mroderick/fix-preversion-to-use-history.md
  • 5dbe685 Update 'nise' to latest version (#1593)
  • 578b9ea Update History.md and AUTHORS for new release
  • 499b698 Fix preversion.sh to stage History.md
  • 1e5fd0f Remove unneeded characters
  • 2a69d51 fix typo in contributing
  • 6cc71ec Document how to trigger progress events (#1585)
  • e49fcf1 Fix broken test-coverage task
  • 527086a Use supports-color module to test if system supports colors
  • 78782b4 Merge pull request #1580 from mroderick/dependency-hygeine

There are 21 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of sinon is breaking the build 🚨

Version 4.1.4 of sinon was just published.

Branch Build failing 🚨
Dependency sinon
Current Version 4.1.3
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

sinon is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Release Notes Minor fix for Symbol names and deprecation of spy.reset
  • Fix: assertion error messages did not handle Symbol names (#1640)
  • Deprecate spy.reset(), use spy.resetHistory() instead (#1446)
Commits

The new version differs by 36 commits.

  • 1ea2749 Update docs/changelog.md and set new release id in docs/_config.yml
  • 078c082 Add release documentation for v4.1.4
  • 571263e 4.1.4
  • f2ee9f1 Update History.md and AUTHORS for new release
  • a8262dd Assertion error messages handle symbolic method names
  • 8fa1e14 Merge pull request #1641 from mroderick/point-to-stack-overflow
  • 7c1ebd0 Update issue links to point to sinonjs/sinon
  • 93418f6 Update documentation to emphasize Stack Overflow
  • ca9e2fa Merge pull request #1636 from fearphage/fix-headless-chrome-in-circle
  • 39186f4 use google-chrome-unstable for tests
  • 6315621 invalidate cache
  • d078af9 try using default chrome install
  • 177c4b6 upgraded to the lastest official version of mochify
  • ecdc4e0 test with updated mochify
  • 360c2e7 added more details and notes

There are 36 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of eslint is breaking the build 🚨

Version 4.18.0 of eslint was just published.

Branch Build failing 🚨
Dependency eslint
Current Version 4.17.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

eslint is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push The Travis CI build failed Details
  • coverage/coveralls First build on greenkeeper/eslint-4.18.0 at 100.0% Details

Commits

The new version differs by 14 commits.

  • 883a2a2 4.18.0
  • 89d55ca Build: changelog update for 4.18.0
  • 70f22f3 Chore: Apply memoization to config creation within glob utils (#9944)
  • 0e4ae22 Update: fix indent bug with binary operators/ignoredNodes (fixes #9882) (#9951)
  • 47ac478 Update: add named imports and exports for object-curly-newline (#9876)
  • e8efdd0 Fix: support Rest/Spread Properties (fixes #9885) (#9943)
  • f012b8c Fix: support Async iteration (fixes #9891) (#9957)
  • 74fa253 Docs: Clarify no-mixed-operators options (fixes #9962) (#9964)
  • 426868f Docs: clean up key-spacing docs (fixes #9900) (#9963)
  • 4a6f22e Update: support eslint-disable-* block comments (fixes #8781) (#9745)
  • 777283b Docs: Propose fix typo for function (#9965)
  • bf3d494 Docs: Fix typo in max-len ignorePattern example. (#9956)
  • d64fbb4 Docs: fix typo in prefer-destructuring.md example (#9930)
  • f8d343f Chore: Fix default issue template (#9946)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of nyc is breaking the build 🚨

Version 11.7.1 of nyc was just published.

Branch Build failing 🚨
Dependency nyc
Current Version 11.7.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

nyc is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push The Travis CI build failed Details
  • coverage/coveralls First build on greenkeeper/nyc-11.7.1 at 100.0% Details

Commits

The new version differs by 2 commits.

  • 5e40c7c chore(release): 11.7.1
  • 5c0adb5 chore: explicit upgrade of istanbul-reports (#816)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.