npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

decimal-eval

v0.1.1

Published

A tiny, safe, fast JavaScript library for decimal arithmetic expressions.

Downloads

2,717

Readme

decimal-eval

A tiny, safe, fast JavaScript library for decimal arithmetic expressions.

Build Status Codecov npm BUNDLE SIZE GitHub

English | 简体中文

Features

  • :v: Automatically deal with the JavaScript decimal precision problem by bignumber.js, and supports big number
  • :rocket: Fast and tiny, only 28.7 KB minified and 11.2 KB gzipped (pure only 10.5 KB minified and 3.3 KB gzipped without bignumber.js)
  • :writing_hand: Easy to extend custom operator
  • :vulcan_salute: Supports expression scope variables

Get Started

Installation

# use npm
npm i -S decimal-eval

# or use yarn
yarn add decimal-eval

Usage

Supports the four arithmetic operations (+, -, *, /), and automatically deal with JavaScript decimal precision by bignumber.js, and supports big number.

import {evaluate} from 'decimal-eval';

evaluate('0.1 + 0.2') // '0.3'
evaluate('100 * (0.08 - 0.01)'); // '7'
evaluate('9007199254740992 + 1'); // '9007199254740993'
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} from 'decimal-eval';

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

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

// 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 the arithmetic expression and then calculate the result. The name of scope has the following restrictions:

  • muse be start with a-z or A-Z
  • only includes a-zA-Z0-9 and _
import {evaluate} from 'decimal-eval';

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

Parser

new Parser(expression: string).parse(): Node | null

Parse arithmetic expressions.

import {Parser} from 'decimal-eval';

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

new Parser(expression: string).compile(): (scope: Record<string, number | string>) => string

Compile and cache the arithmetic expression.

import {Parser} from 'decimal-eval';

const evaluate = new Parser('1 + abc').compile();
evaluate({ abc: 2 }); // '3'
evaluate({ abc: 9 }); // '10'
evaluate({ def: 1 }); // throw error, the scope name `abc` is not initialized

Parser.createBinaryOperator(value: string, precedence: number, calc: (left: string, right: string) => string): Operator

Create a binary operator.

Parser.createUnaryOperator(value: string, precedence: number, calc: (value: string) => string): Operator

Create a unary operator.

Parser.useOperator(operator: Operator): void

Install an operator which created by the Parser.createBinaryOperator() or Parser.createUnaryOperator() method.

Parser.useAdapter(adapter)

Set custom calculation adapter methods for four arithmetic (+, -, *, /). bignumber.js is used by default.

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

Advanced

Use pure package (no dependencies)

When using a custom method to deal with the decimal precision problem, you can use a pure package, which can reduce the size by about 60%. It doesn't include bignumber.js.

import {evaluate, Parser} from 'decimal-eval/dist/pure';

// set custom calculation adapter
// Parser.useAdapter(adapter);

// Does not deal with precision by default
evaluate('0.1 + 0.2'); // '0.30000000000000004'

// not supports big number by default
evaluate('9007199254740992 + 1'); // '9007199254740992'

Re-export bignumber.js

Useful for deal JavaScript decimal precision problem without having to install bignumber.js again.

import {BigNumber} from 'decimal-eval';
const val = new BigNumber(0.1).plus(0.2);
console.log(String(val)); // '0.3'

Precedence of built-in operators

The operator precedence according to: MDN operator precedence.

| operator | precedence | | -------- | ---------- | | ... + ... | 13 | | ... - ... | 13 | | ... * ... | 14 | | ... / ... | 14 | | + ... | 16 | | - ... | 16 |