GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - cnlon/jep: "Javascript Expression Parser" (JEP) is tiny library used to evaluate expression to javascript function

BranchesTags
Open more actions menu

Latest commit

 

History

69 Commits
69 Commits

Folders and files

NameName
Last commit message
Last commit date
.gitignore
.gitignore
 
 
.travis.yml
.travis.yml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
jep.js
jep.js
 
 
jep.test.js
jep.test.js
 
 
package.json
package.json
 
 

Repository files navigation

Javascript Expression Parser

npm version Build Status js-standard-style

简介

JEP (Javascript Expression Parser) 是一个十分小巧的库,用于将 JavaScript 表达式解析为 JavaScript 函数。

优势:

  1. 底层转换使用 Function,比 eval 快。
  2. 使用最近最少使用算法(LRU),缓存解析结果,减少解析次数。
  3. 支持解析至函数字符串,方便预编译。

安装

npm install jep

使用

快速上手

try: CodePen JSFiddle

const jep = new JEP()

const fun = jep.make('a + 2 === 3')
const result = fun({a: 1})

console.log(result)
// true

使用参数

try: CodePen JSFiddle

const jep = new JEP({params: ['$', 'SQUARE_METER']})

const scope = {
  radius: 3,
  square (n) {
    return n * n
  },
  fixed (numObj, num) {
    return numObj.toFixed(num)
  },
}
const SQUARE_METER = 'm²'

const source = 'fixed((Math.PI + square(radius)), 2) + SQUARE_METER'
const result = jep.make(source)(scope, SQUARE_METER)

console.log(result)
// 12.14m²

API

参数

const jep = new JEP({
  cache: 1000,
  scope: '$',
  params: ['$', 'other_param'],
})

cache: Number 类型,jep 内部使用 LRU 缓存解析过的表达式,cache 表示最大缓存数,默认 1000

scope: String 类型,在已解析的表达式或函数中,用于表示 scope 的变量名,默认 '$'

const jep = new JEP()
const parsed = jep.parse('a + b')
console.log(parsed)
// $.a+$.b

params: Array 类型,该数组中每一项都为 String 类型,执行函数时需要依次传入对应的参数。 第一个必须为 scope 对应的变量名。其余变量名,在表达式中可以直接被访问。

const jep = new JEP({
  params: ['$', 'other'],
})
const scope = {a: 1}
const other = {a: 2}
const result = jep.make('a + other.a')(scope, other)
console.log(result)
// 3

方法

parse: 参数为 String 类型的待编译的表达式,返回编译好的 String 类型表达式

const jep = new JEP()
const source = 'a + b'
const expression = jep.parse(source)
console.log(expression)
// $.a+$.b

build: 参数为 String 类型的已编译表达式,返回编译好的 Function (成功) 或 undefined (失败)

const jep = new JEP()
const source = 'a + b'
const expression = jep.parse(source) // $.a+$.b
const fun = jep.build(expression) // 返回函数,类似 function($){return $.a+$.b}
const result = fun({a: 1, b: 2})
console.log(result)
// 3

buildToString: 和 build 类似,参数为 String 类型的已编译表达式,返回的是函数字符串

const jep = new JEP()
const expression = jep.parse('a + b') // $.a+$.b
const funString = jep.buildToString(expression)
console.log(funString)
// function($){return $.a+$.b}

make: 和 build 类似,参数为 String 类型的待编译表达式,返回编译好的 Function (成功) 或 undefined (失败)

const jep = new JEP()
const source = 'a + b'
const fun = jep.make(source) // 返回函数,类似 function($){return $.a+$.b}
const result = fun({a: 1, b: 2})
console.log(result)
// 3

makeToString: 和 make 类似,参数为 String 类型的待编译表达式,返回的是函数字符串

const jep = new JEP()
const funString = jep.makeToString('a + b')
console.log(funString)
// function($){return $.a+$.b}

License

MIT

About

"Javascript Expression Parser" (JEP) is tiny library used to evaluate expression to javascript function

Topics

javascript-expression-parserjep

Resources

Readme
MIT license
Activity

Stars

12 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.