mcpField
D
dinero-best-practices

dinero-best-practices

github.com/dinerojs开发2364 次安装35 周前更新
代码仓库

关于 dinero-best-practices

dinero-best-practices 是面向 Dinero.js 开发者的金额处理指引技能,涵盖 JavaScript 和 TypeScript 中对象创建、运算精度及导入方式,并按优先级组织规则。

主要功能

  1. 01以货币最小单位创建金额
  2. 02采用安全的金额运算方式
  3. 03选择 number 或 bigint 计算器
  4. 04处理精度与加密货币金额
  5. 05按需导入以支持摇树优化

适用场景

  1. 01将用户或 API 输入转换为金额对象
  2. 02拆分总额或计算百分比
  3. 03处理超出安全整数范围的金额
  4. 04选择匹配的 bigint 货币导入

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/dinerojs/skills --skill dinero-best-practices

安装后在 Claude Code 中输入/dinero-best-practices确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

rules/
_sections.md
arithmetic-allocate-not-divide.md
arithmetic-immutability.md
arithmetic-percentages.md
arithmetic-scaled-amounts.md
creation-from-floats.md
creation-minor-units.md
creation-zero-exponent.md
imports-bigint-currencies.md
imports-tree-shaking.md
precision-bigint.md
precision-crypto.md
precision-trim-scale.md

详细介绍

Dinero.js 最佳实践

使用 Dinero.js 时的核心规则。Dinero.js 是用于安全创建、计算和格式化金额的 JavaScript/TypeScript 库。本指南涵盖 4 个类别的规则,并按影响程度排序。

适用场景

在以下情况下,请参考这些指南:

  • 根据用户输入、API 响应或数据库值创建 Dinero 对象
  • 对金额执行算术运算(加法、拆分、乘法)
  • 在 number 和 bigint 计算器之间进行选择
  • 从 dinero.js、dinero.js/currencies 或 dinero.js/bigint 导入
  • 处理价格、成本、税费或任何财务计算

按优先级划分的规则类别

优先级类别影响前缀
1对象创建严重creation-
2算术运算严重arithmetic-
3精度高precision-
4导入中imports-

快速参考

1. 对象创建(严重)
  • creation-minor-units - 始终以整数形式传入最小货币单位的金额
  • creation-from-floats - 使用辅助函数将浮点输入转换为最小货币单位
  • creation-zero-exponent - 指数为 0 的货币(例如 JPY)直接使用主要货币单位
2. 算术运算(严重)
  • arithmetic-immutability - 所有操作都会返回新对象;请保存返回值
  • arithmetic-allocate-not-divide - 拆分金额时使用 allocate,不要手动除法
  • arithmetic-scaled-amounts - 切勿乘以未经处理的小数;应使用缩放后的金额
  • arithmetic-percentages - 使用 allocate 或缩放后的 multiply 计算百分比
3. 精度(高)
  • precision-bigint - 金额超过 Number.MAX_SAFE_INTEGER 时,使用 dinero.js/bigint
  • precision-crypto - 加密货币因指数较高而需要使用 bigint
  • precision-trim-scale - 链式操作后,使用 trimScale 去除末尾的零
4. 导入(中)
  • imports-tree-shaking - 仅导入所需内容;独立函数支持摇树优化
  • imports-bigint-currencies - 计算器类型须匹配:搭配 dinero.js/bigint 使用 dinero.js/bigint/currencies

使用方法

阅读各条规则文件,了解详细说明和代码示例:

rules/creation-minor-units.md
rules/arithmetic-allocate-not-divide.md

每个规则文件都包含:

  • 简要说明规则的重要性
  • 错误代码示例及其说明
  • 正确代码示例及其说明
  • 其他背景信息和参考资料

评论

支持 Markdown