Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

一个字符串表达式解析和求解运行库

License

Notifications You must be signed in to change notification settings

oi-contrib/evalExpress

Repository files navigation

一个字符串表达式解析和求解运行库

downloads npm GitHub repo stars

NPM

如何使用?

npm install --save eval-express

然后就可以引入需要的方法:

 import { evalExpress, getValue, setValue } from "eval-express";

evalExpress

解析对象上字符串表达式:

var value=evalExpress(target, express, scope = {});

在指定对象target上求解表达式express的值,一个可选参数scope表示,如果scope有值,会拦截target,下同。

比如 现在有个json:

var json = {
 "a": {
 "b": [1, 2, 3]
 }
};

那么执行下面的语句:

var result = evalExpress(json, 'a.b[0]-10');

结果result值就是:-9

getValue

获取对象上字符串表达式对应的值:

var value=getValue(target, express, scope = {});

比如,现在有个json:

var json = {
 "a": {
 "b": [1, { "d": "value" }, 3, 4]
 }
};

那么执行下面的语句:

var result = getValue(json, '["a"].b');

结果result值就是:[1, { "d": "value" }, 3, 4]

setValue

设置对象上字符串表达式对应的值:

var newTarget=setValue(target, express, value, scope = {});

虽然会返回新的值,不过旧的值也同步修改了。

比如,现在有个json:

var json = {
 "key" : [1, 2, 3]
};

那么执行下面的语句:

setValue(json, '.key[1]','newValue');

此时,原来的json的值就变成了:

{
 "key" : [1, "newValue", 3]
}

版权

MIT License

Copyright (c) zxl20070701 走一步,再走一步

About

一个字符串表达式解析和求解运行库

Resources

License

Stars

Watchers

Forks

Sponsor this project

AltStyle によって変換されたページ (->オリジナル) /