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
4 changes: 2 additions & 2 deletions src/formatDecimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export default function(x) {
// significant digits p, where x is positive and p is in [1, 21] or undefined.
// For example, formatDecimalParts(1.23) returns ["123", 0].
export function formatDecimalParts(x, p) {
if ((i = (x = p ? x.toExponential(p - 1) : x.toExponential()).indexOf("e")) < 0) return null; // NaN, ±Infinity
var i, coefficient = x.slice(0, i);
if (!isFinite(x) || x === 0) return null; // NaN, ±Infinity, ±0
var i = (x = p ? x.toExponential(p - 1) : x.toExponential()).indexOf("e"), coefficient = x.slice(0, i);

// The string returned by toExponential either has the form \d\.\d+e[-+]\d+
// (e.g., 1.2e+3) or the form \de[-+]\d+ (e.g., 1e+3).
Expand Down
2 changes: 1 addition & 1 deletion src/formatPrefixAuto.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export var prefixExponent;

export default function(x, p) {
var d = formatDecimalParts(x, p);
if (!d) return x + "";
if (!d) return prefixExponent = undefined, x.toPrecision(p);
var coefficient = d[0],
exponent = d[1],
i = exponent - (prefixExponent = Math.max(-8, Math.min(8, Math.floor(exponent / 3))) * 3) + 1,
Expand Down
2 changes: 1 addition & 1 deletion src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default function(locale) {

// Compute the prefix and suffix.
valuePrefix = (valueNegative ? (sign === "(" ? sign : minus) : sign === "-" || sign === "(" ? "" : sign) + valuePrefix;
valueSuffix = (type === "s" ? prefixes[8 + prefixExponent / 3] : "") + valueSuffix + (valueNegative && sign === "(" ? ")" : "");
valueSuffix = (type === "s" && !isNaN(value) && prefixExponent !== undefined ? prefixes[8 + prefixExponent / 3] : "") + valueSuffix + (valueNegative && sign === "(" ? ")" : "");

// Break the formatted value into the integer “value” part that can be
// grouped, and fractional or exponential “suffix” part that is not.
Expand Down
7 changes: 6 additions & 1 deletion test/format-type-r-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {assert, test} from "vitest";
import {format} from "../src/index.js";

test("format(\"r\") can round to significant digits", () => {
assert.strictEqual(format(".2r")(0), "0.0");
assert.strictEqual(format(".1r")(0.049), "0.05");
assert.strictEqual(format(".1r")(-0.049), "−0.05");
assert.strictEqual(format(".1r")(0.49), "0.5");
Expand Down Expand Up @@ -32,6 +31,12 @@ test("format(\"r\") can round to significant digits", () => {
assert.strictEqual(format(".15r")(.999999999999999), "0.999999999999999");
});

test("format(\"r\") can round zero", () => {
assert.strictEqual(format(".2r")(0), "0");
assert.strictEqual(format(".1r")(0), "0");
assert.strictEqual(format("r")(0), "0");
});

test("format(\"r\") can round very small numbers", () => {
const f = format(".2r");
assert.strictEqual(f(1e-22), "0.00000000000000000000010");
Expand Down
7 changes: 7 additions & 0 deletions test/format-type-s-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ test("format(\"s\") outputs SI-prefix notation with default precision 6", () =>
assert.strictEqual(f(.000001), "1.00000µ");
});

test("format(\"s\") does not get confused by NaN, Infinity, etc.", () => {
const f = format("s");
assert.strictEqual(f(999500), "999.500k");
assert.strictEqual(f(Infinity), "Infinity");
assert.strictEqual(f(NaN), "NaN");
});

test("format(\"[.precision]s\") outputs SI-prefix notation with precision significant digits", () => {
const f1 = format(".3s");
assert.strictEqual(f1(0), "0.00");
Expand Down
13 changes: 13 additions & 0 deletions test/precisionFixed-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,16 @@ test("precisionFixed(number) returns the expected value", () => {
assert.strictEqual(precisionFixed(0.089), 2);
assert.strictEqual(precisionFixed(0.011), 2);
});

test("precisionFixed(0) returns NaN", () => {
assert.isNaN(precisionFixed(0));
});

test("precisionFixed(NaN) returns NaN", () => {
assert.isNaN(precisionFixed(NaN));
});

test("precisionFixed(Infinity) returns NaN", () => {
assert.isNaN(precisionFixed(Infinity));
assert.isNaN(precisionFixed(-Infinity));
});
13 changes: 13 additions & 0 deletions test/precisionPrefix-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,16 @@ test("precisionPrefix(step, value) returns the expected precision when value is
assert.strictEqual(precisionPrefix(1e24, 1e27), 0); // 1000Y
assert.strictEqual(precisionPrefix(1e23, 1e27), 1); // 1000.0Y
});

test("precisionPrefix(0, value) returns NaN", () => {
assert.isNaN(precisionPrefix(0, 1));
});

test("precisionPrefix(NaN, value) returns NaN", () => {
assert.isNaN(precisionPrefix(NaN, 1));
});

test("precisionPrefix(Infinity, value) returns NaN", () => {
assert.isNaN(precisionPrefix(Infinity, 1));
assert.isNaN(precisionPrefix(-Infinity, 1));
});
13 changes: 13 additions & 0 deletions test/precisionRound-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,16 @@ test("precisionRound(step, max) returns the expected value", () => {
assert.strictEqual(precisionRound(0.01, 1.00), 2); // "0.99", "1.0"
assert.strictEqual(precisionRound(0.01, 1.01), 3); // "1.00", "1.01"
});

test("precisionRound(0, max) returns NaN", () => {
assert.isNaN(precisionRound(0, 1));
});

test("precisionRound(NaN, max) returns NaN", () => {
assert.isNaN(precisionRound(NaN, 1));
});

test("precisionRound(Infinity, max) returns NaN", () => {
assert.isNaN(precisionRound(Infinity, 1));
assert.isNaN(precisionRound(-Infinity, 1));
});