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
4 changes: 4 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ describe('ms(number)', () => {

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

it('should roundtrip very large values that format in scientific notation', () => {
expect(ms(ms(Number.MAX_VALUE))).toBe(Number.MAX_VALUE);
});
});

// 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
16 changes: 15 additions & 1 deletion src/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from '@jest/globals';
import { parse } from './index';
import { format, parse } from './index';

describe('parse(string)', () => {
it('should not throw an error', () => {
Expand Down Expand Up @@ -66,6 +66,20 @@ describe('parse(string)', () => {
expect(parse('.5ms')).toBe(0.5);
});

it('should parse scientific notation values', () => {
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);
});

it('should roundtrip format output with scientific notation', () => {
const formatted = format(Number.MAX_VALUE);
expect(formatted).toMatch(/e[+-]\d+y$/i);
expect(parse(formatted)).toBe(Number.MAX_VALUE);
});

it('should work with negative integers', () => {
expect(parse('-100ms')).toBe(-100);
});
Expand Down