关于 dinero-best-practices
dinero-best-practices 是面向 Dinero.js 开发者的金额处理指引技能,涵盖 JavaScript 和 TypeScript 中对象创建、运算精度及导入方式,并按优先级组织规则。
主要功能
- 01以货币最小单位创建金额
- 02采用安全的金额运算方式
- 03选择 number 或 bigint 计算器
- 04处理精度与加密货币金额
- 05按需导入以支持摇树优化
适用场景
- 01将用户或 API 输入转换为金额对象
- 02拆分总额或计算百分比
- 03处理超出安全整数范围的金额
- 04选择匹配的 bigint 货币导入
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ dinerojs/ skills --skill dinero-best-practices安装后在 Claude Code 中输入/dinero-best-practices确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
rules/_sections.mdarithmetic-allocate-not-divide.mdarithmetic-immutability.mdarithmetic-percentages.mdarithmetic-scaled-amounts.mdcreation-from-floats.mdcreation-minor-units.mdcreation-zero-exponent.mdimports-bigint-currencies.mdimports-tree-shaking.mdprecision-bigint.mdprecision-crypto.mdprecision-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/bigintprecision-crypto- 加密货币因指数较高而需要使用 bigintprecision-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