All files / src parse.ts

100% Statements 15/15
100% Branches 2/2
100% Functions 2/2
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 2914x   14x 14x 14x   14x 474x 119x     355x 60x     295x           14x 474x   459x   434x    
import { ZERO } from './lib/units';
import { Duration, DurationInput } from './types';
import { parseISODuration } from './lib/parseISODuration';
import { validate } from './lib/validate';
import { cleanDurationObject } from './lib/cleanDurationObject';
 
const baseParse = (duration: DurationInput): Duration => {
	if (typeof duration === 'string') {
		return parseISODuration(duration);
	}
 
	if (typeof duration === 'number') {
		return { ...ZERO, milliseconds: duration };
	}
 
	return { ...ZERO, ...duration };
};
 
/**
 * Parse various duration formats to a simple suration object.
 */
export const parse = (duration: DurationInput): Duration => {
	const output = baseParse(duration);
 
	validate(output);
 
	return cleanDurationObject(output);
};