Ceval

online demo;
零依赖,借鉴了 expr-eval 底层实现,重构为更适合表达式运算的 ceval;
No dependence, the achieve basics expr-cval, Refactoring to ceval is more suitable for calculating expressions;
┌───────────────────────────────┐
│ │
│ Destination: lib/index.js │
│ Bundle Size: 21.92 KB │
│ Minified Size: 21.89 KB │
│ Gzipped Size: 6.76 KB │
│ │
└───────────────────────────────┘
usage
npm i ceval -S
introduce

Options
const Parser = require('ceval')
const analysis = new Parser({
endableOperators?: boolean = true;
endableBitNumber?: boolean = true;
allowMemberAccess?: boolean = true;
allowHandleNumberPrecision?: boolean = true;
allowOperatorsCovered?: boolean;
defaultReturnValues?: any = ''
})
API
Parser Instance API
| api | desc | type |
|:----|----|:----:|:----:|
| operatorMap | Operators mapping table, which can be used in preset values overlay operation | Record<string, Function>|
| getSupportOperationMap | The name of the operator method supported by the query can be overridden | (ops: string) => null | Function;|
| parseString | Parsing strings, exposing methods to the outside world | (expression: string, values?: Record<string, any>) => any;|
| getCurrentValues | Get current datapool preset + external + internal declaration | () => Record<string, any> |
| updatePresetValues| Update PresetValues |(values: Record<string, any>) => void|
| updateOptions| Update Option | (Options: Partial) => void|
| getOptions| get Options | () => Readonly|
about Options example test case;
basic
const { parseString } = analysis
Number
parseString('0b01')
parseString('0b11')
parseString('0b010101')
parseString('01')
parseString('077')
parseString('01111')
parseStrin('.1')
parseStrin('33')
parseStrin('100.00')
parseString('0x01')
parseString('0xaf')
parseString('0x9fac')
运算
const obj = `{ a: 1, b: 2, c: 3, d: { e: 4, f: 5}}`
parseString(`1+1`);
parseString(`-1-2-3`);
parseString(`1*2*3`);
parseString(`1/2/4`);
parseString(`undefined || 2`);
parseString(`~-1 || -2 || 3`);
parseString(`-0 == +0`);
parseString(`~1 > 1`);
parseString(`false > false > 1`);
parseString(`5 >= 0`);
parseString(`1 in [1, 2, 3]`);
parseString(`undefined in [1, 2, true]`);
parseString(`'a' in ${obj}`);
parseString(`\'\'a\'\' in ${obj}`);
parseString(`1 === true`);
parseString(`3%2`);
Function
parseString(`
function abs(a,b,c) {
var a = 5; /* => inject to presetValues */
let b = 1; /* => inject to current scope */
c = 2;
const d = 4; /* If the current scope contains the variable D, It will trigger warning, but the operation will still be completed, is overlay */
return(a+b+c);
};
abs(3,4,8);
`)
Object & Array
parseString(`[1*2, false, true, undefined, null]`);
parseString(`var a = { b: { c: ['a','b','c','d']} };'e' in a.b.c`);
parseString(`{ a: 1, b: 2, c: { d: undefined, e: { f: false, g: { h: null }}}}`);
Variable
parseString(`
var a = { a: 2 };
var b = { b: a };
`)
parseString(`
var a = { a: [false, true, undefined, null, ''] };
var b = { b: true, c: undefined, d:{ e: a, f: '1', g: {}}};
`)
Other
更多examples请移步测试用例。
TODO: Test39 部分测试用例
更多功能扩展中,欢迎提出 feature 和 参与。
development
develop
npm start
build
# webapck build umd module, 无压缩
npm run build:umd
# rollup build umd module, 压缩版本
npm run build:rollup
# webpack build docs
npm run build:docs
publish
npm publish