Merge pull request #43 from prolink007/master

Added error checking for when an invalid string is input.
This commit is contained in:
Michael Barrett 2019-05-04 20:19:30 +01:00 committed by GitHub
commit a3ed595a17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View File

@ -61,6 +61,8 @@ function parseTimestring (string, returnUnit, opts) {
totalSeconds += getSeconds(value, unit, unitValues)
})
} else {
throw new Error(`The string [${string}] is invalid for timestring`)
}
if (returnUnit) {

View File

@ -87,6 +87,14 @@ describe('timestring', () => {
expect(() => timestring('1g')).to.throw(Error)
})
it('throws an error when no numbers are in the timestring', () => {
expect(() => timestring('asdf')).to.throw(Error)
})
it('throws an error when numbers tail the timestring', () => {
expect(() => timestring('asdf123')).to.throw(Error)
})
it('can parse a messy time string', () => {
expect(timestring('5 D a YS 4 h 2 0 mI nS')).to.equal(447600)
})