An open API service indexing awesome lists of open source software.

https://github.com/helkyle/tiny-eval

A tiny and safe eval written in TypeScript
https://github.com/helkyle/tiny-eval

Last synced: 10 months ago
JSON representation

A tiny and safe eval written in TypeScript

Awesome Lists containing this project

README

          

# tiny-eval

A tiny, safe, and elegant arithmetic expression evaluator for TypeScript/JavaScript

---

## โœจ Features

- **Tiny**: Minimal dependencies, fast and lightweight
- **Safe**: No use of `eval`, parses and evaluates expressions securely
- **TypeScript-first**: Written in TypeScript, with full type definitions

---

## ๐Ÿ“ฆ Installation

```bash
pnpm install tiny-eval
```

---

## ๐Ÿš€ Usage

```ts
import { evaluate } from 'tiny-eval'

console.log(evaluate('1 + 2 * 3')) // 7
console.log(evaluate('(1 + 2) * 3')) // 9
console.log(evaluate('0.1 + 0.2')) // 0.3
```

---

## ๐Ÿงช Examples

```ts
// Basic operations
evaluate('1+2') // 3
evaluate('5-3') // 2
evaluate('4*2') // 8
evaluate('8/2') // 4

// Operator precedence
evaluate('2+3*4') // 14
evaluate('2*3+4') // 10

// Parentheses
evaluate('(2+3)*4') // 20
evaluate('2*(3+4)') // 14
evaluate('((1+2)*3)+4') // 13

// Decimals
evaluate('0.1+0.2') // 0.3
evaluate('1.5*2') // 3

---

## ๐Ÿง‘โ€๐Ÿ’ป Testing

```bash
pnpm test
```

---

## ๐Ÿ“„ License

MIT License ยฉ 2025 HelKyle

---