From 117fd38eeb6ba865cc396ae730d1f806b575078f Mon Sep 17 00:00:00 2001 From: dualfroz Date: Fri, 4 Sep 2026 09:04:26 +0200 Subject: [PATCH] fix: pluralize unit word using the maxDecimalPoints-truncated count When maxDecimalPoints truncates a unit count to exactly 1, the rendered number showed 1 while the word was pluralized from the raw count (for example humanizeDuration(1500, { maxDecimalPoints: 0 }) returned "1 seconds"). Pluralize from the normalized count that is actually displayed so the number and word agree. --- humanize-duration.js | 4 +++- test/humanizer.js | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/humanize-duration.js b/humanize-duration.js index 0df4917..c2a148c 100644 --- a/humanize-duration.js +++ b/humanize-duration.js @@ -1382,7 +1382,9 @@ var languageWord = language[unitName]; var word; if (typeof languageWord === "function") { - word = languageWord(unitCount); + // Pluralize on the rendered count: `maxDecimalPoints` may truncate 1.5 to + // "1", and the word must agree with what is shown. + word = languageWord(normalizedUnitCount); } else { word = languageWord; } diff --git a/test/humanizer.js b/test/humanizer.js index 2d966ed..d8ee572 100644 --- a/test/humanizer.js +++ b/test/humanizer.js @@ -162,6 +162,19 @@ describe("humanizer", function () { assert.strictEqual(h(7999), "7.999 seconds"); }); + it('pluralizes based on the count rendered by "maxDecimalPoints"', function () { + // A count truncated to exactly 1 must take the singular word. + assert.strictEqual(humanizer({ maxDecimalPoints: 0 })(1500), "1 second"); + assert.strictEqual(humanizer({ maxDecimalPoints: 2 })(1005), "1 second"); + // A truncated count that is not 1 stays plural. + assert.strictEqual(humanizer({ maxDecimalPoints: 0 })(2500), "2 seconds"); + // German shares the same singular/plural mechanism. + assert.strictEqual( + humanizer({ language: "de", maxDecimalPoints: 0 })(1500), + "1 Sekunde" + ); + }); + it("can ask for the largest units", function () { const h = humanizer({ largest: 2 });