Functional Weave
Code in TypeScript

money.compare@1.0.0

README.md

953 bytes · view raw

# money.compare

`compareMoney(a, b)` is -1, 0 or 1, the shape every sort function takes. It
is exactly -1, 0 or 1, not the difference `a - b`, so a caller can switch on it
and it cannot overflow.

Comparing GBP 10 with EUR 10 is an error ("currency mismatch"), not an
answer. Which of the two is larger depends on an exchange rate, and a
comparison that quietly looks only at the numbers will sort a mixed-currency
list into nonsense. Convert first with `money.convert`.

The module also exports `minMoney(a, b)`, `maxMoney(a, b)` and
`clampMoney(amount, min, max)`, built on `compareMoney` and `math.clamp` with
the same currency check. `minMoney` and `maxMoney` return the first argument
when the two are equal. `clampMoney` refuses a range whose `min` is above its
`max`, as `math.clamp` does: capping a refund at the original charge, or a
discount at the order total, with the bounds the wrong way round is a bug to
surface, not a value to return.