dinero-currency-patterns
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDinero.js Currency Patterns
Dinero.js 货币处理模式
Patterns for handling currencies with Dinero.js: type safety, conversions, custom currencies, database storage, and payment service integration.
以下是使用 Dinero.js 处理货币的相关模式:类型安全、换算、自定义币种、数据库存储以及支付服务集成。
When to Apply
适用场景
Reference these guidelines when:
- Converting between currencies with
convert - Defining custom currencies (e.g., cryptocurrencies, loyalty points)
- Looking up currencies dynamically from external input
- Storing monetary values in a database
- Integrating with payment services (Stripe, PayPal, Square)
在以下场景中可参考这些指南:
- 使用 进行币种间换算
convert - 定义自定义币种(例如加密货币、会员积分)
- 从外部输入动态查询币种
- 在数据库中存储货币值
- 集成支付服务(Stripe、PayPal、Square)
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Type Safety | HIGH | |
| 2 | Conversion | HIGH | |
| 3 | Storage | HIGH | |
| 4 | Payment Integration | MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 类型安全 | 高 | |
| 2 | 换算 | 高 | |
| 3 | 存储 | 高 | |
| 4 | 支付集成 | 中 | |
Quick Reference
快速参考
1. Type Safety (HIGH)
1. 类型安全(高优先级)
- - Define custom currencies with
types-as-constfor compile-time safetyas const satisfies - - TypeScript catches currency mismatches in operations at compile time
types-currency-mismatch - - Validate currency codes from external sources at runtime
types-lookup-validation
- - 使用
types-as-const定义自定义币种,实现编译时安全as const satisfies - - TypeScript 会在编译时捕获操作中的币种不匹配问题
types-currency-mismatch - - 在运行时验证来自外部来源的币种代码
types-lookup-validation
2. Conversion (HIGH)
2. 换算(高优先级)
- - Use scaled amounts for fractional exchange rates, not floats
convert-scaled-rates - - Build reusable converter functions with higher-order patterns
convert-reusable
- - 对分数汇率使用缩放后的金额,不要用浮点数
convert-scaled-rates - - 基于高阶模式构建可复用的换算函数
convert-reusable
3. Storage (HIGH)
3. 存储(高优先级)
- - Store amount, currency code, and scale as separate columns
storage-database - - Avoid PostgreSQL's
storage-no-money-typetype for multi-currency applicationsmoney
- - 将金额、币种代码和精度作为单独的列存储
storage-database - - 多币种应用避免使用 PostgreSQL 的
storage-no-money-type类型money
4. Payment Integration (MEDIUM)
4. 支付集成(中优先级)
- - Map Dinero objects to payment service formats with dedicated helpers
payment-services
- - 使用专用辅助函数将 Dinero 对象映射为支付服务支持的格式
payment-services
How to Use
使用方式
Read individual rule files for detailed explanations and code examples:
rules/types-as-const.md
rules/convert-scaled-rates.mdEach rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references
阅读单独的规则文件获取详细说明和代码示例:
rules/types-as-const.md
rules/convert-scaled-rates.md每个规则文件包含:
- 规则重要性的简要说明
- 附带解释的错误代码示例
- 附带解释的正确代码示例
- 额外上下文和参考资料