Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@emmetio/math-expression

Package Overview
Dependencies
Maintainers
2
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@emmetio/math-expression

Parse and evaluate simple math expressions

  • 1.0.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.9K
increased by10.9%
Maintainers
2
Weekly downloads
 
Created
Source

Math expression parser and evaluator

Fast and small (less than 2KB minified and gzipped) zero-dependency math expression parser and evaluator.

import evaluate from '@emmetio/math-expression';

console.log(evaluate('2 * (-3 + 5)')); // 4

Motivation

In JavaScript, you can use built-in eval() function to evaluate expressions:

console.log(eval('2 * (-3 + 5)')); // 4

But in most modern JavaScript environments eval() function is considered harmful since it can evaluate any arbitrary JS code. And in some cases (like in Atom editor) it’s disabled by default due to security reasons.

With @emmetio/math-expression module you can safely parse & evaluate basic math expressions in any JS environment.

Extract expression from text

This module is used by Emmet project to evaluate math expression in-place in code source. A default user workflow is to enter math expression somewhere in source code and run action to evaluate it. You can use extract function to extract math expression from given source code, starting at specified position:

import evaluate, { extract } from '@emmetio/math-expression';

const code = 'Here goes math: 2 + 3 foo bar';
const [start, end] = extract(code, 21); // 16, 21
const expr = code.substring(start, end); // 2 + 3
console.log(evaluate(expr)); // 5

Keywords

FAQs

Package last updated on 14 Apr 2020

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc