From 76b1a4a8655f773e9ddeb8ccdedd0dd8c2a6a3e6 Mon Sep 17 00:00:00 2001 From: artahir101 Date: Fri, 6 Feb 2026 13:29:04 +0500 Subject: [PATCH] fix: handle negative pluralization correctly --- src/format.test.ts | 12 ++++++++++++ src/index.ts | 5 +++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/format.test.ts b/src/format.test.ts index cdabb0f..3d793d1 100644 --- a/src/format.test.ts +++ b/src/format.test.ts @@ -258,4 +258,16 @@ describe('format(invalid inputs)', () => { format(-Infinity); }).toThrow(); }); + + it('should handle negative pluralization correctly', () => { + expect(format(-1500, { long: true })).toBe('-1 second'); + expect(format(-1501, { long: true })).toBe('-2 seconds'); + expect(format(-1000, { long: true })).toBe('-1 second'); + }); + + it('should handle positive pluralization correctly', () => { + expect(format(1500, { long: true })).toBe('2 seconds'); + expect(format(1501, { long: true })).toBe('2 seconds'); + expect(format(1000, { long: true })).toBe('1 second'); + }); }); diff --git a/src/index.ts b/src/index.ts index d50e3c7..b1f3df5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -239,6 +239,7 @@ function plural( n: number, name: string, ): StringValue { - const isPlural = msAbs >= n * 1.5; - return `${Math.round(ms / n)} ${name}${isPlural ? 's' : ''}` as StringValue; + const rounded = Math.round(ms / n); + const isPlural = Math.abs(rounded) !== 1; + return `${rounded} ${name}${isPlural ? 's' : ''}` as StringValue; }