Functional Weave
Code in TypeScript

money.compare@2.0.0

README.md

1,874 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`.

`minMoney(a, b)` and `maxMoney(a, b)` are built on `compareMoney`, with the
same currency check, and return the first argument when the two are equal.
`clampMoney(amount, min, max)` holds an amount within an inclusive range,
all three in one currency, using `math.clamp`. It refuses a range whose `min`
is above its `max` ("min must not be greater than max"): 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.

This is a group of four functions, each in its own file. `minMoney` and
`maxMoney` import `compareMoney`, so installing either with `only=` brings it
too; `clampMoney` stands alone.

## What changed from 1.0.0

1.0.0 was one function, `compareMoney`, with `minMoney`, `maxMoney` and
`clampMoney` exported beside it but unpinned: no signature in the manifest
and no vectors, so nothing held the three languages to the same answer for
them. 2.0.0 is a group in which all four are published functions with their
own signatures and vectors. `compareMoney` keeps every 1.0.0 vector.

No answer changed. It is a new major version because the package's shape
changed: it installs as one module per function plus the group module
(`money_compare` still re-exports all four), and a project can take only some
of it. Dependents stay on `^1.0.0` until they move deliberately; 1.0.0 is
unchanged.