@sindresorhus/to-milliseconds
Advanced tools
Comparing version 1.0.0 to 1.1.0
53
index.js
'use strict'; | ||
module.exports = object => { | ||
let ms = 0; | ||
const converters = { | ||
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 | ||
}; | ||
for (const [key, value] of Object.entries(object)) { | ||
if (typeof value !== 'number') { | ||
throw new TypeError(`Expected a \`number\` for key \`${key}\`, got \`${value}\` (${typeof value})`); | ||
} | ||
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})`); | ||
} | ||
switch (key) { | ||
case 'days': | ||
ms += value * 864e5; | ||
break; | ||
case 'hours': | ||
ms += value * 36e5; | ||
break; | ||
case 'minutes': | ||
ms += value * 6e4; | ||
break; | ||
case 'seconds': | ||
ms += value * 1e3; | ||
break; | ||
case 'milliseconds': | ||
ms += value; | ||
break; | ||
case 'microseconds': | ||
ms += value / 1e3; | ||
break; | ||
case 'nanoseconds': | ||
ms += value / 1e6; | ||
break; | ||
default: | ||
throw new Error('Unsupported time key'); | ||
} | ||
if (!converters[key]) { | ||
throw new Error('Unsupported time key'); | ||
} | ||
return ms; | ||
}; | ||
return milliseconds + converters[key](value); | ||
}, 0); | ||
module.exports = toMilliseconds; | ||
module.exports.default = toMilliseconds; |
{ | ||
"name": "@sindresorhus/to-milliseconds", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Convert an object of time properties to milliseconds: `{seconds: 2}` → `2000`", | ||
@@ -16,6 +16,7 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "xo && ava" | ||
"test": "xo && ava && tsd-check" | ||
}, | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
@@ -39,5 +40,6 @@ "keywords": [ | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
"ava": "^1.3.1", | ||
"tsd-check": "^0.5.0", | ||
"xo": "^0.24.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4020
5
3
34