Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@sindresorhus/to-milliseconds

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sindresorhus/to-milliseconds - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

index.d.ts

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"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc