钱 特斯
这个库旨在让TypeScript/JavaScript中的货币值工作更加安全,更容易.
货币价值总是有货币. 它们按货币区分(1个是) 不等于1欧元,每种货币都有不同的属性,影响 如何显示,四舍五入,分析。 得到所有东西可能很困难 如果你是处理钱, 得到它可能很贵 有什么不对劲!
见: http://martinfowler.com/eaaCatalog/money.html (中文(简体) )
安装
# Using npm:
$ npm i @half0wl/money
# Using yarn:
$ yarn add @half0wl/money
导入
// TypeScript
import { Money } from "@half0wl/money"
// JavaScript/CommonJS
'use strict';
const Money = require("@half0wl/money").Money
特征
- 算术(加,减等)和比较(等分,大/小等)
- 不可磨灭. 每个操作返回一个新的
Money对象,属性不可修改 - 地方意识字符串格式化
- 轻量级; 无外部依赖性
- JavaScript 已完全支持. 启用 严格模式 (单位:千美元)
'use strict';)是高度推荐的,但是没有它,这将会是好的 - 完整文档
概念
金钱对象有一个 currency 财务报告和审定财务报表 amount,其中:
- 货币是一种 ISO-4217 标准 α代码,例如:.
USD, (中文(简体) ),JPY, (中文(简体) ),EUR, (中文(简体) ),SGD - 数额以货币最低单位表示,例如5.00美元=500美分
{ currency: USD, amount: 500 }
使用量
完整的 API 文档 Money 类可用 这里。 。 。 .
import { Money } from "@half0wl/money"
/**
* Basic operations
*/
const listing = new Money('USD', 19999) // Default constructor from int
listing.getCurrency() // => USD
listing.getAmount() // => 19999
listing.toLocaleString() // => '$199.99'
const shipping = Money.fromFloat('USD', 15.00) // From float
shipping.getAmount() // => 1500
shipping.toLocaleString() // => '$15.00'
const xmasDiscount = Money.fromString('USD', '2000') // From int-string
xmasDiscount.getAmount() // => 2000
xmasDiscount.toLocaleString() // => '$20.00'
const promoDiscount = Money.fromString('USD', '10.00') // From float-string
promoDiscount.getAmount() // => 1000
promoDiscount.toLocaleString() // => '$10.00'
const subtotal = listing
.add(shipping)
.subtract(xmasDiscount, promoDiscount)
subtotal.getCurrency() // => 'USD'
subtotal.getAmount() // => 18499
subtotal.toLocaleString() // => '$184.99'
/**
* Other arithmetic operations
*/
subtotal.multiply(2.4).toLocaleString() // => '$443.98'
subtotal.divide(8).toLocaleString() // => $23.12
/**
* Comparisons
*/
promoDiscount.equals(promoDiscount) // => false
promoDiscount.greaterThan(promoDiscount) // => true
shipping.greaterThanOrEqual(listing) // => false
shipping.lessThan(listing) // => true
/**
* Formatting
*/
const tenDollars = new Money('USD', 1000)
// Using default locale (`en-US`):
tenDollars.toLocaleString() // => $10.00
// Using different locale:
tenDollars.toLocaleString('fr-CA') // => 10,00 $ US
// JSON
tenDollars.toJSON() // => {
// currency: 'USD',
// amount: 1000
// }
/**
* Immutability
*/
// All operations return a new `Money` object:
const sixEur = new Money('EUR', 600)
const oneEur = new Money('EUR', 100)
sixEur.add(oneEur) // This returns a new Money object!
sixEur.toLocaleString() // => '€6.00'
// The amount value is *not* updated in-place.
// To get the result of `sixEur.add(oneEur)`:
const result = sixEur.add(oneEur)
result.toLocaleString() // => '€7.00'
// Properties are *always* read-only:
// TypeScript
const twoEur = new Money('EUR', 200)
twoEur['amount'] = 0 // => error TS2540: Cannot assign to 'amount'
// because it is a read-only property.
// JavaScript (in strict mode)
'use strict';
const threeEur = new Money('EUR', 300)
threeEur['amount'] = 0 // => TypeError: Cannot assign to read only
// property 'amount' of object '#<Money>'
发展
复制并安装依赖性 :
$ git clone https://github.com/half0wl/money.ts.git
$ yarn install
试验位于 /tests运行它们:
$ yarn test
代码文档使用 类型文件。 。 。 . 要生成文件 :
$ yarn gen-docs