Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/vweevers/bruce-millis-option

Convert various time formats to milliseconds and throw if result is NaN.
https://github.com/vweevers/bruce-millis-option

milliseconds ms nodejs npm-package time

Last synced: 24 days ago
JSON representation

Convert various time formats to milliseconds and throw if result is NaN.

Awesome Lists containing this project

README

        

# bruce-millis-option

**Convert various time formats to milliseconds and throw if result is `NaN`. Like [`bruce-millis`](https://github.com/vweevers/bruce-millis) but dies harder.**

[![npm status](http://img.shields.io/npm/v/bruce-millis-option.svg)](https://www.npmjs.org/package/bruce-millis-option)
[![node](https://img.shields.io/node/v/bruce-millis-option.svg)](https://www.npmjs.org/package/bruce-millis-option)
[![Travis build status](https://img.shields.io/travis/com/vweevers/bruce-millis-option.svg?label=travis)](http://travis-ci.com/vweevers/bruce-millis-option)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

## Usage

```js
const ms = require('bruce-millis-option')

ms(24) // 24
ms('5s') // 5000
ms('1m') // 60000
ms('2h') // 7200000
ms('2.5 hrs') // 9000000
ms('10h') // 36000000
ms('1d') // 86400000
ms('2 days') // 172800000
ms('1y') // 31557600000
ms('-3 days') // -259200000
ms('-1h') // -3600000

ms('invalid') // throws TypeError
```

## API

### `ms(value[, hint])`

Parses `value` with [`bruce-millis`](https://github.com/vweevers/bruce-millis), throws a TypeError if result is `NaN`. A `hint` may be provided to customize the error message:

```js
ms(options.timeout, 'The timeout option')
```

```
$ node example.js
/example/node_modules/bruce-millis-option/index.js:11
throw new TypeError(
^

TypeError: The timeout option must be a number in milliseconds or a string with unit (e.g. '5m')
at module.exports (/example/node_modules/bruce-millis-option/index.js:11:11)
at Object. (/example/example.js:2:1)
at ..
```

## Install

With [npm](https://npmjs.org) do:

```
npm install bruce-millis-option
```

## License

[MIT](LICENSE.md) © 2020-present Vincent Weevers