forked from sindresorhus/to-milliseconds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
30 lines (25 loc) · 856 Bytes
/
index.js
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
29
30
'use strict';
const converters = {
years: value => value * 3154e+10,
months: value => value * 2628e+9,
weeks: value => value * 6048e+8,
days: value => value * 864e5,
hours: value => value * 36e5,
minutes: value => value * 6e4,
seconds: value => value * 1e3,
milliseconds: value => value,
microseconds: value => value / 1e3,
nanoseconds: value => value / 1e6
};
const toMilliseconds = object => Object.entries(object).reduce((milliseconds, [key, value]) => {
if (typeof value !== 'number') {
throw new TypeError(`Expected a \`number\` for key \`${key}\`, got \`${value}\` (${typeof value})`);
}
if (!converters[key]) {
throw new Error('Unsupported time key');
}
return milliseconds + converters[key](value);
}, 0);
module.exports = toMilliseconds;
// TODO: remove this for next major version
module.exports.default = toMilliseconds;