Skip to content

Commit

Permalink
4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mike182uk committed Apr 22, 2017
1 parent be12014 commit 9ae3e78
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 102 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Expand Up @@ -2,8 +2,6 @@ language: node_js

node_js:
- stable
- v0.10
- v0.12
- v4
- v5
- v6
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -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))
Expand Down
64 changes: 40 additions & 24 deletions 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)
Expand All @@ -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
```
Expand All @@ -33,24 +33,28 @@ 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
```

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
Expand All @@ -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
```
Expand All @@ -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')

let str = '22h 16m'

var hours = timestring(str, 'h') // 22.266666666666666
var days = timestring(str, 'd') // 0.9277777777777778
var weeks = timestring(str, 'w') // 0.13253968253968254
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
Expand All @@ -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
```
Expand All @@ -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
Expand Down
70 changes: 32 additions & 38 deletions index.js
@@ -1,4 +1,4 @@
var _ = require('lodash')
'use strict'

/**
* Exports
Expand All @@ -12,7 +12,7 @@ module.exports = parseTimestring
* @type {Object}
*/

var defaultOpts = {
const DEFAULT_OPTS = {
hoursPerDay: 24,
daysPerWeek: 7,
weeksPerMonth: 4,
Expand All @@ -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'],
Expand All @@ -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)
})
Expand All @@ -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,
Expand All @@ -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)]
}
9 changes: 2 additions & 7 deletions package.json
@@ -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": {
Expand All @@ -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",
Expand Down

0 comments on commit 9ae3e78

Please sign in to comment.