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

describe('ms(string)', () => {
it('should not throw an error', () => {
Expand Down Expand Up @@ -329,6 +329,29 @@ describe('ms(number)', () => {
});
});

// roundtrip

describe('ms(roundtrip)', () => {
it('should roundtrip for common values', () => {
expect(ms(ms('1s'))).toBe('1s');
expect(ms(ms('1m'))).toBe('1m');
expect(ms(ms('1h'))).toBe('1h');
expect(ms(ms('1d'))).toBe('1d');
expect(ms(ms('1w'))).toBe('1w');
expect(ms(ms('1y'))).toBe('1y');
});

it('should not return NaN for format() output with large numbers', () => {
const formatted = ms(Number.MAX_SAFE_INTEGER) as StringValue;
expect(Number.isNaN(ms(formatted))).toBe(false);
});

it('should not return NaN for format() output with large negative numbers', () => {
const formatted = ms(-Number.MAX_SAFE_INTEGER) as StringValue;
expect(Number.isNaN(ms(formatted))).toBe(false);
});
});

// invalid inputs

describe('ms(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
23 changes: 22 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 @@ -142,6 +142,27 @@ describe('parse(long string)', () => {
});
});

// scientific notation

describe('parse(scientific notation)', () => {
it('should handle scientific notation in number part', () => {
expect(parse('1e3ms')).toBe(1000);
expect(parse('1.5e2ms')).toBe(150);
expect(parse('1e+3ms')).toBe(1000);
expect(parse('1e-3ms')).toBe(0.001);
});

it('should handle format() output for very large values', () => {
const formatted = format(Number.MAX_SAFE_INTEGER);
expect(Number.isNaN(parse(formatted))).toBe(false);
});

it('should handle format() output for very small negative values', () => {
const formatted = format(-Number.MAX_SAFE_INTEGER);
expect(Number.isNaN(parse(formatted))).toBe(false);
});
});

// invalid inputs

describe('parse(invalid inputs)', () => {
Expand Down