diff --git a/README.md b/README.md index 6873b22..efb905d 100644 --- a/README.md +++ b/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 +``` diff --git a/index.js b/index.js index 04bff1c..6f6ee8e 100644 --- a/index.js +++ b/index.js @@ -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) }