Skip to content

Commit

Permalink
Merge pull request #6 from dnode/feature/define-format-for-output
Browse files Browse the repository at this point in the history
Define format for output
  • Loading branch information
dy authored Apr 22, 2020
2 parents 10e1645 + 56d3e42 commit a5e1223
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,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
```
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,18 @@ parse.y = parse.d * 365.25
* convert `str` to ms
*
* @param {String} str
* @param {String} format
* @return {Number}
*/

function parse(str){
function parse(str, format){
format = format || 'ms'
var result = null
// ignore commas
str = str.replace(/(\d),(\d)/g, '$1$2')
str.replace(duration, function(_, n, units){
units = parse[units] || parse[units.toLowerCase().replace(/s$/, '')]
if (units) result = (result || 0) + parseFloat(n, 10) * units
})
return result
return result / parse[format]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "parse-duration",
"version": "0.2.1",
"description": "convert a human readable duration string to ms",
"description": "convert a human readable duration string to a duration format",
"keywords": [
"parse",
"duration",
Expand Down

0 comments on commit a5e1223

Please sign in to comment.