Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,13 @@ describe('ms(number)', () => {

expect(ms(-234234234)).toBe('-3d');
});

it('should roundtrip format/parse for large numbers', () => {
const formatted = ms(Number.MAX_VALUE);
expect(typeof formatted).toBe('string');
const parsed = ms(formatted as `${number}`);
expect(parsed).not.toBeNaN();
});
});

// invalid inputs
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function parse(str: string): number {
);
}
const match =
/^(?<value>-?\d*\.?\d+) *(?<unit>milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|months?|mo|years?|yrs?|y)?$/i.exec(
/^(?<value>-?(?:\d*\.?\d+)(?:e[+-]?\d+)?) *(?<unit>milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|months?|mo|years?|yrs?|y)?$/i.exec(
str,
);

Expand Down
9 changes: 9 additions & 0 deletions src/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ describe('parse(string)', () => {
it('should work with negative decimals starting with "."', () => {
expect(parse('-.5h')).toBe(-1800000);
});

it('should work with scientific notation (roundtrip from format)', () => {
expect(parse('5.696545792019405e+297y')).not.toBeNaN();
expect(parse('1e3ms')).toBe(1000);
expect(parse('1.5e2s')).toBe(150000);
expect(parse('-1e3ms')).toBe(-1000);
expect(parse('1E3ms')).toBe(1000);
expect(parse('1e+3ms')).toBe(1000);
});
});

// long strings
Expand Down