update error when unable to parse timestring
This commit is contained in:
parent
a3ed595a17
commit
fa8d1519d2
11
README.md
11
README.md
@ -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
|
||||
```
|
||||
|
8
index.js
8
index.js
@ -54,16 +54,16 @@ function parseTimestring (string, returnUnit, opts) {
|
||||
.replace(/[^.\w+-]+/g, '')
|
||||
.match(/[-+]?[0-9.]+[a-z]+/g)
|
||||
|
||||
if (groups !== null) {
|
||||
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)
|
||||
})
|
||||
} else {
|
||||
throw new Error(`The string [${string}] is invalid for timestring`)
|
||||
}
|
||||
|
||||
if (returnUnit) {
|
||||
return convert(totalSeconds, returnUnit, unitValues)
|
||||
|
Loading…
Reference in New Issue
Block a user