@sindresorhus/to-milliseconds
Advanced tools
Comparing version 1.2.0 to 2.0.0
@@ -1,41 +0,32 @@ | ||
declare namespace toMilliseconds { | ||
interface TimeDescriptor { | ||
readonly days?: number; | ||
readonly hours?: number; | ||
readonly minutes?: number; | ||
readonly seconds?: number; | ||
readonly milliseconds?: number; | ||
readonly microseconds?: number; | ||
readonly nanoseconds?: number; | ||
} | ||
export interface TimeDescriptor { | ||
readonly days?: number; | ||
readonly hours?: number; | ||
readonly minutes?: number; | ||
readonly seconds?: number; | ||
readonly milliseconds?: number; | ||
readonly microseconds?: number; | ||
readonly nanoseconds?: number; | ||
} | ||
declare const toMilliseconds: { | ||
/** | ||
Convert an object of time properties to milliseconds: `{seconds: 2}` → `2000`. | ||
/** | ||
Convert an object of time properties to milliseconds: `{seconds: 2}` → `2000`. | ||
@example | ||
``` | ||
import toMilliseconds = require('@sindresorhus/to-milliseconds'); | ||
@example | ||
``` | ||
import toMilliseconds from '@sindresorhus/to-milliseconds'; | ||
toMilliseconds({ | ||
days: 15, | ||
hours: 11, | ||
minutes: 23, | ||
seconds: 20, | ||
milliseconds: 1 | ||
}); | ||
//=> 1337000001 | ||
toMilliseconds({ | ||
days: 15, | ||
hours: 11, | ||
minutes: 23, | ||
seconds: 20, | ||
milliseconds: 1 | ||
}); | ||
//=> 1337000001 | ||
setTimeout(() => { | ||
// … | ||
}, toMilliseconds({minutes: 2})); | ||
``` | ||
*/ | ||
(timeDescriptor: toMilliseconds.TimeDescriptor): number; | ||
// TODO: remove this for next major version | ||
default: typeof toMilliseconds; | ||
}; | ||
export = toMilliseconds; | ||
setTimeout(() => { | ||
// … | ||
}, toMilliseconds({minutes: 2})); | ||
``` | ||
*/ | ||
export default function toMilliseconds(timeDescriptor: TimeDescriptor): number; |
30
index.js
@@ -1,3 +0,1 @@ | ||
'use strict'; | ||
const converters = { | ||
@@ -13,16 +11,20 @@ days: value => value * 864e5, | ||
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})`); | ||
} | ||
export default function toMilliseconds(timeDescriptor) { | ||
let totalMilliseconds = 0; | ||
if (!converters[key]) { | ||
throw new Error('Unsupported time key'); | ||
for (const [key, value] of Object.entries(timeDescriptor)) { | ||
if (typeof value !== 'number') { | ||
throw new TypeError(`Expected a \`number\` for key \`${key}\`, got \`${value}\` (${typeof value})`); | ||
} | ||
const converter = converters[key]; | ||
if (!converter) { | ||
throw new Error(`Unsupported time key: ${key}`); | ||
} | ||
totalMilliseconds += converter(value); | ||
} | ||
return milliseconds + converters[key](value); | ||
}, 0); | ||
module.exports = toMilliseconds; | ||
// TODO: remove this for next major version | ||
module.exports.default = toMilliseconds; | ||
return totalMilliseconds; | ||
} |
{ | ||
"name": "@sindresorhus/to-milliseconds", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "Convert an object of time properties to milliseconds: `{seconds: 2}` → `2000`", | ||
"license": "MIT", | ||
"repository": "sindresorhus/to-milliseconds", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=12" | ||
}, | ||
@@ -39,6 +42,6 @@ "scripts": { | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.1", | ||
"xo": "^0.24.0" | ||
"ava": "^3.15.0", | ||
"tsd": "^0.14.0", | ||
"xo": "^0.39.1" | ||
} | ||
} |
@@ -1,6 +0,5 @@ | ||
# to-milliseconds [![Build Status](https://travis-ci.org/sindresorhus/to-milliseconds.svg?branch=master)](https://travis-ci.org/sindresorhus/to-milliseconds) | ||
# to-milliseconds | ||
> Convert an object of time properties to milliseconds: `{seconds: 2}` → `2000` | ||
## Install | ||
@@ -12,7 +11,6 @@ | ||
## Usage | ||
```js | ||
const toMilliseconds = require('@sindresorhus/to-milliseconds'); | ||
import toMilliseconds from '@sindresorhus/to-milliseconds'; | ||
@@ -33,10 +31,9 @@ toMilliseconds({ | ||
## API | ||
### toMilliseconds(input) | ||
### toMilliseconds(timeDescriptor) | ||
#### input | ||
#### timeDescriptor | ||
Type: `Object` | ||
Type: `object` | ||
@@ -53,3 +50,2 @@ Specify an object with any of the following properties: | ||
## Related | ||
@@ -59,6 +55,1 @@ | ||
- [pretty-ms](https://github.com/sindresorhus/pretty-ms) - Convert milliseconds to a human readable string | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
Sorry, the diff of this file is not supported yet
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
Yes
4216
51
52