Skip to content

Commit

Permalink
update error when unable to parse timestring
Browse files Browse the repository at this point in the history
  • Loading branch information
mike182uk committed May 4, 2019
1 parent a3ed595 commit fa8d151
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,14 @@ console.log(daysThisWeek) // will log 5
```

It is important to note that the `daysPerYear` configuration option will be used to convert a month or year to seconds, so if you are using custom configuration options make sure that you adjust this value to suit if you expect to be parsing timestrings containing months or years.

## Notes

If the string that is passed into `timestring` can not be parsed then an error will be thrown:

```js
const timestring = require('timestring')

let str = 'aaabbbccc'
let time = timestring(str) // will throw an error
```
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ function parseTimestring (string, returnUnit, opts) {
.replace(/[^.\w+-]+/g, '')
.match(/[-+]?[0-9.]+[a-z]+/g)

if (groups !== null) {
groups.forEach(group => {
let value = group.match(/[0-9.]+/g)[0]
let unit = group.match(/[a-z]+/g)[0]

totalSeconds += getSeconds(value, unit, unitValues)
})
} else {
throw new Error(`The string [${string}] is invalid for timestring`)
if (groups === null) {
throw new Error(`The string [${string}] could not be parsed by timestring`)
}

groups.forEach(group => {
let value = group.match(/[0-9.]+/g)[0]
let unit = group.match(/[a-z]+/g)[0]

totalSeconds += getSeconds(value, unit, unitValues)
})

if (returnUnit) {
return convert(totalSeconds, returnUnit, unitValues)
}
Expand Down

0 comments on commit fa8d151

Please sign in to comment.