Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function(locale) {
minus = locale.minus === undefined ? "−" : locale.minus + "",
nan = locale.nan === undefined ? "NaN" : locale.nan + "";

function newFormat(specifier) {
function newFormat(specifier, options) {
specifier = formatSpecifier(specifier);

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

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

// What format function should we use?
// Is this an integer type?
Expand Down Expand Up @@ -132,12 +132,11 @@ export default function(locale) {
}

function formatPrefix(specifier, value) {
var f = newFormat((specifier = formatSpecifier(specifier), specifier.type = "f", specifier)),
e = Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3,
var e = Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3,
k = Math.pow(10, -e),
prefix = prefixes[8 + e / 3];
f = newFormat((specifier = formatSpecifier(specifier), specifier.type = "f", specifier), {suffix: prefixes[8 + e / 3]});
return function(value) {
return f(k * value) + prefix;
return f(k * value);
};
}

Expand Down
31 changes: 24 additions & 7 deletions test/formatPrefix-test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
import {assert, test} from "vitest";
import {formatPrefix} from "../src/index.js";
import {format, formatPrefix} from "../src/index.js";

test("formatPrefix(\"s\", value)(number) formats with the SI prefix appropriate to the specified value", () => {
test("formatPrefix(\",.0s\", value)(number) formats with the SI prefix appropriate to the specified value", () => {
assert.strictEqual(formatPrefix(",.0s", 1e-6)(.00042), "420µ");
assert.strictEqual(formatPrefix(",.0s", 1e-6)(.0042), "4,200µ");
});

test("formatPrefix(\",.3s\", value)(number) formats with the SI prefix appropriate to the specified value", () => {
assert.strictEqual(formatPrefix(",.3s", 1e-3)(.00042), "0.420m");
});

test("formatPrefix(\"s\", value)(number) uses yocto for very small reference values", () => {
test("formatPrefix(\",.0s\", value)(number) uses yocto for very small reference values", () => {
assert.strictEqual(formatPrefix(",.0s", 1e-27)(1e-24), "1y");
});

test("formatPrefix(\"s\", value)(number) uses yotta for very small reference values", () => {
test("formatPrefix(\",.0s\", value)(number) uses yotta for very small reference values", () => {
assert.strictEqual(formatPrefix(",.0s", 1e27)(1e24), "1Y");
});

test("formatPrefix(\"$,s\", value)(number) formats with the specified SI prefix", () => {
test("formatPrefix(\" $12,.1s\", value)(number) formats with the specified SI prefix", () => {
// The fixed length of 12 is inclusive of the unit 'M'
const f = formatPrefix(" $12,.1s", 1e6);
assert.strictEqual(f(-42e6), " −$42.0M");
assert.strictEqual(f(+4.2e6), " $4.2M");
assert.strictEqual(f(-42e6), " −$42.0M");
assert.strictEqual(f(+4.2e6), " $4.2M");
});

test("formatPrefix(\" $12,.1s\", value)(number) matches format(\" $12,.2s\")(number) when the units are the same", () => {
// The fixed length of 12 is inclusive of the unit 'M'
const fp = formatPrefix(" $12,.1s", 1e6);
const f = format(" $12,.2s");
assert.strictEqual(fp(+4.2e6), " $4.2M");
assert.strictEqual(f(+4.2e6), " $4.2M");
});

test("formatPrefix(\"($~s\", value)(number) formats with the SI prefix inside parentheses", () => {
assert.strictEqual(formatPrefix("($~s", 1e3)(1e3), "$1k");
assert.strictEqual(formatPrefix("($~s", 1e3)(-1e3), "($1k)");
});