Skip to content

Commit

Permalink
Define format for output
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Herrmann committed May 10, 2017
1 parent 12f010a commit c36ad87
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
8 changes: 8 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ Available unit types are:
- years (y, yr)

And its easy to add more

### parse(str, format)

The output format can also be defined

```js
parse('1hr 20mins', 'm') // => 80
```
20 changes: 18 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,30 @@ parse.year =
parse.yr =
parse.y = parse.d * 365.25

/**
* dividers for the output format
*/

let ms, s, m, h, d, w;
const dividers = {
ms: ms = 1,
s: s = ms * 1000,
m: m = s * 60,
h: h = m * 60,
d: d = h * 24,
w: w = d * 7,
};

/**
* convert `str` to ms
*
* @param {String} str
* @param {String} format
* @return {Number}
*/

function parse(str){
function parse(str, format){
format = format || 'ms'
var result = 0
// ignore commas
str = str.replace(/(\d),(\d)/g, '$1$2')
Expand All @@ -58,5 +74,5 @@ function parse(str){
|| 1
result += parseFloat(n, 10) * units
})
return result
return result / dividers[format]
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "parse-duration",
"version": "0.1.1",
"description": "convert a human readable duration string to ms",
"version": "0.2.0",
"description": "convert a human readable duration string to a duration format",
"keywords": ["parse","duration","time"],
"dependencies": {},
"devDependencies": {
Expand Down

0 comments on commit c36ad87

Please sign in to comment.