@vstiff/config-evaluator

0.4.0 • Public • Published

Config Evaluator

Simple module to safely evaluate javascript-like expressions. Useful for user-editable little pieces of dynamic logic stored in database.

Installation

npm install @vstiff/config-evaluator

Usage

evcon(expr, ctx) will return result of evaluation of given expression.

Example

const { evcon } = require('@vstiff/config-evaluator');

const expr = `{
   ingredients: [
     { name: 'sugar', amount: sugar },
     { name: 'water', amount: 100 - coffeeratio * 10 },
     { name: 'coffee', amount: 100 + coffeeratio * 30 },
     { name: 'milk', amount: 50, ignore: withmilk == 0 }
   ]
 }`;

const ctx = {
 sugar: 3,
 coffeeratio: 2,
 withmilk: 0
};

console.log(evcon(expr, ctx))

// {
//   ingredients: [
//     { name: 'sugar', amount: 3 },
//     { name: 'water', amount: 80 },
//     { name: 'coffee', amount: 160 },
//     { name: 'milk', amount: 50, ignore: true }
//   ]
// }

Readme

Keywords

none

Package Sidebar

Install

npm i @vstiff/config-evaluator

Weekly Downloads

10

Version

0.4.0

License

MIT

Unpacked Size

4.68 kB

Total Files

5

Last publish

Collaborators

  • vstiff