Skip to content

Commit 17585db

Browse files
GlenKelleyGlen Kelleymbostock
authored
Fix for issue 134 (#135)
* Added tests for Accounting notation with unix suffixes. * Fixed unit order to be inside parentheses. * options * fix merge * fix merge, again --------- Co-authored-by: Glen Kelley <gkelley@palantir.com> Co-authored-by: Mike Bostock <mbostock@gmail.com>
1 parent a13ebb9 commit 17585db

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

src/locale.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default function(locale) {
2020
minus = locale.minus === undefined ? "−" : locale.minus + "",
2121
nan = locale.nan === undefined ? "NaN" : locale.nan + "";
2222

23-
function newFormat(specifier) {
23+
function newFormat(specifier, options) {
2424
specifier = formatSpecifier(specifier);
2525

2626
var fill = specifier.fill,
@@ -45,8 +45,8 @@ export default function(locale) {
4545

4646
// Compute the prefix and suffix.
4747
// For SI-prefix, the suffix is lazily computed.
48-
var prefix = symbol === "$" ? currencyPrefix : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : "",
49-
suffix = symbol === "$" ? currencySuffix : /[%p]/.test(type) ? percent : "";
48+
var prefix = (options?.prefix !== undefined ? options.prefix : "") + (symbol === "$" ? currencyPrefix : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : ""),
49+
suffix = (symbol === "$" ? currencySuffix : /[%p]/.test(type) ? percent : "") + (options?.suffix !== undefined ? options.suffix : "");
5050

5151
// What format function should we use?
5252
// Is this an integer type?
@@ -132,12 +132,11 @@ export default function(locale) {
132132
}
133133

134134
function formatPrefix(specifier, value) {
135-
var f = newFormat((specifier = formatSpecifier(specifier), specifier.type = "f", specifier)),
136-
e = Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3,
135+
var e = Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3,
137136
k = Math.pow(10, -e),
138-
prefix = prefixes[8 + e / 3];
137+
f = newFormat((specifier = formatSpecifier(specifier), specifier.type = "f", specifier), {suffix: prefixes[8 + e / 3]});
139138
return function(value) {
140-
return f(k * value) + prefix;
139+
return f(k * value);
141140
};
142141
}
143142

test/formatPrefix-test.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
import {assert, test} from "vitest";
2-
import {formatPrefix} from "../src/index.js";
2+
import {format, formatPrefix} from "../src/index.js";
33

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", () => {
55
assert.strictEqual(formatPrefix(",.0s", 1e-6)(.00042), "420µ");
66
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", () => {
710
assert.strictEqual(formatPrefix(",.3s", 1e-3)(.00042), "0.420m");
811
});
912

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", () => {
1114
assert.strictEqual(formatPrefix(",.0s", 1e-27)(1e-24), "1y");
1215
});
1316

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", () => {
1518
assert.strictEqual(formatPrefix(",.0s", 1e27)(1e24), "1Y");
1619
});
1720

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'
1923
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)");
2239
});

0 commit comments

Comments
 (0)