3.0.0 • Published 9 years ago

gep v3.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
9 years ago

General Expression Parser

Build Status npm version js-standard-style

安装

npm install --save gep

bower install --save gep

使用

快速上手

try: CodePen JSFiddle

const gep = new Gep()

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

console.log(result)
// true

使用参数

try: CodePen JSFiddle

const gep = new Gep({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 = gep.make(source)(scope, SQUARE_METER)

console.log(result)
// 12.14m²

API

参数

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

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

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

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

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

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

方法

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

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

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

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

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

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

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

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

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

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

License

MIT

3.0.0

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.3.1

9 years ago

1.3.0

10 years ago

1.2.0

10 years ago

1.1.0

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago