{"id":20881286,"url":"https://github.com/peakchen90/decimal-eval","last_synced_at":"2025-05-12T16:32:40.627Z","repository":{"id":96138115,"uuid":"290764109","full_name":"peakchen90/decimal-eval","owner":"peakchen90","description":"A tiny, safe, fast JavaScript library for decimal arithmetic expressions.","archived":false,"fork":false,"pushed_at":"2022-01-17T06:43:29.000Z","size":700,"stargazers_count":32,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-28T14:03:24.545Z","etag":null,"topics":["calculation","decimal","eval","expression-evaluator","javascript","js","math","math-eval","math-evaluator","mathjs"],"latest_commit_sha":null,"homepage":"https://codesandbox.io/s/decimal-eval-demo-2gy0k","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/peakchen90.png","metadata":{"files":{"readme":"README.ZH-CN.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-27T12:02:51.000Z","updated_at":"2024-11-01T09:10:31.000Z","dependencies_parsed_at":"2023-03-13T16:37:41.001Z","dependency_job_id":null,"html_url":"https://github.com/peakchen90/decimal-eval","commit_stats":{"total_commits":98,"total_committers":1,"mean_commits":98.0,"dds":0.0,"last_synced_commit":"e0951640296c534819dcec941bfb14f0a8b7bbe8"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peakchen90%2Fdecimal-eval","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peakchen90%2Fdecimal-eval/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peakchen90%2Fdecimal-eval/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peakchen90%2Fdecimal-eval/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/peakchen90","download_url":"https://codeload.github.com/peakchen90/decimal-eval/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253247684,"owners_count":21877904,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["calculation","decimal","eval","expression-evaluator","javascript","js","math","math-eval","math-evaluator","mathjs"],"created_at":"2024-11-18T07:24:20.990Z","updated_at":"2025-05-12T16:32:40.258Z","avatar_url":"https://github.com/peakchen90.png","language":"TypeScript","readme":"# decimal-eval\n一个小巧、安全、快速的 JavaScript 库，用于计算算术表达式。\n\n[![Build Status](https://www.travis-ci.com/peakchen90/decimal-eval.svg?branch=master)](https://www.travis-ci.com/peakchen90/decimal-eval)\n[![Codecov](https://img.shields.io/codecov/c/github/peakchen90/decimal-eval.svg)](https://codecov.io/gh/peakchen90/decimal-eval)\n[![npm](https://img.shields.io/npm/v/decimal-eval.svg)](https://www.npmjs.com/package/decimal-eval)\n[![BUNDLE SIZE](https://badgen.net/bundlephobia/minzip/decimal-eval)](https://bundlephobia.com/result?p=decimal-eval)\n[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/peakchen90/decimal-eval/blob/master/LICENSE)\n\n[English](./README.md) | 简体中文\n\n\n## 特性\n- :v: 使用 [bignumber.js](https://github.com/MikeMcl/bignumber.js) 自动处理 JavaScript 小数精度，并支持大整数\n- :rocket: 执行快、体积小，压缩后只有 28.7 KB, GZIP 后仅 11.2 KB (无 `bignumber.js` 依赖版本压缩后只有 10.5 KB, GZIP 后仅 3.3 KB)\n- :writing_hand: 非常容易扩展自己的自定义运算符\n- :vulcan_salute: 支持表达式变量占位符\n\n\n## 快速开始\n\n### 安装\n```\n# 使用 npm\nnpm i -S decimal-eval\n\n# 或使用 yarn\nyarn add decimal-eval\n```\n\n### 使用\n支持基本的四则运算 (`+`, `-`, `*`, `/`), 并默认使用 [bignumber.js](https://github.com/MikeMcl/bignumber.js) 自动处理 JavaScript 小数精度，并支持大整数\n\n```js\nimport {evaluate} from 'decimal-eval';\n\nevaluate('0.1 + 0.2') // '0.3'\nevaluate('100 * (0.08 - 0.01)'); // '7'\nevaluate('9007199254740992 + 1'); // '9007199254740993'\nevaluate('1 + abc', { abc: 2 }); // '3'\n```\n\n除了上述基本的运算符，也支持扩展自定义运算符，包括一元运算符和二元运算符。内置运算符的优先级另见[参考文档](#toc-built-in-operators-precedence)。\n\n```js\nimport {evaluate, Parser} from 'decimal-eval';\n\n// 创建一个二元运算符 \"add\", 优先级是 13\nconst addOp = Parser.createBinaryOperator('add', 13, (left, right) =\u003e {\n  return String(Number(left) + Number(right));\n});\n\n// 创建一个一元运算符 \"sin\", 优先级是 16\nconst sinOp = Parser.createUnaryOperator('sin', 16, (value) =\u003e {\n  return String(Math.sin(value));\n});\n\n// 安装自定义运算符\nParser.useOperator(addOp);\nParser.useOperator(sinOp);\n\n// 下面的表达式与: `1 + Math.sin(-2)` 类似\nevaluate('1 add sin -2') // '0.09070257317431829'\n```\n\n\n## API\n### evaluate(expression: string, scope?: Record\u003cstring, number\u003e): number\n解析表达式并计算结果，scope 的字段名有以下限制：\n- 必须由字符 `a-Z` 或者 `A-Z` 开始\n- 只能包含字符：`a-z`、`A-Z`、`0-9` 和 `_`\n\n```js\nimport {evaluate} from 'decimal-eval';\n\nevaluate('1 + 2'); // '3'\nevaluate('1 + abc', { abc: 2 }); // '3'\n```\n\n### Parser\n\n#### new Parser(expression: string).parse(): Node | null\n解析表达式，返回 AST\n\n```js\nimport {Parser} from 'decimal-eval';\n\nconst node = new Parser('1 + 2').parse();\n```\n\n#### new Parser(expression: string).compile(): (scope: Record\u003cstring, number | string\u003e) =\u003e string\n编译并缓存表达式，返回一个方法快速计算\n\n```js\nimport {Parser} from 'decimal-eval';\n\nconst evaluate = new Parser('1 + abc').compile();\nevaluate({ abc: 2 }); // '3'\nevaluate({ abc: 9 }); // '10'\nevaluate({ def: 1 }); // 抛出错误，字段名 `abc` 未初始化\n```\n\n#### Parser.useOperator(operator)\n安装一个运算符，运算符通过 `Parser.createBinaryOperator()` 或 `Parser.createUnaryOperator()` 方法创建\n\n#### Parser.useAdapter(adapter)\n为四则运算 (`+`, `-`, `*`, `/`) 设置计算方法适配器，默认使用 [bignumber.js](https://github.com/MikeMcl/bignumber.js) 计算\n\n```js\nParser.useAdapter({\n  '+': (left, right) =\u003e String(Number(left) + Number(right)),\n  '-': (left, right) =\u003e String(Number(left) - Number(right)),\n  '*': (left, right) =\u003e String(Number(left) * Number(right)),\n  '/': (left, right) =\u003e String(Number(left) / Number(right))\n})\n```\n\n\n## 进阶\n\n### 使用 Pure 包 (无依赖)\n当需要使用自定义方法去处理小数精度问题时，你可以使用 Pure 包，可以减少 60% 的体积，这个包不包含 `bignumber.js`\n\n```js\nimport {evaluate, Parser} from 'decimal-eval/dist/pure';\n\n// 设置自定义计算方法\n// Parser.useAdapter(adapter);\n\n// 默认不会自动处理小数精度问题\nevaluate('0.1 + 0.2'); // '0.30000000000000004'\n\n// 默认不支持大整数\nevaluate('9007199254740992 + 1'); // '9007199254740992'\n```\n\n### 导出 `bignumber.js`\n[bignumber.js](https://github.com/MikeMcl/bignumber.js) 对处理 JavaScript 小数精度问题很有用，而不用重复安装\n\n```js\nimport {BigNumber} from 'decimal-eval';\nconst val = new BigNumber(0.1).plus(0.2);\nconsole.log(String(val)); // '0.3'\n```\n\n### \u003cspan id=\"toc-built-in-operators-precedence\"\u003e内置运算符优先级参考\u003c/span\u003e\n\n|  运算符      | 优先级      |\n|  --------   | ---------- |\n| `... + ...` | 13         |\n| `... - ...` | 13         |\n| `... * ...` | 14         |\n| `... / ...` | 14         |\n| `+ ...`     | 16         |\n| `- ...`     | 16         |\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeakchen90%2Fdecimal-eval","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpeakchen90%2Fdecimal-eval","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeakchen90%2Fdecimal-eval/lists"}