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: 25 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,28 @@ describe('ms(invalid inputs)', () => {
}).toThrow();
});
});

//multiple units
describe('ms(multipleunits)', () => {
it('should not throw an error, when ms(hasmorethanoneunit)', () => {
expect(() => {
ms('3y40s');
}).not.toThrow();
expect(() => {
ms('3y 40s');
}).not.toThrow();
expect(() => {
ms('3 y 40 s');
}).not.toThrow();
});

it('should convert all values into ms', () => {
expect(ms('1 h 45 s')).toBe(3645000);
expect(ms('3seconds40s')).toBe(43000);
expect(ms('1m1mo1ms')).toBe(2629860001);
});

it('should return NaN if invalid', () => {
expect(Number.isNaN(ms('3h2failure'))).toBe(true);
});
});
31 changes: 29 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ type UnitAnyCase = Capitalize<Unit> | Uppercase<Unit> | Unit;
export type StringValue =
| `${number}`
| `${number}${UnitAnyCase}`
| `${number} ${UnitAnyCase}`;
| `${number} ${UnitAnyCase}`
| `${`${number}${Years}` | ``}${`${number}${Months}` | ``}${`${number}${Weeks}` | ``}${`${number}${Days}` | ``}${`${number}${Hours}` | ``}${`${number}${Minutes}` | ``}${`${number}${Seconds}` | ``}${`${number}${Milliseconds}` | `${number}` | ``}`
| `${`${number} ${Years}` | ``} ${`${number} ${Months}` | ``} ${`${number} ${Weeks}` | ``} ${`${number} ${Days}` | ``} ${`${number} ${Hours}` | ``} ${`${number} ${Minutes}` | ``} ${`${number} ${Seconds}` | ``} ${`${number} ${Milliseconds}` | `${number}` | ``}`
| `${`${number}${Years}` | ``} ${`${number}${Months}` | ``} ${`${number}${Weeks}` | ``} ${`${number}${Days}` | ``} ${`${number}${Hours}` | ``} ${`${number}${Minutes}` | ``} ${`${number}${Seconds}` | ``} ${`${number}${Milliseconds}` | `${number}` | ``}`;

interface Options {
/**
Expand Down Expand Up @@ -80,7 +83,7 @@ export function parse(str: string): number {
);

if (!match?.groups) {
return NaN;
return multipleUnits(str);
}

// Named capture groups need to be manually typed today.
Expand Down Expand Up @@ -157,6 +160,30 @@ export function parseStrict(value: StringValue): number {
return parse(value);
}

/**
* Parse the given string with multiple time units and return milliseconds.
*
* @param value - A typesafe StringValue comprised of multiple strings to convert
* into milliseconds
* @returns The sum of all parsed strings value in milliseconds, or `NaN` if the
* string can't be parsed
*/

function multipleUnits(value: string): number {
const regEx =
/\d*\.?\d+ *(?:milliseconds?|msecs?|ms|seconds?|secs?|s|months?|mo|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)/gi;

const match = [...value.matchAll(regEx)].flat();
const unmatchedString = value.replaceAll(regEx, '');
const spaceRegEx = /^ *$/;

if (match.length === 0 || spaceRegEx.exec(unmatchedString) === null) {
return NaN;
}

return match.reduce((accumulator, unit) => parse(unit) + accumulator, 0);
}

/**
* Short format for `ms`.
*/
Expand Down
20 changes: 20 additions & 0 deletions src/parse-strict.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,23 @@ describe('parseStrict(invalid inputs)', () => {
}).toThrow();
});
});

//multiple units
describe('ms(multipleunits)', () => {
it('should not throw an error, when ms(hasmorethanoneunit)', () => {
expect(() => {
parseStrict('3y40s');
}).not.toThrow();
expect(() => {
parseStrict('3y 40s');
}).not.toThrow();
expect(() => {
parseStrict('3 y 40 s');
}).not.toThrow();
});

it('should convert all values into ms', () => {
expect(parseStrict('1 h 45 s')).toBe(3645000);
expect(parseStrict('3seconds40s')).toBe(43000);
});
});
24 changes: 24 additions & 0 deletions src/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,27 @@ describe('parse(invalid inputs)', () => {
}).toThrow();
});
});

//multiple units
describe('ms(multipleunits)', () => {
it('should not throw an error, when ms(hasmorethanoneunit)', () => {
expect(() => {
parse('3y40s');
}).not.toThrow();
expect(() => {
parse('3y 40s');
}).not.toThrow();
expect(() => {
parse('3 y 40 s');
}).not.toThrow();
});

it('should convert all values into ms', () => {
expect(parse('1 h 45 s')).toBe(3645000);
expect(parse('3seconds40s')).toBe(43000);
});

it('should return NaN if invalid', () => {
expect(Number.isNaN(parse('3h2failure'))).toBe(true);
});
});