3.4.0 • Published 3 years ago

jshiki v3.4.0

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

j式 | jshiki

Build Status

Lightweight expression parsing and execution library for Node.js

Basic Usage

$ npm install --save jshiki
const jshiki = require('jshiki')

var expression = jshiki.parse('5 + (12 / 3)')
var result = expression.eval() // 9

var expressionText = '" Hello! ".trim() + " My name\'s " + name'
var scope = {
  name: 'Azumi'
}
expression = jshiki.parse(expressionText, { scope })
result = expression.eval() // "Hello! My name's Azumi"

Why

Polymer Expressions are great, but there were times when I needed similar functionality but in the backend instead of in the browser.

jshiki provides similar expression handling, but made for use in Node.

Details

jshiki supports:

  • Numeric literals
  • String literals
  • Array literals
  • Object literals
  • Function calls
  • Member access

jshiki lets you pass in a predefined scope object, because expressions do not have access to globals. For example:

expression = jshiki.parse('func(1234)', {
  scope: {
    func: function (num) {
      var comparison = num > 1000 ?
      'greater than' :
      (num === 1000 ?
        'equal to' :
        'smaller than'
      )
      return num + ' is ' + comparison + ' 1000'
    }
  }
})
result = expression.eval() // "1234 is greater than 1000"

jshiki does not interpret assignment, which keeps expressions from overwriting outside data. For example:

expression = jshiki.parse('property.key = "Haha, I overwrote your stuff!"', {
  scope: {
    property: {
      key: "value"
    }
  }
})
result = expression.eval() // throws Error: Unexpected token =

License

MIT.

Portions of the code were adapted from the Polymer Expressions and esprima libraries, which both have their own 3-clause BSD licenses. Copies are provided in the lib directory.

3.4.0

3 years ago

3.3.0

3 years ago

3.2.0

3 years ago

3.1.0

3 years ago

3.0.0

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.1.1

3 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago