Skip to content

Commit 4c73df0

Browse files
committed
fix: avoid throw for invalid cases when not needed
1 parent d17aebf commit 4c73df0

File tree

6 files changed

+90
-17
lines changed

6 files changed

+90
-17
lines changed

benchmarks/bench-inc.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict'
2+
3+
const Benchmark = require('benchmark')
4+
const invalidVersions = require('../test/fixtures/invalid-versions')
5+
const inc = require('../functions/inc')
6+
const suite = new Benchmark.Suite()
7+
8+
const cases = ['1.2.3', '4.2.0', '1.1.1-928490632884417731e7af463c92b034d6a78268fc993bcb88a57944']
9+
const invalidCases = invalidVersions.map(invalid => invalid[0])
10+
11+
for (const test of cases) {
12+
suite.add(`inc(${test})`, function () {
13+
inc(test, 'release')
14+
})
15+
}
16+
17+
for (const test of invalidCases) {
18+
suite.add(`invalid inc(${test})`, function () {
19+
inc(test, 'release')
20+
})
21+
}
22+
23+
suite
24+
.on('cycle', function (event) {
25+
console.log(String(event.target))
26+
})
27+
.run({ async: false })

benchmarks/bench-valid.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict'
2+
3+
const Benchmark = require('benchmark')
4+
const invalidVersions = require('../test/fixtures/invalid-versions')
5+
const valid = require('../functions/valid')
6+
const suite = new Benchmark.Suite()
7+
8+
const cases = ['1.2.3', '4.2.0', '1.1.1-928490632884417731e7af463c92b034d6a78268fc993bcb88a57944']
9+
const invalidCases = invalidVersions.map(invalid => invalid[0])
10+
11+
for (const test of cases) {
12+
suite.add(`valid(${test})`, function () {
13+
valid(test)
14+
})
15+
}
16+
17+
for (const test of invalidCases) {
18+
suite.add(`invalid valid(${test})`, function () {
19+
valid(test)
20+
})
21+
}
22+
23+
suite
24+
.on('cycle', function (event) {
25+
console.log(String(event.target))
26+
})
27+
.run({ async: false })

classes/semver.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@ const { safeRe: re, t } = require('../internal/re')
66

77
const parseOptions = require('../internal/parse-options')
88
const { compareIdentifiers } = require('../internal/identifiers')
9+
10+
function handleErrorOnSemver (semver, errorMessage, throwErrors) {
11+
if (throwErrors) {
12+
throw new TypeError(errorMessage)
13+
} else {
14+
semver.errorMessage = errorMessage
15+
}
16+
}
17+
918
class SemVer {
10-
constructor (version, options) {
19+
constructor (version, options, throwErrors = true) {
1120
options = parseOptions(options)
1221

1322
if (version instanceof SemVer) {
@@ -18,13 +27,11 @@ class SemVer {
1827
version = version.version
1928
}
2029
} else if (typeof version !== 'string') {
21-
throw new TypeError(`Invalid version. Must be a string. Got type "${typeof version}".`)
30+
return handleErrorOnSemver(this, `Invalid version. Must be a string. Got type "${typeof version}".`, throwErrors)
2231
}
2332

2433
if (version.length > MAX_LENGTH) {
25-
throw new TypeError(
26-
`version is longer than ${MAX_LENGTH} characters`
27-
)
34+
return handleErrorOnSemver(this, `version is longer than ${MAX_LENGTH} characters`, throwErrors)
2835
}
2936

3037
debug('SemVer', version, options)
@@ -37,7 +44,7 @@ class SemVer {
3744
const m = version.trim().match(options.loose ? re[t.LOOSE] : re[t.FULL])
3845

3946
if (!m) {
40-
throw new TypeError(`Invalid Version: ${version}`)
47+
return handleErrorOnSemver(this, `Invalid Version: ${version}`, throwErrors)
4148
}
4249

4350
this.raw = version
@@ -48,15 +55,15 @@ class SemVer {
4855
this.patch = +m[3]
4956

5057
if (this.major > MAX_SAFE_INTEGER || this.major < 0) {
51-
throw new TypeError('Invalid major version')
58+
return handleErrorOnSemver(this, 'Invalid major version', throwErrors)
5259
}
5360

5461
if (this.minor > MAX_SAFE_INTEGER || this.minor < 0) {
55-
throw new TypeError('Invalid minor version')
62+
return handleErrorOnSemver(this, 'Invalid minor version', throwErrors)
5663
}
5764

5865
if (this.patch > MAX_SAFE_INTEGER || this.patch < 0) {
59-
throw new TypeError('Invalid patch version')
66+
return handleErrorOnSemver(this, 'Invalid patch version', throwErrors)
6067
}
6168

6269
// numberify any prerelease numeric ids

functions/inc.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
'use strict'
22

33
const SemVer = require('../classes/semver')
4+
const parse = require('./parse')
45

56
const inc = (version, release, options, identifier, identifierBase) => {
6-
if (typeof (options) === 'string') {
7-
identifierBase = identifier
8-
identifier = options
9-
options = undefined
7+
const parsed = parse(version instanceof SemVer ? version.version : version, options)
8+
9+
if (parsed === null) {
10+
return null
1011
}
1112

1213
try {
13-
return new SemVer(
14-
version instanceof SemVer ? version.version : version,
15-
options
16-
).inc(release, identifier, identifierBase).version
14+
return parsed.inc(release, identifier, identifierBase).version
1715
} catch (er) {
1816
return null
1917
}

functions/parse.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const parse = (version, options, throwErrors = false) => {
55
if (version instanceof SemVer) {
66
return version
77
}
8+
89
try {
910
return new SemVer(version, options)
1011
} catch (er) {

test/classes/semver.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ test('invalid version numbers', (t) => {
9797
t.end()
9898
})
9999

100+
test('invalid version numbers without throw', (t) => {
101+
['1.2.3.4', 'NOT VALID', 1.2, null, 'Infinity.NaN.Infinity'].forEach((v) => {
102+
const parsed = new SemVer(v, undefined, false)
103+
104+
t.strictSame(parsed.errorMessage, typeof v === 'string'
105+
? `Invalid Version: ${v}`
106+
: `Invalid version. Must be a string. Got type "${typeof v}".`
107+
)
108+
})
109+
110+
t.end()
111+
})
112+
100113
test('incrementing', t => {
101114
t.plan(increments.length)
102115
increments.forEach(([

0 commit comments

Comments
 (0)