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

https://github.com/mrgriefs/calculate-string

Parses a string containing mathematical operators to a number
https://github.com/mrgriefs/calculate-string

javascript numbers parser string-manipulation

Last synced: 9 months ago
JSON representation

Parses a string containing mathematical operators to a number

Awesome Lists containing this project

README

          

Calculate String


Patreon
David
Travis CI
GitHub package.json version
RunKit
RunKit


Parses strings containing mathematical operations.

## Table of Contents

- [Introduction](#introduction)
- [Installation](#installation)
- [Usage](#usage)

## Introduction

### Why?

So you can parse a string without using unsafe evals nor have to spend a detour creating a parser.

## Installation

With npm:

```bash
$ npm install calculate-string
```

With yarn:

```bash
$ yarn add calculate-string
```

## Usage

Try me on [RunKit](https://npm.runkit.com/calculate-string)

```javascript
const calculateString = require('calculate-string')

calculateString('1,000 + 1,000') // String: '2000'
calculateString('(100 + 10) / 10') // String: '11'
BigInt(calculateString('2 ** 64')) // BigInt: 18,446,744,073,709,551,616
BigInt(calculateString('NaN')) // SyntaxError: Cannot convert NaN to a BigInt
Number(calculateString('1e+6 ^ -1e+6')) // Number: -Infinity
calculateString("this won't get parsed")) == 'NaN' // Boolean: true
calculateString('1,000,000 ^ 10') // String: '1e+60'
Number(calculateString('1,000,000 ^ 10')) // Number: 1e+60
```

```html







document.getElementById("result").innerHTML = calculateString(
"100 + 100"
);

```