|
1 | 1 | import {assert, test} from "vitest"; |
2 | | -import {formatPrefix} from "../src/index.js"; |
| 2 | +import {format, formatPrefix} from "../src/index.js"; |
3 | 3 |
|
4 | | -test("formatPrefix(\"s\", value)(number) formats with the SI prefix appropriate to the specified value", () => { |
| 4 | +test("formatPrefix(\",.0s\", value)(number) formats with the SI prefix appropriate to the specified value", () => { |
5 | 5 | assert.strictEqual(formatPrefix(",.0s", 1e-6)(.00042), "420µ"); |
6 | 6 | assert.strictEqual(formatPrefix(",.0s", 1e-6)(.0042), "4,200µ"); |
| 7 | +}); |
| 8 | + |
| 9 | +test("formatPrefix(\",.3s\", value)(number) formats with the SI prefix appropriate to the specified value", () => { |
7 | 10 | assert.strictEqual(formatPrefix(",.3s", 1e-3)(.00042), "0.420m"); |
8 | 11 | }); |
9 | 12 |
|
10 | | -test("formatPrefix(\"s\", value)(number) uses yocto for very small reference values", () => { |
| 13 | +test("formatPrefix(\",.0s\", value)(number) uses yocto for very small reference values", () => { |
11 | 14 | assert.strictEqual(formatPrefix(",.0s", 1e-27)(1e-24), "1y"); |
12 | 15 | }); |
13 | 16 |
|
14 | | -test("formatPrefix(\"s\", value)(number) uses yotta for very small reference values", () => { |
| 17 | +test("formatPrefix(\",.0s\", value)(number) uses yotta for very small reference values", () => { |
15 | 18 | assert.strictEqual(formatPrefix(",.0s", 1e27)(1e24), "1Y"); |
16 | 19 | }); |
17 | 20 |
|
18 | | -test("formatPrefix(\"$,s\", value)(number) formats with the specified SI prefix", () => { |
| 21 | +test("formatPrefix(\" $12,.1s\", value)(number) formats with the specified SI prefix", () => { |
| 22 | + // The fixed length of 12 is inclusive of the unit 'M' |
19 | 23 | const f = formatPrefix(" $12,.1s", 1e6); |
20 | | - assert.strictEqual(f(-42e6), " −$42.0M"); |
21 | | - assert.strictEqual(f(+4.2e6), " $4.2M"); |
| 24 | + assert.strictEqual(f(-42e6), " −$42.0M"); |
| 25 | + assert.strictEqual(f(+4.2e6), " $4.2M"); |
| 26 | +}); |
| 27 | + |
| 28 | +test("formatPrefix(\" $12,.1s\", value)(number) matches format(\" $12,.2s\")(number) when the units are the same", () => { |
| 29 | + // The fixed length of 12 is inclusive of the unit 'M' |
| 30 | + const fp = formatPrefix(" $12,.1s", 1e6); |
| 31 | + const f = format(" $12,.2s"); |
| 32 | + assert.strictEqual(fp(+4.2e6), " $4.2M"); |
| 33 | + assert.strictEqual(f(+4.2e6), " $4.2M"); |
| 34 | +}); |
| 35 | + |
| 36 | +test("formatPrefix(\"($~s\", value)(number) formats with the SI prefix inside parentheses", () => { |
| 37 | + assert.strictEqual(formatPrefix("($~s", 1e3)(1e3), "$1k"); |
| 38 | + assert.strictEqual(formatPrefix("($~s", 1e3)(-1e3), "($1k)"); |
22 | 39 | }); |
0 commit comments