@algorithm.ts/calculator
TypeScript icon, indicating that this package has built-in type declarations

4.0.0 • Public • Published

A tiny calculator for number arithmetics such as +-*/().

Install

  • npm

    npm install --save @algorithm.ts/calculator
  • yarn

    yarn add @algorithm.ts/calculator

Usage

// Perform integer arithmetics.
import calculator from 'algorithm.ts/calculator'
// or
import { calculator } from 'algorithm.ts/calculator'

// Perform decimal arithmetics.
import { decimalCalculator } from 'algorithm.ts/calculator'

// Perform bigint arithmetics.
import { bigintCalculator } from 'algorithm.ts/calculator'

Examples

  • integer arithmetics

    import calculator from '@algorithm.ts/calculator'
    
    calculator.calculate('3/2')                  // => 1
    calculator.calculate('-2+1')                 // => -1
    calculator.calculate('-2*3 + 2*5*3/6')       // => -1
    calculator.calculate('(1+(4+5+2)-3)+(6+8)')  // => 23
  • decimal arithmetics

    import { decimalCalculator as calculator } from '@algorithm.ts/calculator'
    
    calculator.calculate('3/2')                  // => 1.5
    calculator.calculate('-2+1')                 // => -1
    calculator.calculate('-2*3 + 2*5*3/6')       // => -1
    calculator.calculate('(1+(4+5+2)-3)+(6+8)')  // => 23
  • bigint arithmetics

    import { bigintCalculator as calculator } from '@algorithm.ts/calculator'
    
    calculator.calculate('22222222222222222222222222222 * 3333333333333333333323232')
    // => 74074074074074074073849599999259259259259259259261504n
  • Illegal inputs

    import calculator from '@algorithm.ts/calculator'
    
    calculator.calculate('-2++1')      // => SyntaxError
    calculator.calculate('-2*/23')     // => SyntaxError
    calculator.calculate('1+(4+5+2))') // => SyntaxError
    calculator.calculate('1+(4+5+2')   // => SyntaxError
  • A solution of https://leetcode.com/problems/basic-calculator/

    import calculator from '@algorithm.ts/calculator'
    export function calculate(expression: string): number {
      return calculator.calculate(expression)
    }
  • A solution of https://leetcode.com/problems/basic-calculator-ii/

    import calculator from '@algorithm.ts/calculator'
    export function calculate(expression: string): number {
      return calculator.calculate(expression)
    }

Related

Package Sidebar

Install

npm i @algorithm.ts/calculator

Weekly Downloads

1

Version

4.0.0

License

MIT

Unpacked Size

37.1 kB

Total Files

8

Last publish

Collaborators

  • lemonclown