timestring
Advanced tools
Comparing version 3.2.0 to 4.0.0
# Changelog | ||
## 4.0.0 | ||
- Drop support for Node.js `< 4.0.0` | ||
- Remove dependency on `lodash` | ||
## 3.2.0 | ||
- Added `mon` unit ([@IanMitchell](https://github.com/IanMitchell)) | ||
- Add `mon` unit ([@IanMitchell](https://github.com/IanMitchell)) | ||
## 3.1.0 | ||
- Added `ms` unit ([@mrbar42](https://github.com/mrbar42)) | ||
- Add `ms` unit ([@mrbar42](https://github.com/mrbar42)) | ||
@@ -11,0 +16,0 @@ ## 3.0.0 |
118
index.js
@@ -1,2 +0,2 @@ | ||
var _ = require('lodash'); | ||
'use strict' | ||
@@ -7,3 +7,3 @@ /** | ||
module.exports = parseTimestring; | ||
module.exports = parseTimestring | ||
@@ -16,8 +16,8 @@ /** | ||
var defaultOpts = { | ||
const DEFAULT_OPTS = { | ||
hoursPerDay: 24, | ||
daysPerWeek: 7, | ||
weeksPerMonth: 4, | ||
monthsPerYear: 12, | ||
}; | ||
monthsPerYear: 12 | ||
} | ||
@@ -30,3 +30,3 @@ /** | ||
var unitMap = { | ||
const UNIT_MAP = { | ||
ms: ['ms', 'milli', 'millisecond', 'milliseconds'], | ||
@@ -38,5 +38,5 @@ s: ['s', 'sec', 'secs', 'second', 'seconds'], | ||
w: ['w', 'week', 'weeks'], | ||
mth: ['mon', 'mth', 'mths','month', 'months'], | ||
y: ['y', 'yr', 'yrs', 'year', 'years'], | ||
}; | ||
mth: ['mon', 'mth', 'mths', 'month', 'months'], | ||
y: ['y', 'yr', 'yrs', 'year', 'years'] | ||
} | ||
@@ -46,32 +46,32 @@ /** | ||
* | ||
* @param {string} string | ||
* @param {string} [returnUnit] | ||
* @param {Object} [opts] | ||
* @return {number} | ||
* @param {String} string | ||
* @param {String} returnUnit | ||
* @param {Object} opts | ||
* @return {Number} | ||
*/ | ||
function parseTimestring(string, returnUnit, opts) { | ||
opts = _.extend(_.clone(defaultOpts), opts || {}); | ||
function parseTimestring (string, returnUnit, opts) { | ||
opts = Object.assign({}, DEFAULT_OPTS, opts || {}) | ||
var totalSeconds = 0; | ||
var unitValues = getUnitValues(opts); | ||
var groups = string | ||
let totalSeconds = 0 | ||
let unitValues = getUnitValues(opts) | ||
let groups = string | ||
.toLowerCase() | ||
.replace(/[^\.\w+-]+/g, '') | ||
.match(/[-+]?[0-9]+[a-z]+/g); | ||
.replace(/[^.\w+-]+/g, '') | ||
.match(/[-+]?[0-9]+[a-z]+/g) | ||
if (groups !== null) { | ||
_.each(groups, function(group) { | ||
var value = group.match(/[0-9]+/g)[0]; | ||
var unit = group.match(/[a-z]+/g)[0]; | ||
groups.forEach(group => { | ||
let value = group.match(/[0-9]+/g)[0] | ||
let unit = group.match(/[a-z]+/g)[0] | ||
totalSeconds += getSeconds(value, unit, unitValues); | ||
}); | ||
totalSeconds += getSeconds(value, unit, unitValues) | ||
}) | ||
} | ||
if (returnUnit) { | ||
return convert(totalSeconds, returnUnit, unitValues); | ||
return convert(totalSeconds, returnUnit, unitValues) | ||
} | ||
return totalSeconds; | ||
return totalSeconds | ||
} | ||
@@ -82,20 +82,20 @@ | ||
* | ||
* @param {Object} opts | ||
* @param {Object} opts | ||
* @returns {Object} | ||
*/ | ||
function getUnitValues(opts) { | ||
var unitValues = { | ||
function getUnitValues (opts) { | ||
let unitValues = { | ||
ms: 0.001, | ||
s: 1, | ||
m: 60, | ||
h: 3600, | ||
}; | ||
h: 3600 | ||
} | ||
unitValues.d = opts.hoursPerDay * unitValues.h; | ||
unitValues.w = opts.daysPerWeek * unitValues.d; | ||
unitValues.mth = opts.weeksPerMonth * unitValues.w; | ||
unitValues.y = opts.monthsPerYear * unitValues.mth; | ||
unitValues.d = opts.hoursPerDay * unitValues.h | ||
unitValues.w = opts.daysPerWeek * unitValues.d | ||
unitValues.mth = opts.weeksPerMonth * unitValues.w | ||
unitValues.y = opts.monthsPerYear * unitValues.mth | ||
return unitValues; | ||
return unitValues | ||
} | ||
@@ -106,16 +106,14 @@ | ||
* | ||
* @param {string} unit | ||
* @returns {string} | ||
* @param {String} unit | ||
* @returns {String} | ||
*/ | ||
function getUnitKey(unit) { | ||
for (var k in unitMap) { | ||
for (var u in unitMap[k]) { | ||
if (unit === unitMap[k][u]) { | ||
return k; | ||
} | ||
function getUnitKey (unit) { | ||
for (let key of Object.keys(UNIT_MAP)) { | ||
if (UNIT_MAP[key].indexOf(unit) > -1) { | ||
return key | ||
} | ||
} | ||
throw new Error('The unit [' + unit + '] is not supported by timestring'); | ||
throw new Error(`The unit [${unit}] is not supported by timestring`) | ||
} | ||
@@ -126,12 +124,10 @@ | ||
* | ||
* @param {number} value | ||
* @param {string} unit | ||
* @param {Object} unitValues | ||
* @returns {number} | ||
* @param {Number} value | ||
* @param {String} unit | ||
* @param {Object} unitValues | ||
* @returns {Number} | ||
*/ | ||
function getSeconds(value, unit, unitValues) { | ||
var baseValue = unitValues[getUnitKey(unit)]; | ||
return value * baseValue; | ||
function getSeconds (value, unit, unitValues) { | ||
return value * unitValues[getUnitKey(unit)] | ||
} | ||
@@ -142,12 +138,10 @@ | ||
* | ||
* @param {number} value | ||
* @param {string} unit | ||
* @param {Object} unitValues | ||
* @returns {number} | ||
* @param {Number} value | ||
* @param {String} unit | ||
* @param {Object} unitValues | ||
* @returns {Number} | ||
*/ | ||
function convert(value, unit, unitValues) { | ||
var baseValue = unitValues[getUnitKey(unit)]; | ||
return value / baseValue; | ||
function convert (value, unit, unitValues) { | ||
return value / unitValues[getUnitKey(unit)] | ||
} |
{ | ||
"name": "timestring", | ||
"version": "3.2.0", | ||
"version": "4.0.0", | ||
"description": "Parse a human readable time string into a time based value", | ||
@@ -9,3 +9,3 @@ "main": "index.js", | ||
"dev": "watch 'clear; npm test -s;' ./ -d", | ||
"sa": "jshint index.js && jscs index.js", | ||
"lint": "standard *.js", | ||
"test": "mocha test.js" | ||
@@ -27,12 +27,7 @@ }, | ||
"duration", | ||
"parse", | ||
"string", | ||
"date" | ||
], | ||
"engines": { | ||
"node": ">=0.10" | ||
"node": ">=4" | ||
}, | ||
"dependencies": { | ||
"lodash": "^4.0.0" | ||
}, | ||
"devDependencies": { | ||
@@ -42,15 +37,8 @@ "chai": "^3.4.1", | ||
"coveralls": "^2.11.2", | ||
"cz-conventional-changelog": "^1.1.5", | ||
"istanbul": "^0.4.1", | ||
"jscs": "^3.0.3", | ||
"jshint": "^2.9.1", | ||
"mocha": "^3.0.0", | ||
"mocha-lcov-reporter": "1.2.0", | ||
"mocha-lcov-reporter": "1.3.0", | ||
"standard": "^10.0.0", | ||
"watch": "^1.0.1" | ||
}, | ||
"config": { | ||
"commitizen": { | ||
"path": "./node_modules/cz-conventional-changelog" | ||
} | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# Timestring | ||
# timestring | ||
@@ -9,3 +9,2 @@ [![Version](https://img.shields.io/npm/v/timestring.svg?style=flat-square)](https://www.npmjs.com/package/timestring) | ||
[![License](https://img.shields.io/github/license/mike182uk/timestring.svg?style=flat-square)](https://www.npmjs.com/package/timestring) | ||
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square)](http://commitizen.github.io/cz-cli/) | ||
@@ -25,8 +24,8 @@ Parse a human readable time string into a time based value. | ||
```js | ||
var timestring = require('timestring'); | ||
const timestring = require('timestring') | ||
var str = '1h 15m'; | ||
var time = timestring(str); | ||
let str = '1h 15m' | ||
let time = timestring(str) | ||
console.log(time); // will log 4500 | ||
console.log(time) // will log 4500 | ||
``` | ||
@@ -39,6 +38,8 @@ | ||
```js | ||
var str = '1d 3h 25m 18s'; | ||
var time = timestring(str); | ||
const timestring = require('timestring') | ||
console.log(time); // will log 98718 | ||
let str = '1d 3h 25m 18s' | ||
let time = timestring(str) | ||
console.log(time) // will log 98718 | ||
``` | ||
@@ -49,6 +50,8 @@ | ||
```js | ||
var str = '1 d 3HOurS 25 min 1 8s'; | ||
var time = timestring(str); | ||
const timestring = require('timestring') | ||
console.log(time); // will log 98718 | ||
let str = '1 d 3HOurS 25 min 1 8s' | ||
let time = timestring(str) | ||
console.log(time) // will log 98718 | ||
``` | ||
@@ -58,3 +61,3 @@ | ||
Timestring will parse the following keywords into time values: | ||
`timestring` will parse the following keywords into time values: | ||
@@ -73,6 +76,8 @@ 1. `ms, milli, millisecond, milliseconds` - will parse to milliseconds | ||
```js | ||
var str = '1day 15h 20minutes 15s'; | ||
var time = timestring(str); | ||
const timestring = require('timestring') | ||
console.log(time); // will log 141615 | ||
let str = '1day 15h 20minutes 15s' | ||
let time = timestring(str) | ||
console.log(time) // will log 141615 | ||
``` | ||
@@ -94,7 +99,13 @@ | ||
```js | ||
var str = '22h 16m'; | ||
const timestring = require('timestring') | ||
var hours = timestring(str, 'h'); // 22.266666666666666 | ||
var days = timestring(str, 'd'); // 0.9277777777777778 | ||
var weeks = timestring(str, 'w'); // 0.13253968253968254 | ||
let str = '22h 16m' | ||
let hours = timestring(str, 'h') | ||
let days = timestring(str, 'd') | ||
let weeks = timestring(str, 'w') | ||
console.log(hours) // will log 22.266666666666666 | ||
console.log(days) // will log 0.9277777777777778 | ||
console.log(weeks) // will log 0.13253968253968254 | ||
``` | ||
@@ -121,10 +132,12 @@ | ||
```js | ||
var str = '1d'; | ||
var opts = { | ||
hoursPerDay: 1 | ||
const timestring = require('timestring') | ||
let str = '1d' | ||
let opts = { | ||
hoursPerDay: 1 | ||
} | ||
var time = timestring(str, 'h', opts); | ||
let time = timestring(str, 'h', opts) | ||
console.log(time); // will log 1 | ||
console.log(time) // will log 1 | ||
``` | ||
@@ -139,12 +152,14 @@ | ||
```js | ||
var opts = { | ||
hoursPerDay: 7.5, | ||
daysPerWeek: 5 | ||
const timestring = require('timestring') | ||
let opts = { | ||
hoursPerDay: 7.5, | ||
daysPerWeek: 5 | ||
} | ||
var hoursToday = timestring('1d', 'h', opts); | ||
var daysThisWeek = timestring('1w', 'd', opts); | ||
let hoursToday = timestring('1d', 'h', opts) | ||
let daysThisWeek = timestring('1w', 'd', opts) | ||
console.log(hoursToday); // will log 7.5 | ||
console.log(daysThisWeek); // will log 5 | ||
console.log(hoursToday) // will log 7.5 | ||
console.log(daysThisWeek) // will log 5 | ||
``` |
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
0
8
157
10456
114
- Removedlodash@^4.0.0
- Removedlodash@4.17.21(transitive)