Skip to content

Commit fd9578b

Browse files
committed
fix: throw on bad version
1 parent 503a4e5 commit fd9578b

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

functions/diff.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const parse = require('./parse')
1+
const parse = require('./parse.js')
22

33
const diff = (version1, version2) => {
4-
const v1 = parse(version1)
5-
const v2 = parse(version2)
4+
const v1 = parse(version1, null, true)
5+
const v2 = parse(version2, null, true)
66
const comparison = v1.compare(v2)
77

88
if (comparison === 0) {

functions/parse.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { MAX_LENGTH } = require('../internal/constants')
22
const SemVer = require('../classes/semver')
3-
const parse = (version, options) => {
3+
const parse = (version, options, throwErrors = false) => {
44
if (version instanceof SemVer) {
55
return version
66
}
@@ -16,6 +16,9 @@ const parse = (version, options) => {
1616
try {
1717
return new SemVer(version, options)
1818
} catch (er) {
19+
if (throwErrors) {
20+
throw er
21+
}
1922
return null
2023
}
2124
}

test/functions/diff.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,13 @@ test('diff versions test', (t) => {
4343

4444
t.end()
4545
})
46+
47+
test('throws on bad version', (t) => {
48+
t.throws(() => {
49+
diff('bad', '1.2.3')
50+
}, {
51+
message: 'Invalid Version: bad',
52+
name: 'TypeError',
53+
})
54+
t.end()
55+
})

test/functions/parse.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ t.test('returns null instead of throwing when presented with garbage', t => {
99
t.equal(parse(v, opts), null, msg))
1010
})
1111

12+
t.test('throw errors if asked to', t => {
13+
t.throws(() => {
14+
parse('bad', null, true)
15+
})
16+
t.end()
17+
})
18+
1219
t.test('parse a version into a SemVer object', t => {
1320
t.match(parse('1.2.3'), new SemVer('1.2.3'))
1421
const s = new SemVer('4.5.6')

0 commit comments

Comments
 (0)