From 6c51534a70c772a790b1e19225c710c6e87cf51e Mon Sep 17 00:00:00 2001 From: Pitchfork-and-Torch Date: Fri, 18 Sep 2026 02:06:46 +0000 Subject: [PATCH 1/3] fix: format() without scientific notation for parse() round-trips MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Very small or very large finite values (e.g. 1e-7, 1e21) stringified as 1e-7ms / …e+…y, which parse() cannot read (returns NaN). Format the numeric component with toLocaleString so output stays decimal. Also align the parse() length error text with the actual 1..100 check. --- src/format.test.ts | 11 +++++++++++ src/index.ts | 42 +++++++++++++++++++++++++++++++----------- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/format.test.ts b/src/format.test.ts index cdabb0f6..8748c2ec 100644 --- a/src/format.test.ts +++ b/src/format.test.ts @@ -259,3 +259,14 @@ describe('format(invalid inputs)', () => { }).toThrow(); }); }); + +describe('format avoids scientific notation', () => { + it('formats tiny millisecond values without scientific notation', () => { + expect(format(1e-7)).toBe('0.0000001ms'); + }); + + it('formats huge values without scientific notation', () => { + expect(format(1e21)).toBe('31688087814y'); + expect(format(1e21).includes('e')).toBe(false); + }); +}); diff --git a/src/index.ts b/src/index.ts index d50e3c77..5f18df3a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -71,7 +71,7 @@ export function ms( export function parse(str: string): number { if (typeof str !== 'string' || str.length === 0 || str.length > 100) { throw new Error( - `Value provided to ms.parse() must be a string with length between 1 and 99. value=${JSON.stringify(str)}`, + `Value provided to ms.parse() must be a string with length between 1 and 100. value=${JSON.stringify(str)}`, ); } const match = @@ -157,33 +157,53 @@ export function parseStrict(value: StringValue): number { return parse(value); } + +/** + * Format a number without scientific notation so parse() can round-trip it. + */ +function numberToDecimalString(n: number): string { + if (Object.is(n, -0)) { + return '0'; + } + if (Number.isInteger(n)) { + return n.toLocaleString('en-US', { + useGrouping: false, + maximumFractionDigits: 0, + }); + } + return n.toLocaleString('en-US', { + useGrouping: false, + maximumSignificantDigits: 21, + }); +} + /** * Short format for `ms`. */ function fmtShort(ms: number): StringValue { const msAbs = Math.abs(ms); if (msAbs >= y) { - return `${Math.round(ms / y)}y`; + return `${numberToDecimalString(Math.round(ms / y))}y`; } if (msAbs >= mo) { - return `${Math.round(ms / mo)}mo`; + return `${numberToDecimalString(Math.round(ms / mo))}mo`; } if (msAbs >= w) { - return `${Math.round(ms / w)}w`; + return `${numberToDecimalString(Math.round(ms / w))}w`; } if (msAbs >= d) { - return `${Math.round(ms / d)}d`; + return `${numberToDecimalString(Math.round(ms / d))}d`; } if (msAbs >= h) { - return `${Math.round(ms / h)}h`; + return `${numberToDecimalString(Math.round(ms / h))}h`; } if (msAbs >= m) { - return `${Math.round(ms / m)}m`; + return `${numberToDecimalString(Math.round(ms / m))}m`; } if (msAbs >= s) { - return `${Math.round(ms / s)}s`; + return `${numberToDecimalString(Math.round(ms / s))}s`; } - return `${ms}ms`; + return `${numberToDecimalString(ms)}ms`; } /** @@ -212,7 +232,7 @@ function fmtLong(ms: number): StringValue { if (msAbs >= s) { return plural(ms, msAbs, s, 'second'); } - return `${ms} ms`; + return `${numberToDecimalString(ms)} ms`; } /** @@ -240,5 +260,5 @@ function plural( name: string, ): StringValue { const isPlural = msAbs >= n * 1.5; - return `${Math.round(ms / n)} ${name}${isPlural ? 's' : ''}` as StringValue; + return `${numberToDecimalString(Math.round(ms / n))} ${name}${isPlural ? 's' : ''}` as StringValue; } From a7d4b146fe8ee57bc91293f25a1ce652dc3e6bad Mon Sep 17 00:00:00 2001 From: Pitchfork-and-Torch <297513015+Pitchfork-and-Torch@users.noreply.github.com> Date: Sun, 1 Nov 2026 15:04:57 -0500 Subject: [PATCH 2/3] fix: keep decimal format off toLocaleString String() stays the path for ordinary values. Only scientific notation is expanded by hand, so format(1e-7) still round-trips and common calls stay near the old cost. --- src/index.ts | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index 5f18df3a..92d0a4ec 100644 --- a/src/index.ts +++ b/src/index.ts @@ -160,21 +160,36 @@ export function parseStrict(value: StringValue): number { /** * Format a number without scientific notation so parse() can round-trip it. + * + * Ordinary values stay on String(), which is what format() used to emit. + * Only an exponent (1e-7, 1e21) is expanded by hand. toLocaleString() is + * avoided: it is hundreds of times slower and locale-sensitive. */ function numberToDecimalString(n: number): string { if (Object.is(n, -0)) { return '0'; } - if (Number.isInteger(n)) { - return n.toLocaleString('en-US', { - useGrouping: false, - maximumFractionDigits: 0, - }); + const plain = String(n); + if (!/[eE]/.test(plain)) { + return plain; + } + const match = /^(-?)(\d+(?:\.\d+)?)[eE]([+-]?\d+)$/.exec(plain); + if (!match) { + return plain; + } + const sign = match[1]; + const digits = match[2].replace('.', ''); + const exponent = Number(match[3]); + const dot = match[2].indexOf('.'); + const fractionDigits = dot === -1 ? 0 : match[2].length - dot - 1; + const point = digits.length - fractionDigits + exponent; + if (point <= 0) { + return `${sign}0.${'0'.repeat(-point)}${digits}`; + } + if (point >= digits.length) { + return `${sign}${digits}${'0'.repeat(point - digits.length)}`; } - return n.toLocaleString('en-US', { - useGrouping: false, - maximumSignificantDigits: 21, - }); + return `${sign}${digits.slice(0, point)}.${digits.slice(point)}`; } /** From d3c3ddd980dbb5a6e14ad444c6578af47561831a Mon Sep 17 00:00:00 2001 From: Pitchfork-and-Torch <297513015+Pitchfork-and-Torch@users.noreply.github.com> Date: Thu, 5 Nov 2026 18:35:13 -0500 Subject: [PATCH 3/3] Test format(1e36), where the year count stringifies with an exponent The formatted result has no exponent. The helper comment keeps round-trips scoped to common values, because expanded extremes can exceed parse()'s 100-character limit. --- src/format.test.ts | 9 +++++++++ src/index.ts | 8 +++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/format.test.ts b/src/format.test.ts index 8748c2ec..991f2904 100644 --- a/src/format.test.ts +++ b/src/format.test.ts @@ -269,4 +269,13 @@ describe('format avoids scientific notation', () => { expect(format(1e21)).toBe('31688087814y'); expect(format(1e21).includes('e')).toBe(false); }); + + it('expands a positive exponent that String() would keep', () => { + // 1e36 years: Math.round(ms / y) stringifies with e+. The formatted + // result has no exponent. This does not claim a parse() round-trip. + // Expanded Number.MIN_VALUE / MAX_VALUE exceed parse()'s 100-character limit. + const formatted = format(1e36); + expect(formatted.endsWith('y')).toBe(true); + expect(/[eE]/.test(formatted)).toBe(false); + }); }); diff --git a/src/index.ts b/src/index.ts index 92d0a4ec..fc783701 100644 --- a/src/index.ts +++ b/src/index.ts @@ -159,11 +159,13 @@ export function parseStrict(value: StringValue): number { /** - * Format a number without scientific notation so parse() can round-trip it. + * Format a number without scientific notation so common values can round-trip + * through parse(). * * Ordinary values stay on String(), which is what format() used to emit. - * Only an exponent (1e-7, 1e21) is expanded by hand. toLocaleString() is - * avoided: it is hundreds of times slower and locale-sensitive. + * Only an exponent (1e-7, 1e21) is expanded by hand. An expanded value longer + * than 100 characters still has no exponent, but parse() rejects it. + * toLocaleString() is avoided: it is hundreds of times slower and locale-sensitive. */ function numberToDecimalString(n: number): string { if (Object.is(n, -0)) {