money.compare
Compare two amounts of one currency, refusing to compare across currencies; min, max and clamp too.
1.0.0 (not the latest) · published 2026-10-03 by charlie · Anterra
Pinned by 11 tests, run in TypeScript, Python and Rust.
What it does
`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`.
For example
compareMoney(£1.00, £2.00)→ -1 less is -1compareMoney(£2.00, £1.00)→ 1 greater is 1compareMoney(£19.99, £19.99)→ 0 equal is 0
The function
The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.
export function compareMoney(a: Money, b: Money): number
| a | Money | |
| b | Money | |
| returns | int | -1 if a is less than b, 0 if equal, 1 if greater |
Your code names it in one line, in the file that uses it
import { compareMoney } from "#fune/money.compare@^1";
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
import { clamp } from "./math_clamp.ts"; ← from math.clamp ^1.0.0 · built alongside by fune
import { type Money, assertSameCurrency, money } from "./money_amount.ts"; ← from money.amount ^1.0.0 · built alongside by fune
/**
* -1, 0 or 1, comparing two amounts of the same currency.
*
* Refuses mixed currencies: which of GBP 10 and EUR 10 is larger depends on a
* rate, and a comparison that ignores the currency sorts a list into nonsense.
*/
export function compareMoney(a: Money, b: Money): number {
assertSameCurrency(a, b);
return a.minor < b.minor ? -1 : a.minor > b.minor ? 1 : 0;
}
/** The smaller amount; the first when they are equal. */
export function minMoney(a: Money, b: Money): Money {
return compareMoney(b, a) < 0 ? b : a;
}
/** The larger amount; the first when they are equal. */
export function maxMoney(a: Money, b: Money): Money {
return compareMoney(b, a) > 0 ? b : a;
}
/** `amount` held within `min..max`, all three in one currency. */
export function clampMoney(amount: Money, min: Money, max: Money): Money {
assertSameCurrency(amount, min);
assertSameCurrency(amount, max);
return money(clamp(amount.minor, min.minor, max.minor), amount.currency);
}Install
fune build
With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and its 2 dependencies, pins them in fune.lock, downloads only the TypeScript package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:
fune add money.compare
The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./money.compare-1.0.0-typescript.fune, or fetch it from a terminal with fune pull money.compare@1.0.0:typescript.
The whole function, every language, is one file too: money.compare-1.0.0.fune, 9,081 bytes, sha256 a2f90e08a0b24b24f121a1d6df2604bc4d7f49028d1aeeaf71dcd14cd8162520. It installs into a project of any language.
Customise it in your app
The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.
before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.
// fune: before money.compare
after — your function gets the result and the arguments, and returns the final result.
// fune: after money.compare
replace — inside this capability’s code only, calls to a dependency go to your function, with the same signature. Other capabilities that use it are unaffected; write in * to replace it everywhere.
// fune: replace math.clamp in money.compare
// fune: replace money.amount in money.compare
step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show money.compare --steps.
// fune: step money.compare after <n|label>
Tests
A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.
| Case | Arguments | Expected | |
|---|---|---|---|
| less is -1 | £1.00, £2.00 | → | -1 |
| greater is 1 | £2.00, £1.00 | → | 1 |
| equal is 0 | £19.99, £19.99 | → | 0 |
| the answer is 1, not the difference of 1998 | £19.99, £0.01 | → | 1 |
| a larger debt is less | -£5.00, -£1.00 | → | -1 |
| zero is more than a credit | £0.00, -£0.01 | → | 1 |
| one minor unit apart | £100.00, £100.01 | → | -1 |
| yen compares in whole yen | ¥1,500, ¥1,499 | → | 1 |
| the largest safe amounts still compare exactly | £90,071,992,547,409.91, £90,071,992,547,409.90 | → | 1 |
| opposite extremes | -£90,071,992,547,409.91, £90,071,992,547,409.91 | → | -1 |
Show the other 1 test
| Case | Arguments | Expected | |
|---|---|---|---|
| comparing across currencies is an error, not an answer | £10.00, €10.00 | → | error: currency mismatch |
More from the author
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.
Files
| Path | Bytes |
|---|---|
| README.md | 953 |
| impl/python.py | 1,088 |
| impl/rust.rs | 1,557 |
| impl/typescript.ts | 1,098 |
| vectors.json | 2,476 |