|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | + |
| 3 | +import { humanize } from '../humanize'; |
| 4 | + |
| 5 | +describe('humanize', () => { |
| 6 | + it('returns the same string for numbers below 1000', () => { |
| 7 | + expect(humanize(0)).toBe('0'); |
| 8 | + expect(humanize(1)).toBe('1'); |
| 9 | + expect(humanize(12)).toBe('12'); |
| 10 | + expect(humanize(999)).toBe('999'); |
| 11 | + }); |
| 12 | + |
| 13 | + it('formats thousands with k suffix', () => { |
| 14 | + expect(humanize(1000)).toBe('1k'); |
| 15 | + expect(humanize(1500)).toBe('1.5k'); |
| 16 | + expect(humanize(12_300)).toBe('12.3k'); |
| 17 | + // >= 100 of the unit → no decimals |
| 18 | + expect(humanize(123_456)).toBe('123k'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('formats millions with M suffix', () => { |
| 22 | + expect(humanize(1_000_000)).toBe('1M'); // trailing .0 removed |
| 23 | + expect(humanize(1_500_000)).toBe('1.5M'); |
| 24 | + expect(humanize(12_000_000)).toBe('12M'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('formats billions with B suffix', () => { |
| 28 | + expect(humanize(1_000_000_000)).toBe('1B'); |
| 29 | + expect(humanize(1_250_000_000)).toBe('1.3B'); |
| 30 | + expect(humanize(12_345_678_901)).toBe('12.3B'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('rounds within the same unit and removes trailing .0', () => { |
| 34 | + // Rounds up within the same unit (k), does not carry over to the next unit |
| 35 | + expect(humanize(999_500)).toBe('1000k'); |
| 36 | + // Rounds to a whole number for millions when first decimal rounds to .0 |
| 37 | + expect(humanize(99_950_000)).toBe('100M'); |
| 38 | + // No trailing .0 |
| 39 | + expect(humanize(100_000)).toBe('100k'); |
| 40 | + }); |
| 41 | +}); |
0 commit comments