dinero-currency-patterns

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Dinero.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

按优先级划分的规则类别

PriorityCategoryImpactPrefix
1Type SafetyHIGH
types-
2ConversionHIGH
convert-
3StorageHIGH
storage-
4Payment IntegrationMEDIUM
payment-
优先级类别影响程度前缀
1类型安全
types-
2换算
convert-
3存储
storage-
4支付集成
payment-

Quick Reference

快速参考

1. Type Safety (HIGH)

1. 类型安全(高优先级)

  • types-as-const
    - Define custom currencies with
    as const satisfies
    for compile-time safety
  • types-currency-mismatch
    - TypeScript catches currency mismatches in operations at compile time
  • types-lookup-validation
    - Validate currency codes from external sources at runtime
  • types-as-const
    - 使用
    as const satisfies
    定义自定义币种,实现编译时安全
  • types-currency-mismatch
    - TypeScript 会在编译时捕获操作中的币种不匹配问题
  • types-lookup-validation
    - 在运行时验证来自外部来源的币种代码

2. Conversion (HIGH)

2. 换算(高优先级)

  • convert-scaled-rates
    - Use scaled amounts for fractional exchange rates, not floats
  • convert-reusable
    - Build reusable converter functions with higher-order patterns
  • convert-scaled-rates
    - 对分数汇率使用缩放后的金额,不要用浮点数
  • convert-reusable
    - 基于高阶模式构建可复用的换算函数

3. Storage (HIGH)

3. 存储(高优先级)

  • storage-database
    - Store amount, currency code, and scale as separate columns
  • storage-no-money-type
    - Avoid PostgreSQL's
    money
    type for multi-currency applications
  • storage-database
    - 将金额、币种代码和精度作为单独的列存储
  • storage-no-money-type
    - 多币种应用避免使用 PostgreSQL 的
    money
    类型

4. Payment Integration (MEDIUM)

4. 支付集成(中优先级)

  • payment-services
    - Map Dinero objects to payment service formats with dedicated helpers
  • payment-services
    - 使用专用辅助函数将 Dinero 对象映射为支付服务支持的格式

How to Use

使用方式

Read individual rule files for detailed explanations and code examples:
rules/types-as-const.md
rules/convert-scaled-rates.md
Each 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
每个规则文件包含:
  • 规则重要性的简要说明
  • 附带解释的错误代码示例
  • 附带解释的正确代码示例
  • 额外上下文和参考资料