@hizollo/calculator
v1.1.1
Published
A calculator written in JavaScript.
Downloads
7
Readme
Calculator
用 TypeScript 寫成的簡單計算機。
使用方式
import { Calculator } from "@hizollo/calculator";
const calculator = new Calculator();
const formula = '1 + 2 * 3';
const result = calculator.calculate(formula);
console.log(result); // 7
支援語法
見文件。
錯誤系統
class CalcError extends Error
- 額外屬性
code
(ErrorCodes
):錯誤代碼type
(ErrorTypes
):錯誤類別position
(number
):出錯位置args
(unknown[]
):出錯參數
使用範例
import { Calculator, CalcError, ErrorCodes } from "@hizollo/calculator";
const calculator = new Calculator();
const formula = 'random thing';
try {
const result = calculator.calculate(formula);
console.log(result);
} catch (e) {
const error = e as CalcError<ErrorCodes>;
console.log(`${error.message}\nAt: ${error.position}`);
}