Socket
Socket
Sign inDemoInstall

ts-expression-evaluator

Package Overview
Dependencies
1
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ts-expression-evaluator

Context-based expression parse and evaluator.


Version published
Maintainers
1
Install size
1.44 MB
Created

Readme

Source

ts-expression-evaluator

NPM version build status Coverage Status

Context-based expression evaluator.

install

  • npm npm install ts-expression-evaluator
  • yarn yarn add ts-expression-evaluator

Quick start

import evaluate, { registerFunction } from 'ts-expression-evaluator'
const context = {
  id: 10,
  name: 'FED',
  count: 10,
  staffs: [{
    id: 1,
    name: 'Tina'
  }, {
    id: 2,
    name: 'James'
  }],
}

// math
evaluate('1+2') // 3

// eval from context
evaluate('name', context) // 'FED'
evaluate('staffs[0].name', context) // 'Tina'
evaluate('count + 10', context) // 20

// ===
evaluate('count === 11', context) // false

// register custom function
registerFunction('IF', (condition, ifTrue, ifFalse) => {
  return condition ? ifTrue : ifFalse;
})

evaluate('IF(name === 'FED', 'It\'s FED.', 'It\'s not FED.')') // It's FED.

// array filter
evaluate('staffs[this.id === 1]', context) // [{id: 1, name: 'Tina'}]

Details

Unary Operators

OperationSymbol
Negate!

Binary Operators

OperationSymbol
Add, Concat+
Subtract-
Multiply*
Divide/

Logical Operators

OperationSymbol
Logical AND&&
Logical OR||

Comparisons

ComparisonSymbol
Equal==
Not equal!=
Greater than>
Greater than or equal>=
Less than<
Less than or equal<=

Native Types

TypeExamples
Booleanstrue, false
Strings"Hello "user"", 'Hey there!'
Numerics6, -7.2, 5, -3.14159
Arrays['hello', 'world!']

API

evaluate(exp: string, context: object): any

registerFunction(name: string, func: Function): void

registerFunctions(funcs: {[key: string]: Function}): void

License

MIT

Keywords

FAQs

Last updated on 13 May 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc