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.
This commit is contained in:
dualfroz committed 2026-09-04 09:04:26 +02:00
1 parent 194562d9da
commit 93dd2b2f06
3 files changed
+105 -1

No files matched your search

+14
View File
@@ -162,6 +162,20 @@ describe("humanizer", function () {
assert.strictEqual(h(7999), "7.999 seconds");
});
it('pluralizes based on the count rendered by "maxDecimalPoints"', function () {
// When `maxDecimalPoints` truncates the count down to exactly 1, the unit
// word must agree with the displayed "1", i.e. be singular.
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 });