update error when unable to parse timestring

This commit is contained in:
Michael Barrett 2019-05-04 20:26:45 +01:00
parent a3ed595a17
commit fa8d1519d2
2 changed files with 20 additions and 9 deletions

View File

@ -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
```

View File

@ -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)
}