import { describe, expect, it } from 'vitest' import { digits, figures, moved } from './figure' /** * The arithmetic and the language behind every number on the surface. * * A headline figure is the first thing a person reads and the least likely * thing anyone checks, which is the combination worth a test. */ const en = figures({ locale: 'en-US', currency: 'USD' }) describe('a figure in a headline', () => { it('separates thousands until the number needs a suffix', () => { expect(en.brief(842)).toBe('842') expect(en.brief(4210)).toBe('4.21K') expect(en.brief(4_210_000)).toBe('4.21M') expect(en.brief(4_210_000_000)).toBe('4.21B') }) it('keeps the sign, so a negative figure does not read as a positive one', () => { expect(en.brief(-4_210_000)).toBe('-4.21M') }) it('states a sum and shortens a headline, which are two figures', () => { expect(en.money(1_284_003)).toBe('$1,284,003.00') expect(en.worth(1_284_003)).toBe('$1.28M') }) it('draws a price under one at the digits that mean something', () => { // Two places after the point prints a thousandth as zero, and a price // printed as zero is worth less than no price at all. expect(en.count(0.00042)).toBe('0.00042') }) it('refuses a figure that is not one, rather than inventing a rendering', () => { expect(en.count(Number.NaN)).toBe('—') expect(en.money(Number.POSITIVE_INFINITY)).toBe('—') }) }) describe('the reader', () => { it('decides the separators, the suffix and where the mark goes', () => { const fr = figures({ locale: 'fr-CA', currency: 'USD' }) const ja = figures({ locale: 'ja-JP', currency: 'JPY' }) // French Canadian puts the mark after the digits and groups with spaces; // Japanese counts in 万 at four digits rather than K at three. expect(fr.worth(1_284_003)).not.toBe(en.worth(1_284_003)) expect(ja.brief(4_210_000)).toContain('万') }) it('signs a movement and does not sign a charge', () => { // `+0.25%` on a fee reads as a fee that went up, which is the one thing the // figure does not say. expect(en.percent(0.0421)).toBe('+4.21%') expect(en.percent(-0.0421)).toBe('-4.21%') expect(en.percent(0)).toBe('0.00%') expect(en.rate(0.0025)).toBe('0.25%') }) it('splits an amount where its own language splits it', () => { // Not at a character: which character separates the two, and whether the // fraction leads or trails, is a property of the language. expect(en.parted(1_284_003)).toEqual(['$1,284,003', '.00']) }) it('names a currency out of the same table the figures come from', () => { expect(en.sign('EUR')).toBe('€') }) }) describe('what a figure did', () => { it('is a proportion, because the hundred is the reader’s to apply', () => { expect(moved(110, 100)).toBeCloseTo(0.1) expect(moved(90, 100)).toBeCloseTo(-0.1) expect(en.percent(moved(110, 100)!)).toBe('+10.00%') }) it('has no answer where there is nothing to compare against', () => { expect(moved(110, null)).toBeNull() expect(moved(null, 100)).toBeNull() }) it('has no answer where the earlier figure is zero', () => { // A rise from nothing is not a proportion. The naive form is Infinity, // which renders as "Infinity%" beside a real number. expect(moved(110, 0)).toBeNull() }) it('refuses a figure that is not one', () => { expect(moved(Number.NaN, 100)).toBeNull() expect(moved(110, Number.POSITIVE_INFINITY)).toBeNull() }) }) describe('what a field keeps of what was typed into it', () => { it('takes digits and at most one point', () => { expect(digits('1a2b.3c4')).toBe('12.34') expect(digits('1.2.3')).toBe('1.23') }) it('takes no more places than the unit carries', () => { expect(digits('1.23456789', 6)).toBe('1.234567') expect(digits('1.5', 0)).toBe('1') }) it('keeps them all where no unit says otherwise', () => { expect(digits('1.23456789012345678')).toBe('1.23456789012345678') }) })