timestring
Advanced tools
Comparing version 6.0.0 to 7.0.0
# Changelog | ||
## 7.0.0 | ||
- Drop support for Node.js `< 12.0.0` | ||
- If a numeric value is provided it is trated as a string containing milliseconds ([alexeymarunin](https://github.com/alexeymarunin)) | ||
## 6.0.0 | ||
@@ -4,0 +9,0 @@ |
44
index.js
@@ -41,14 +41,18 @@ /** | ||
* | ||
* @param {String} string | ||
* @param {String} returnUnit | ||
* @param {Object} opts | ||
* @return {Number} | ||
* @param {string|number} value | ||
* @param {string} returnUnit | ||
* @param {Object} opts | ||
* @returns {number} | ||
*/ | ||
function parseTimestring (string, returnUnit, opts) { | ||
function parseTimestring (value, returnUnit, opts) { | ||
opts = Object.assign({}, DEFAULT_OPTS, opts || {}) | ||
if (typeof value === 'number' || value.match(/^[-+]?[0-9.]+$/g)) { | ||
value = parseInt(value) + 'ms' | ||
} | ||
let totalSeconds = 0 | ||
let unitValues = getUnitValues(opts) | ||
let groups = string | ||
const unitValues = getUnitValues(opts) | ||
const groups = value | ||
.toLowerCase() | ||
@@ -59,8 +63,8 @@ .replace(/[^.\w+-]+/g, '') | ||
if (groups === null) { | ||
throw new Error(`The string [${string}] could not be parsed by timestring`) | ||
throw new Error(`The value [${value}] could not be parsed by timestring`) | ||
} | ||
groups.forEach(group => { | ||
let value = group.match(/[0-9.]+/g)[0] | ||
let unit = group.match(/[a-z]+/g)[0] | ||
const value = group.match(/[0-9.]+/g)[0] | ||
const unit = group.match(/[a-z]+/g)[0] | ||
@@ -85,3 +89,3 @@ totalSeconds += getSeconds(value, unit, unitValues) | ||
function getUnitValues (opts) { | ||
let unitValues = { | ||
const unitValues = { | ||
ms: 0.001, | ||
@@ -104,8 +108,8 @@ s: 1, | ||
* | ||
* @param {String} unit | ||
* @returns {String} | ||
* @param {string} unit | ||
* @returns {string} | ||
*/ | ||
function getUnitKey (unit) { | ||
for (let key of Object.keys(UNIT_MAP)) { | ||
for (const key of Object.keys(UNIT_MAP)) { | ||
if (UNIT_MAP[key].indexOf(unit) > -1) { | ||
@@ -122,6 +126,6 @@ return key | ||
* | ||
* @param {Number} value | ||
* @param {String} unit | ||
* @param {number} value | ||
* @param {string} unit | ||
* @param {Object} unitValues | ||
* @returns {Number} | ||
* @returns {number} | ||
*/ | ||
@@ -136,6 +140,6 @@ | ||
* | ||
* @param {Number} value | ||
* @param {String} unit | ||
* @param {number} value | ||
* @param {string} unit | ||
* @param {Object} unitValues | ||
* @returns {Number} | ||
* @returns {number} | ||
*/ | ||
@@ -142,0 +146,0 @@ |
{ | ||
"name": "timestring", | ||
"version": "6.0.0", | ||
"version": "7.0.0", | ||
"description": "Parse a human readable time string into a time based value", | ||
@@ -29,13 +29,12 @@ "main": "index.js", | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=12" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.2.0", | ||
"coveralls": "^3.0.3", | ||
"chai": "^4.3.7", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^6.1.4", | ||
"mocha": "^10.2.0", | ||
"mocha-lcov-reporter": "1.3.0", | ||
"standard": "^12.0.1", | ||
"standard": "^17.0.0", | ||
"watch": "^1.0.2" | ||
} | ||
} |
# timestring | ||
[![Version](https://img.shields.io/npm/v/timestring.svg?style=flat-square)](https://www.npmjs.com/package/timestring) | ||
[![Build Status](https://img.shields.io/travis/mike182uk/timestring.svg?style=flat-square)](http://travis-ci.org/mike182uk/timestring) | ||
[![Coveralls](https://img.shields.io/coveralls/mike182uk/timestring/master.svg?style=flat-square)](https://coveralls.io/r/mike182uk/timestring) | ||
[![Build Status](https://img.shields.io/github/actions/workflow/status/mike182uk/timestring/ci.yml?branch=main&style=flat-square)](https://github.com/mike182uk/timestring/actions/workflows/ci.yml?query=workflow%3ACI) | ||
[![Coveralls](https://img.shields.io/coveralls/mike182uk/timestring/main.svg?style=flat-square)](https://coveralls.io/r/mike182uk/timestring) | ||
[![npm](https://img.shields.io/npm/dm/timestring.svg?style=flat-square)](https://www.npmjs.com/package/timestring) | ||
@@ -171,1 +171,9 @@ [![License](https://img.shields.io/github/license/mike182uk/timestring.svg?style=flat-square)](https://www.npmjs.com/package/timestring) | ||
``` | ||
If a number is passed into `timestring` it will be treated as a string containing milliseconds: | ||
```js | ||
const timestring = require('timestring') | ||
let time = timestring(3000) // 3s | ||
``` |
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
11863
6
118
179