From e0be5577a74a92fa13d34b463dd9bf4e3a42948f Mon Sep 17 00:00:00 2001 From: Ashton Cummings Date: Thu, 20 Sep 2018 09:26:31 -0500 Subject: [PATCH 1/2] Added error checking for when an invalid string is input. --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index b21f71e..194bff7 100644 --- a/index.js +++ b/index.js @@ -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) { From 66ed4f27fe15a024d51b266be8e36110bcace956 Mon Sep 17 00:00:00 2001 From: Ashton Cummings Date: Thu, 20 Sep 2018 12:44:56 -0500 Subject: [PATCH 2/2] Added unit tests. --- test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test.js b/test.js index 7ae84f0..14d5d45 100644 --- a/test.js +++ b/test.js @@ -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) })