Skip to content

Commit

Permalink
Set maximumSignificantDigits only when number > 0 to avoid format err…
Browse files Browse the repository at this point in the history
…ors on HTC Browser.

Fixes #96.
  • Loading branch information
John Madhavan-Reese committed Jan 19, 2018
1 parent 6f3a82c commit 9e6c6df
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/moment-duration-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@
localeStringOptions.minimumFractionDigits = fractionDigits;
}

if (maximumSignificantDigits) {
// toLocaleString output is "0.0" instead of "0" for HTC browsers
// when maximumSignificantDigits is set. See #96.
if (maximumSignificantDigits && number > 0) {
localeStringOptions.maximumSignificantDigits = maximumSignificantDigits;
}

Expand Down
15 changes: 15 additions & 0 deletions test/moment-duration-format-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,8 @@ test("userLocale and useGrouping", function () {
});

test("useSignificantDigits", function () {
equal(moment.duration(0, "seconds").format("s", { useSignificantDigits: true, precision: 0 }), "0");
equal(moment.duration(0, "seconds").format("s", { useSignificantDigits: true, precision: 0, useToLocaleString: false }), "0");
equal(moment.duration(99999, "seconds").format("s", { useSignificantDigits: true, precision: 2 }), "100,000");
equal(moment.duration(99.99, "seconds").format("s", { useSignificantDigits: true, precision: 3 }), "100");
equal(moment.duration(99.9944, "seconds").format("s", { useSignificantDigits: true, precision: 5 }), "99.994");
Expand Down Expand Up @@ -543,6 +545,13 @@ test("useSignificantDigits and trunc", function () {
}), "12:00");
});

test("useSignificantDigits and trim: false", function () {
equal(moment.duration(10, "seconds").format("h[h] m[m] s[s]",
{ useSignificantDigits: true, trim: false }), "0h 0m 10s");
equal(moment.duration(10, "seconds").format("h[h] m[m] s[s]",
{ useSignificantDigits: true, precision: 4, trim: false }), "0h 0m 10s");
});

test("Documentation examples", function () {
equal(moment.duration(123, "minutes").format(), "2:03:00");
equal(moment.duration(123, "months").format(), "10 years, 3 months");
Expand Down Expand Up @@ -1629,6 +1638,12 @@ test("Mixed positive and negative durations", function () {
});

test("useSignificantDigits", function () {
deepEqual(moment.duration.format([
moment.duration(10, "seconds"),
moment.duration(1000000, "seconds")],
"y[y] M[m] d[d] h[h] m[m] s[s]",
{ useSignificantDigits: true, precision: 4, useToLocaleString: false }
), ["0d 0h 10s", "11d 14h 0s"]);
deepEqual(moment.duration.format([
moment.duration(10, "seconds"),
moment.duration(100, "seconds"),
Expand Down

0 comments on commit 9e6c6df

Please sign in to comment.