4.0.0
This commit is contained in:
parent
be12014b8b
commit
9ae3e78a4b
@ -2,8 +2,6 @@ language: node_js
|
||||
|
||||
node_js:
|
||||
- stable
|
||||
- v0.10
|
||||
- v0.12
|
||||
- v4
|
||||
- v5
|
||||
- v6
|
||||
|
@ -1,5 +1,10 @@
|
||||
# Changelog
|
||||
|
||||
## 4.0.0
|
||||
|
||||
- Drop support for Node.js `< 4.0.0`
|
||||
- Remove dependency on `lodash`
|
||||
|
||||
## 3.2.0
|
||||
|
||||
- Add `mon` unit ([@IanMitchell](https://github.com/IanMitchell))
|
||||
|
64
README.md
64
README.md
@ -1,4 +1,4 @@
|
||||
# Timestring
|
||||
# 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)
|
||||
@ -20,10 +20,10 @@ npm install --save timestring
|
||||
### Overview
|
||||
|
||||
```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
|
||||
```
|
||||
@ -33,8 +33,10 @@ console.log(time) // will log 4500
|
||||
The time string can contain as many time groups as needed:
|
||||
|
||||
```js
|
||||
var str = '1d 3h 25m 18s'
|
||||
var time = timestring(str)
|
||||
const timestring = require('timestring')
|
||||
|
||||
let str = '1d 3h 25m 18s'
|
||||
let time = timestring(str)
|
||||
|
||||
console.log(time) // will log 98718
|
||||
```
|
||||
@ -42,15 +44,17 @@ console.log(time) // will log 98718
|
||||
and can be as messy as you like:
|
||||
|
||||
```js
|
||||
var str = '1 d 3HOurS 25 min 1 8s'
|
||||
var time = timestring(str)
|
||||
const timestring = require('timestring')
|
||||
|
||||
let str = '1 d 3HOurS 25 min 1 8s'
|
||||
let time = timestring(str)
|
||||
|
||||
console.log(time) // will log 98718
|
||||
```
|
||||
|
||||
### Keywords
|
||||
|
||||
Timestring will parse the following keywords into time values:
|
||||
`timestring` will parse the following keywords into time values:
|
||||
|
||||
1. `ms, milli, millisecond, milliseconds` - will parse to milliseconds
|
||||
2. `s, sec, secs, second, seconds` - will parse to seconds
|
||||
@ -64,8 +68,10 @@ Timestring will parse the following keywords into time values:
|
||||
Keywords can be used interchangeably:
|
||||
|
||||
```js
|
||||
var str = '1day 15h 20minutes 15s'
|
||||
var time = timestring(str)
|
||||
const timestring = require('timestring')
|
||||
|
||||
let str = '1day 15h 20minutes 15s'
|
||||
let time = timestring(str)
|
||||
|
||||
console.log(time) // will log 141615
|
||||
```
|
||||
@ -84,11 +90,17 @@ By default the return time value will be in seconds. This can be changed by pass
|
||||
8. `y` - Years
|
||||
|
||||
```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
|
||||
```
|
||||
|
||||
### Optional Configuration
|
||||
@ -110,12 +122,14 @@ The following options are configurable:
|
||||
4. `monthsPerYear`
|
||||
|
||||
```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
|
||||
```
|
||||
@ -127,13 +141,15 @@ This would be useful for specific application needs.
|
||||
*Example - Employees of my company work 7.5 hours a day, and only work 5 days a week. In my time tracking app, when they type `1d` i want 7.5 hours to be tracked. When they type `1w` i want 5 days to be tracked etc.*
|
||||
|
||||
```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
|
||||
|
70
index.js
70
index.js
@ -1,4 +1,4 @@
|
||||
var _ = require('lodash')
|
||||
'use strict'
|
||||
|
||||
/**
|
||||
* Exports
|
||||
@ -12,7 +12,7 @@ module.exports = parseTimestring
|
||||
* @type {Object}
|
||||
*/
|
||||
|
||||
var defaultOpts = {
|
||||
const DEFAULT_OPTS = {
|
||||
hoursPerDay: 24,
|
||||
daysPerWeek: 7,
|
||||
weeksPerMonth: 4,
|
||||
@ -25,7 +25,7 @@ var defaultOpts = {
|
||||
* @type {Object}
|
||||
*/
|
||||
|
||||
var unitMap = {
|
||||
const UNIT_MAP = {
|
||||
ms: ['ms', 'milli', 'millisecond', 'milliseconds'],
|
||||
s: ['s', 'sec', 'secs', 'second', 'seconds'],
|
||||
m: ['m', 'min', 'mins', 'minute', 'minutes'],
|
||||
@ -39,26 +39,26 @@ var unitMap = {
|
||||
/**
|
||||
* Parse a timestring
|
||||
*
|
||||
* @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 || {})
|
||||
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)
|
||||
|
||||
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)
|
||||
})
|
||||
@ -74,12 +74,12 @@ function parseTimestring (string, returnUnit, opts) {
|
||||
/**
|
||||
* Get unit values based on the passed options
|
||||
*
|
||||
* @param {Object} opts
|
||||
* @param {Object} opts
|
||||
* @returns {Object}
|
||||
*/
|
||||
|
||||
function getUnitValues (opts) {
|
||||
var unitValues = {
|
||||
let unitValues = {
|
||||
ms: 0.001,
|
||||
s: 1,
|
||||
m: 60,
|
||||
@ -97,48 +97,42 @@ function getUnitValues (opts) {
|
||||
/**
|
||||
* Get the key for a unit
|
||||
*
|
||||
* @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
|
||||
}
|
||||
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`)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of seconds for a value, based on the unit
|
||||
*
|
||||
* @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
|
||||
return value * unitValues[getUnitKey(unit)]
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a value from its existing unit to a new unit
|
||||
*
|
||||
* @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
|
||||
return value / unitValues[getUnitKey(unit)]
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "timestring",
|
||||
"version": "3.2.0",
|
||||
"version": "4.0.0",
|
||||
"description": "Parse a human readable time string into a time based value",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
@ -23,15 +23,10 @@
|
||||
"time",
|
||||
"timestring",
|
||||
"duration",
|
||||
"parse",
|
||||
"string",
|
||||
"date"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=0.10"
|
||||
},
|
||||
"dependencies": {
|
||||
"lodash": "^4.0.0"
|
||||
"node": ">=4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"chai": "^3.4.1",
|
||||
|
60
test.js
60
test.js
@ -1,12 +1,14 @@
|
||||
'use strict'
|
||||
|
||||
/* globals describe, it */
|
||||
|
||||
var chai = require('chai')
|
||||
var expect = chai.expect
|
||||
const chai = require('chai')
|
||||
const timestring = require('./index')
|
||||
|
||||
var timestring = require('./index')
|
||||
const expect = chai.expect
|
||||
|
||||
describe('timestring', function () {
|
||||
it('can parse a timestring', function () {
|
||||
describe('timestring', () => {
|
||||
it('can parse a timestring', () => {
|
||||
expect(timestring('500ms')).to.equal(0.5)
|
||||
expect(timestring('1s')).to.equal(1)
|
||||
expect(timestring('1m')).to.equal(60)
|
||||
@ -17,8 +19,8 @@ describe('timestring', function () {
|
||||
expect(timestring('1y')).to.equal(29030400)
|
||||
})
|
||||
|
||||
it('can parse different unit identifiers', function () {
|
||||
var unitMap = {
|
||||
it('can parse different unit identifiers', () => {
|
||||
let unitMap = {
|
||||
ms: ['ms', 'milli', 'millisecond', 'milliseconds'],
|
||||
s: ['s', 'sec', 'secs', 'second', 'seconds'],
|
||||
m: ['m', 'min', 'mins', 'minute', 'minutes'],
|
||||
@ -29,47 +31,47 @@ describe('timestring', function () {
|
||||
y: ['y', 'yr', 'yrs', 'year', 'years']
|
||||
}
|
||||
|
||||
unitMap.ms.forEach(function (msUnit) {
|
||||
expect(timestring('500 ' + msUnit)).to.equal(0.5)
|
||||
unitMap.ms.forEach(unit => {
|
||||
expect(timestring(`500 ${unit}`)).to.equal(0.5)
|
||||
})
|
||||
|
||||
unitMap.s.forEach(function (sUnit) {
|
||||
expect(timestring('3 ' + sUnit)).to.equal(3)
|
||||
unitMap.s.forEach(unit => {
|
||||
expect(timestring(`3 ${unit}`)).to.equal(3)
|
||||
})
|
||||
|
||||
unitMap.m.forEach(function (mUnit) {
|
||||
expect(timestring('2 ' + mUnit)).to.equal(120)
|
||||
unitMap.m.forEach(unit => {
|
||||
expect(timestring(`2 ${unit}`)).to.equal(120)
|
||||
})
|
||||
|
||||
unitMap.h.forEach(function (hUnit) {
|
||||
expect(timestring('7 ' + hUnit)).to.equal(25200)
|
||||
unitMap.h.forEach(unit => {
|
||||
expect(timestring(`7 ${unit}`)).to.equal(25200)
|
||||
})
|
||||
|
||||
unitMap.d.forEach(function (dUnit) {
|
||||
expect(timestring('4 ' + dUnit)).to.equal(345600)
|
||||
unitMap.d.forEach(unit => {
|
||||
expect(timestring(`4 ${unit}`)).to.equal(345600)
|
||||
})
|
||||
|
||||
unitMap.w.forEach(function (wUnit) {
|
||||
expect(timestring('2 ' + wUnit)).to.equal(1209600)
|
||||
unitMap.w.forEach(unit => {
|
||||
expect(timestring(`2 ${unit}`)).to.equal(1209600)
|
||||
})
|
||||
|
||||
unitMap.mth.forEach(function (mthUnit) {
|
||||
expect(timestring('9 ' + mthUnit)).to.equal(21772800)
|
||||
unitMap.mth.forEach(unit => {
|
||||
expect(timestring(`9 ${unit}`)).to.equal(21772800)
|
||||
})
|
||||
|
||||
unitMap.y.forEach(function (yUnit) {
|
||||
expect(timestring('1 ' + yUnit)).to.equal(29030400)
|
||||
unitMap.y.forEach(unit => {
|
||||
expect(timestring(`1 ${unit}`)).to.equal(29030400)
|
||||
})
|
||||
})
|
||||
|
||||
it('can return a value in a specified unit', function () {
|
||||
it('can return a value in a specified unit', () => {
|
||||
expect(timestring('1m', 'ms')).to.equal(60000)
|
||||
expect(timestring('5m', 's')).to.equal(300)
|
||||
expect(timestring('5m', 'm')).to.equal(5)
|
||||
})
|
||||
|
||||
it('uses the passed options instead of the defaults', function () {
|
||||
var opts = {
|
||||
it('uses the passed options instead of the defaults', () => {
|
||||
let opts = {
|
||||
hoursPerDay: 1,
|
||||
daysPerWeek: 2,
|
||||
weeksPerMonth: 3,
|
||||
@ -82,11 +84,11 @@ describe('timestring', function () {
|
||||
expect(timestring('1y', 'mth', opts)).to.equal(4)
|
||||
})
|
||||
|
||||
it('throws an error when an invalid unit is used in the timestring', function () {
|
||||
expect(function () { timestring('1g') }).to.throw(Error)
|
||||
it('throws an error when an invalid unit is used in the timestring', () => {
|
||||
expect(() => timestring('1g')).to.throw(Error)
|
||||
})
|
||||
|
||||
it('can parse a messy time string', function () {
|
||||
it('can parse a messy time string', () => {
|
||||
expect(timestring('5 D a YS 4 h 2 0 mI nS')).to.equal(447600)
|
||||
})
|
||||
})
|
||||
|
21
yarn.lock
21
yarn.lock
@ -609,6 +609,10 @@ esprima@2.7.x, esprima@^2.6.0, esprima@^2.7.1:
|
||||
version "2.7.3"
|
||||
resolved "https://registry.yarnpkg.com/esprima/-/esprima-2.7.3.tgz#96e3b70d5779f6ad49cd032673d1c312767ba581"
|
||||
|
||||
esprima@^3.1.1:
|
||||
version "3.1.3"
|
||||
resolved "https://registry.yarnpkg.com/esprima/-/esprima-3.1.3.tgz#fdca51cee6133895e3c88d535ce49dbff62a4633"
|
||||
|
||||
esquery@^1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/esquery/-/esquery-1.0.0.tgz#cfba8b57d7fba93f17298a8a006a04cda13d80fa"
|
||||
@ -1020,13 +1024,20 @@ js-tokens@^3.0.0:
|
||||
version "3.0.1"
|
||||
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.1.tgz#08e9f132484a2c45a30907e9dc4d5567b7f114d7"
|
||||
|
||||
js-yaml@3.6.1, js-yaml@3.x, js-yaml@^3.5.1:
|
||||
js-yaml@3.6.1:
|
||||
version "3.6.1"
|
||||
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.6.1.tgz#6e5fe67d8b205ce4d22fad05b7781e8dadcc4b30"
|
||||
dependencies:
|
||||
argparse "^1.0.7"
|
||||
esprima "^2.6.0"
|
||||
|
||||
js-yaml@3.x, js-yaml@^3.5.1:
|
||||
version "3.8.3"
|
||||
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.8.3.tgz#33a05ec481c850c8875929166fe1beb61c728766"
|
||||
dependencies:
|
||||
argparse "^1.0.7"
|
||||
esprima "^3.1.1"
|
||||
|
||||
jsbn@~0.1.0:
|
||||
version "0.1.1"
|
||||
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513"
|
||||
@ -1668,7 +1679,7 @@ strip-json-comments@~2.0.1:
|
||||
version "2.0.1"
|
||||
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"
|
||||
|
||||
supports-color@3.1.2, supports-color@^3.1.0:
|
||||
supports-color@3.1.2:
|
||||
version "3.1.2"
|
||||
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-3.1.2.tgz#72a262894d9d408b956ca05ff37b2ed8a6e2a2d5"
|
||||
dependencies:
|
||||
@ -1678,6 +1689,12 @@ supports-color@^2.0.0:
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7"
|
||||
|
||||
supports-color@^3.1.0:
|
||||
version "3.2.3"
|
||||
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-3.2.3.tgz#65ac0504b3954171d8a64946b2ae3cbb8a5f54f6"
|
||||
dependencies:
|
||||
has-flag "^1.0.0"
|
||||
|
||||
table@^3.7.8:
|
||||
version "3.8.3"
|
||||
resolved "https://registry.yarnpkg.com/table/-/table-3.8.3.tgz#2bbc542f0fda9861a755d3947fefd8b3f513855f"
|
||||
|
Loading…
Reference in New Issue
Block a user