All files / src negate.ts

100% Statements 8/8
100% Branches 2/2
100% Functions 2/2
100% Lines 7/7

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 2314x   14x                 14x 39x   37x 296x         37x    
import { UNITS } from './lib/units';
import { Duration, DurationInput } from './types';
import { parse } from './parse';
 
/**
 * Gets the negative of the input duration.
 *
 * @example
 * negate({ days: -1 }) // { days: 1 }
 * negate({ days: -1, hours 2 }) // { days: 1, hours: -2 }
 */
export const negate = (duration: DurationInput): Duration => {
	const output = { ...parse(duration) };
 
	UNITS.forEach(unit => {
		output[unit] = output[unit] === 0
			? 0
			: -output[unit];
	});
 
	return output;
};