Skip to content

Commit 2a230ec

Browse files
committed
Fixed issues with semver, added dedicated semver unit tests. Version bump.
1 parent f8255e9 commit 2a230ec

4 files changed

Lines changed: 39 additions & 5 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ngnjs/plugin",
3-
"version": "1.0.0-alpha.7",
3+
"version": "1.0.0-alpha.8",
44
"description": "A plugin module for NGN.",
55
"main": "src/index.js",
66
"module": "index.js",

src/semver.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,41 @@ export default class SV {
111111
if (typeof a.subversion === typeof b.subversion && typeof a.subversion === 'string') {
112112
return a.subversion.length > b.subversions.length
113113
}
114+
if (!a.prerelease && b.prerelease) { return true }
115+
if (a.prerelease && !b.prerelease) { return false }
116+
if (a.prerelease && b.prerelease) {
117+
try {
118+
const { aPrefix } = /^(<aPrefix>[^\d]+)/i.exec(a.prerelease).groups
119+
const { bPrefix } = /^(<bPrefix>[^\d]+)/i.exec(b.prerelease).groups
120+
if (aPrefix !== bPrefix) {
121+
return aPrefix > bPrefix
122+
}
123+
const { aNum } = /(<aNum>^\d+)/i.exec(a.prerelease).groups
124+
const { bNum } = /(<bNum>^\d+)/i.exec(b.prerelease).groups
125+
return aNum === undefined ? false : (bNum === undefined ? true : (aNum === bNum ? false : aNum > bNum))
126+
} catch (e) {
127+
return a.prerelease > b.prerelease
128+
}
129+
}
130+
114131
return true
115132
}
116133

117134
static gte () {
118-
return (SV.gt(...arguments) || SV.eq(...arguments))
135+
return (SV.eq(...arguments) || SV.gt(...arguments))
119136
}
120137

121138
static lt () {
122139
return !SV.gte(...arguments)
123140
}
124141

125142
static lte () {
126-
return (SV.lt(...arguments) || SV.eq(...arguments))
143+
return (SV.eq(...arguments) || SV.lt(...arguments))
127144
}
128145

129146
static eq () {
130-
const { a, b } = normalize(arguments)
131-
try { return a.toString() === b.toString() } catch (e) { return false }
147+
const { a, b } = normalize(...arguments)
148+
return a.toString() === b.toString()
132149
}
133150

134151
// Only necessary when inc/dec are enabled.

tests/01-semver.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import test from 'tappedout'
2+
import { Semver as sv } from '@ngnjs/plugin'
3+
4+
const a = '2.0.0-alpha.3'
5+
const b = '2.0.0-alpha.5'
6+
7+
test('Semver Checks', t => {
8+
t.expect(true, sv.eq(a, a), 'equal')
9+
t.expect(true, sv.lt(a, b), 'less than')
10+
t.expect(true, sv.gt(b, a), 'greater than')
11+
t.expect(true, sv.lte(a, b), 'less than or equal to (unequal)')
12+
t.expect(true, sv.gte(b, a), 'greater than or equal to (unequal)')
13+
t.expect(true, sv.lte(a, a), 'less than or equal to (equal)')
14+
t.expect(true, sv.gte(a, a), 'greater than or equal to (equal)')
15+
t.expect(b, sv.select('^2.0.0-alpha', b, a), 'Select latest version of the same minor version.')
16+
t.end()
17+
})
File renamed without changes.

0 commit comments

Comments
 (0)