From 77b546d5f09d3eb33800548eb64aaa68df76cfba Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Mon, 15 Jun 2015 12:48:16 -0700 Subject: [PATCH] Fix for formatDecimal when p is undefined. --- src/formatDecimal.js | 2 +- test/precisionFixed-test.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 test/precisionFixed-test.js diff --git a/src/formatDecimal.js b/src/formatDecimal.js index f0afafe..3247cc9 100644 --- a/src/formatDecimal.js +++ b/src/formatDecimal.js @@ -2,7 +2,7 @@ // significant digits p, where x is positive and p is in [1, 21] or undefined. // For example, formatDecimal(1.23) returns ["123", -1]. export default function(x, p) { - if ((i = (x = x.toExponential(p - 1)).indexOf("e")) < 0) return null; // NaN, ±Infinity + if ((i = (x = x.toExponential(p && p - 1)).indexOf("e")) < 0) return null; // NaN, ±Infinity var i, coefficient = x.slice(0, i); // The string returned by toExponential either has the form \d\.\d+e[-+]\d+ diff --git a/test/precisionFixed-test.js b/test/precisionFixed-test.js new file mode 100644 index 0000000..372cefe --- /dev/null +++ b/test/precisionFixed-test.js @@ -0,0 +1,12 @@ +var tape = require("tape"), + format = require("../"); + +tape("precisionFixed(number) returns the expected value", function(test) { + test.equal(format.precisionFixed(8.9), 0); + test.equal(format.precisionFixed(1.1), 0); + test.equal(format.precisionFixed(0.89), 1); + test.equal(format.precisionFixed(0.11), 1); + test.equal(format.precisionFixed(0.089), 2); + test.equal(format.precisionFixed(0.011), 2); + test.end(); +});