| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { microsOf, money, plainDollars, wholeDollars } from "./money.ts"; |
| 5 | |
| 6 | test("money rounds half up at the cent on whole micros, never on a float", () => { |
| 7 | assert.equal(money(2_760_000), "$2.76"); |
| 8 | assert.equal(money(10_404_999), "$10.40"); |
| 9 | assert.equal(money(10_405_000), "$10.41"); |
| 10 | assert.equal(money(10_410_000), "$10.41"); |
| 11 | assert.equal(money(1_005_000), "$1.01"); |
| 12 | assert.equal(money(1_004_999), "$1.00"); |
| 13 | // 1.005 as a float rounds down with toFixed; on micros it is exactly half, so up. |
| 14 | assert.equal(money(1.005 * 1_000_000), "$1.01"); |
| 15 | assert.equal(money(999_995_000), "$1,000.00"); |
| 16 | assert.equal(money(1_234_567_890_000), "$1,234,567.89"); |
| 17 | }); |
| 18 | |
| 19 | test("a fraction of a cent reads <$0.01, and exactly nothing reads $0.00", () => { |
| 20 | assert.equal(money(0), "$0.00"); |
| 21 | assert.equal(money(1), "<$0.01"); |
| 22 | assert.equal(money(3), "<$0.01"); |
| 23 | assert.equal(money(4_000), "<$0.01"); |
| 24 | assert.equal(money(9_000), "<$0.01"); |
| 25 | assert.equal(money(9_999), "<$0.01"); |
| 26 | assert.equal(money(10_000), "$0.01"); |
| 27 | assert.equal(money(-9_000), "<−$0.01"); |
| 28 | }); |
| 29 | |
| 30 | test("negatives lead with a minus sign", () => { |
| 31 | assert.equal(money(-1_250_000), "−$1.25"); |
| 32 | assert.equal(money(-10_405_000), "−$10.41"); |
| 33 | assert.equal(money(-1_000_000_000), "−$1,000.00"); |
| 34 | }); |
| 35 | |
| 36 | test("precise shows up to four places, trimmed down to two", () => { |
| 37 | assert.equal(money(9_000, { precise: true }), "$0.009"); |
| 38 | assert.equal(money(6_300, { precise: true }), "$0.0063"); |
| 39 | assert.equal(money(250, { precise: true }), "$0.0003"); |
| 40 | assert.equal(money(249, { precise: true }), "$0.0002"); |
| 41 | assert.equal(money(250_000, { precise: true }), "$0.25"); |
| 42 | assert.equal(money(2_760_000, { precise: true }), "$2.76"); |
| 43 | assert.equal(money(24_000, { precise: true }), "$0.024"); |
| 44 | assert.equal(money(1_234_567_890, { precise: true }), "$1,234.5679"); |
| 45 | assert.equal(money(0, { precise: true }), "$0.00"); |
| 46 | assert.equal(money(49, { precise: true }), "<$0.0001"); |
| 47 | assert.equal(money(50, { precise: true }), "$0.0001"); |
| 48 | assert.equal(money(-6_300, { precise: true }), "−$0.0063"); |
| 49 | }); |
| 50 | |
| 51 | test("compact is an axis label", () => { |
| 52 | assert.equal(money(0, { compact: true }), "$0"); |
| 53 | assert.equal(money(2_000_000, { compact: true }), "$2"); |
| 54 | assert.equal(money(500_000, { compact: true }), "$0.50"); |
| 55 | assert.equal(money(1_000, { compact: true }), "$0.001"); |
| 56 | assert.equal(money(1_500_000_000, { compact: true }), "$1.5K"); |
| 57 | assert.equal(money(1_000_000_000, { compact: true }), "$1K"); |
| 58 | assert.equal(money(12_340_000_000, { compact: true }), "$12.3K"); |
| 59 | assert.equal(money(2_500_000_000_000, { compact: true }), "$2.5M"); |
| 60 | }); |
| 61 | |
| 62 | test("whole dollars drop the cents only when there are none", () => { |
| 63 | assert.equal(wholeDollars(1_000_000_000), "$1,000"); |
| 64 | assert.equal(wholeDollars(100_000), "$0.10"); |
| 65 | assert.equal(wholeDollars(-5_000_000), "−$5"); |
| 66 | }); |
| 67 | |
| 68 | test("form fields read plain dollars, rounded the same way", () => { |
| 69 | assert.equal(plainDollars(12_500_000), "12.50"); |
| 70 | assert.equal(plainDollars(20_000_000), "20.00"); |
| 71 | assert.equal(plainDollars(20_000_000, { whole: true }), "20"); |
| 72 | assert.equal(plainDollars(20_125_000, { whole: true }), "20.13"); |
| 73 | assert.equal(plainDollars(20_124_999, { whole: true }), "20.12"); |
| 74 | assert.equal(plainDollars(0, { whole: true }), "0"); |
| 75 | assert.equal(plainDollars(1_200_000_000), "1200.00"); |
| 76 | }); |
| 77 | |
| 78 | test("dollars from a service that counts in dollars become whole micros", () => { |
| 79 | assert.equal(microsOf(0.123456789), 123_457); |
| 80 | assert.equal(microsOf(2.5), 2_500_000); |
| 81 | assert.equal(money(microsOf(0.004)), "<$0.01"); |
| 82 | }); |