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
- Host: GitHub
- URL: https://github.com/mrgriefs/calculate-string
- Owner: MrGriefs
- License: mit
- Created: 2021-04-27T19:53:56.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2022-12-06T01:02:58.000Z (over 3 years ago)
- Last Synced: 2025-09-22T04:49:09.944Z (9 months ago)
- Topics: javascript, numbers, parser, string-manipulation
- Language: JavaScript
- Homepage:
- Size: 167 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Calculate String
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"
);
```