diff --git a/src/format.test.ts b/src/format.test.ts index cdabb0f6..991f2904 100644 --- a/src/format.test.ts +++ b/src/format.test.ts @@ -259,3 +259,23 @@ 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); + }); + + 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 d50e3c77..fc783701 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,70 @@ export function parseStrict(value: StringValue): number { return parse(value); } + +/** + * 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. 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)) { + return '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 `${sign}${digits.slice(0, point)}.${digits.slice(point)}`; +} + /** * 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 +249,7 @@ function fmtLong(ms: number): StringValue { if (msAbs >= s) { return plural(ms, msAbs, s, 'second'); } - return `${ms} ms`; + return `${numberToDecimalString(ms)} ms`; } /** @@ -240,5 +277,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; }