# string-expression

> Library for evaluating simple string expressions

Latest version **0.0.6** (published 2015-10-16) · Apache 2 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install string-expression
pnpm add string-expression
yarn add string-expression
bun add string-expression
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2015-10-16 |
| First published | 2015-09-01 |
| Weekly downloads | 0 |
| License | Apache 2 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Lasana Murray |
| Maintainers | metasansana |
| Keywords | string, eval, expression |

## Links

- npm: https://www.npmjs.com/package/string-expression
- npm.io page: https://npm.io/package/string-expression

## Dependencies (1)

- [lodash.startswith](https://npm.io/package/lodash.startswith.md) ^3.0.1

## Alternatives

- [@mce/gif](https://npm.io/package/@mce/gif.md) — 2.6K weekly downloads
- [cleanse](https://npm.io/package/cleanse.md) — 173 weekly downloads
- [str](https://npm.io/package/str.md) — 127 weekly downloads
- [naming](https://npm.io/package/naming.md) — 95 weekly downloads
- [tap-telco-api](https://npm.io/package/tap-telco-api.md) — 19 weekly downloads

## Recent versions

- 0.0.6 (latest) — 2015-10-16
- 0.0.5 — 2015-10-08
- 0.0.4 — 2015-09-23
- 0.0.2 — 2015-09-01

## README

# string-expression

Library for evaluating simple string expressions

### Motivation

We needed to execute some logic based on some simple JavaScript expressions without using eval etc.

### Usage

This module is meant to be used as part of a browserify build. It's 
written using ES6/ES2015 syntax but is transpiled with babel so that should not be
an issue.

```javascript

import Interpreter from 'string-expression;

var i = new Interpreter();

var context = {
  a: 5,
  b: 5,  
  resolve(key) {    
    return this[key];
  }
};

i.evaluate('a-b', {a:5,b:5}); //returns 0
i.evaluate('a+b', {a:5,b:5}); //returns 10
i.evaluateWithContext('a/b', context); //returns 1

```

`Context` is an interface you can implement to customise how variables
 are resolved. It has a single method `resolve(key)`.
 
#### Supported Syntax
 
 **Unary Operations**
  
  example:
   `!a` or `2`
  
  syntax:
  `OPERAND` or prefix operator to `OPERAND`
  
   where: 
   OPERAND can be a variable name or a primitive (string or number), it can be prefixed with `!`
 
 **Binary Operations**
 
 example:
 `a+b`
 
 syntax:
 `LEFT_OPERAND OPERATOR RIGHT_OPERAND`
  
   where:
   LEFT_OPERAND and RIGHT_OPERAND can be both be a variable name or a primitive (string or number)
   OPERATOR is any of `==,%,<,<=,>,>=,/,*,+,-`, note `==` and `!=` uses strict checks.   
 
 **Ternary Operations**
 
 example: 
 `(a) ? b : a`
  
 syntax:
 `EXPRESSION ? OPERAND : OPERAND`

 where:
 You get the point
 
 
### Notes
 
 Whitespace is important, keep one space between operators an operands except for unary operations.
 Where a variable is not resolved an empty string is returned.
   
 
### License

Apache 2

---
_Source: https://npm.io/package/string-expression · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
