New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

decimal-eval

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

decimal-eval

A small, safe, fast JavaScript library for parsing decimal arithmetic.

Source
npmnpm
Version
0.0.1-rc.2
Version published
Weekly downloads
1.9K
26.49%
Maintainers
1
Weekly downloads
 
Created
Source

decimal-eval

A small, safe, fast JavaScript library for parsing decimal arithmetic.

Travis (.org) branch Codecov npm GitHub

Get Started

Installation

# use npm
npm i -S decimal-eval

# or use yarn
yarn add decimal-eval

Usage

Support the four arithmetic operations of addition, subtraction, multiplication and division, and automatically fix JS decimal precision by big.js.

import {evaluate} from 'decimal-eval';

evaluate('0.1 + 0.2') // 0.3
evaluate('100 * (0.08 - 0.01)'); // 7
evaluate('1 + abc', { abc: 2 }); // 3

In addition to the above operators, it also supports custom operator expansion, and supports unary operators and binary operators. The operator precedence according to: MDN operator precedence.

import {evaluate, Parser, Operator} from 'decimal-eval';

// create binary operator `add`, the precedence is 13
const addOp = Operator.create('add', 13, (left, right) => {
  return left + right;
});

// create unary operator `sin`, the precedence is 16
const sinOp = Operator.create('sin', 16, (value) => {
  return Math.sin(value);
}, true);

// install custom operators
Parser.useOperator(addOp);
Parser.useOperator(sinOp);

// same as: `1 + Math.sin(-2)`
evaluate('1 add sin -2') // 0.09070257317431829

API

evaluate(expression: string, scope?: Record<string, number>): number

Parse and calculate arithmetic expression.

import {evaluate} from 'decimal-eval';

evaluate('1 + 2'); // 3
evaluate('1 + abc', { abc: 2 }); // 3

Operator

Operator.create(value: string, precedence: number, calc: Function, isPrefix = false)

import {Operator} from 'decimal-eval';
// create operator `%`, which is a binary operator, the calc should like: `(left: number, right: number) => number`
const modOp = Operator.create('%', 15, (left, right) => left % right);

// `isPrefix` is true, that is a unary operator, the calc should like: `(value: number) => number`
const absOp = Operator.create('abs', 16, (value) => Math.abs(value), true);

Parser

new Parser(expression: string).parse(): AST

Parse arithmetic expressions.

import {Parser} from 'decimal-eval';

const ast = new Parser('1 + 2').parse();

new Parser(expression: string).compile(): (scope) => number

Compile and cache expression.

import {Parser} from 'decimal-eval';

const evaluate = new Parser('1 + abc');
evaluate({ abc: 2 }); // 3
evaluate({ abc: 9 }); // 10
evaluate({ def: 1 }); // throw error

Parser.useOperator(operator)

Install an operator, which created by the Operator.create() method.

Parser.useAdapter(adapter)

Custom setting calculation adapter method to four arithmetic (+, -, *, /). Big.js is used by default.

Parser.useAdapter({
  '+': (left, right) => left + right,
  '-': (left, right) => left - right,
  '*': (left, right) => left * right,
  '/': (left, right) => left / right
})

Parser.evaluate(expression: string): number

Alias of evaluate(expression: string) method.

Advanced

Keywords

eval

FAQs

Package last updated on 01 Sep 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