Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added error checking for when an invalid string is input. #43

Merged
merged 2 commits into from
May 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ function parseTimestring (string, returnUnit, opts) {

totalSeconds += getSeconds(value, unit, unitValues)
})
} else {
throw new Error(`The string [${string}] is invalid for timestring`)
}

if (returnUnit) {
Expand Down
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,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)
})
Expand Down