Skip to content
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
40 changes: 5 additions & 35 deletions benchmarks/bench-compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,15 @@ const Benchmark = require('benchmark')
const SemVer = require('../classes/semver')
const suite = new Benchmark.Suite()

const versions = ['1.0.3', '2.2.2', '2.3.0']
const versionToCompare = '1.0.2'
const option1 = { includePrelease: true }
const option2 = { includePrelease: true, loose: true }
const option3 = { includePrelease: true, loose: true, rtl: true }
const comparisons = require('../test/fixtures/comparisons')

for (const version of versions) {
suite.add(`compare ${version} to ${versionToCompare}`, function () {
const semver = new SemVer(version)
semver.compare(versionToCompare)
for (const [v0, v1] of comparisons) {
suite.add(`compare ${v0} to ${v1}`, function () {
const semver = new SemVer(v0)
semver.compare(v1)
})
}

for (const version of versions) {
suite.add(
`compare ${version} to ${versionToCompare} with option (${JSON.stringify(option1)})`,
function () {
const semver = new SemVer(version, option1)
semver.compare(versionToCompare)
})
}

for (const version of versions) {
suite.add(`compare ${version} to ${versionToCompare} with option (${JSON.stringify(option2)})`,
function () {
const semver = new SemVer(version, option2)
semver.compare(versionToCompare)
})
}

for (const version of versions) {
suite.add(
`compare ${version} to ${versionToCompare} with option (${JSON.stringify(option3)})`,
function () {
const semver = new SemVer(version, option3)
semver.compare(versionToCompare)
})
}

suite
.on('cycle', function (event) {
console.log(String(event.target))
Expand Down
13 changes: 6 additions & 7 deletions benchmarks/bench-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

const Benchmark = require('benchmark')
const parse = require('../functions/parse')
const { MAX_SAFE_INTEGER } = require('../internal/constants')
const suite = new Benchmark.Suite()

const cases = ['1.2.1', '1.2.2-4', '1.2.3-pre']
const invalidCases = [`${MAX_SAFE_INTEGER}0.0.0`, 'hello, world', 'xyz']
const cases = require(`../test/fixtures/valid-versions`)
const invalidCases = require(`../test/fixtures/invalid-versions`)

for (const test of cases) {
suite.add(`parse(${test})`, function () {
parse(test)
suite.add(`parse(${test[0]})`, function () {
parse(test[0])
})
}

for (const test of invalidCases) {
suite.add(`invalid parse(${test})`, function () {
parse(test)
suite.add(`invalid parse(${test[0]})`, function () {
parse(test[0])
})
}

Expand Down
24 changes: 19 additions & 5 deletions classes/semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,25 @@ class SemVer {
other = new SemVer(other, this.options)
}

return (
compareIdentifiers(this.major, other.major) ||
compareIdentifiers(this.minor, other.minor) ||
compareIdentifiers(this.patch, other.patch)
)
if (this.major < other.major) {
return -1
}
if (this.major > other.major) {
return 1
}
if (this.minor < other.minor) {
return -1
}
if (this.minor > other.minor) {
return 1
}
if (this.patch < other.patch) {
return -1
}
if (this.patch > other.patch) {
return 1
}
return 0
}

comparePre (other) {
Expand Down
4 changes: 4 additions & 0 deletions internal/identifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

const numeric = /^[0-9]+$/
const compareIdentifiers = (a, b) => {
if (typeof a === 'number' && typeof b === 'number') {
return a === b ? 0 : a < b ? -1 : 1
}

const anum = numeric.test(a)
const bnum = numeric.test(b)

Expand Down
3 changes: 3 additions & 0 deletions test/internal/identifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ test('rcompareIdentifiers and compareIdentifiers', (t) => {
['1', '2'],
['alpha', 'beta'],
['0', 'beta'],
[1, 2],
]
set.forEach((ab) => {
const a = ab[0]
Expand All @@ -17,5 +18,7 @@ test('rcompareIdentifiers and compareIdentifiers', (t) => {
})
t.equal(compareIdentifiers('0', '0'), 0)
t.equal(rcompareIdentifiers('0', '0'), 0)
t.equal(compareIdentifiers(1, 1), 0)
t.equal(rcompareIdentifiers(1, 1), 0)
t.end()
})