aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--server/tests/helpers/index.ts1
-rw-r--r--server/tests/helpers/version.ts31
-rw-r--r--shared/core-utils/common/version.ts17
3 files changed, 36 insertions, 13 deletions
diff --git a/server/tests/helpers/index.ts b/server/tests/helpers/index.ts
index 1b5c6d15b..073ae6455 100644
--- a/server/tests/helpers/index.ts
+++ b/server/tests/helpers/index.ts
@@ -6,3 +6,4 @@ import './image'
6import './markdown' 6import './markdown'
7import './request' 7import './request'
8import './validator' 8import './validator'
9import './version'
diff --git a/server/tests/helpers/version.ts b/server/tests/helpers/version.ts
new file mode 100644
index 000000000..7d5600715
--- /dev/null
+++ b/server/tests/helpers/version.ts
@@ -0,0 +1,31 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import { expect } from 'chai'
4import { compareSemVer } from '@shared/core-utils'
5
6describe('Version', function () {
7
8 it('Should correctly compare two stable versions', async function () {
9 expect(compareSemVer('3.4.0', '3.5.0')).to.be.below(0)
10 expect(compareSemVer('3.5.0', '3.4.0')).to.be.above(0)
11
12 expect(compareSemVer('3.4.0', '4.1.0')).to.be.below(0)
13 expect(compareSemVer('4.1.0', '3.4.0')).to.be.above(0)
14
15 expect(compareSemVer('3.4.0', '3.4.1')).to.be.below(0)
16 expect(compareSemVer('3.4.1', '3.4.0')).to.be.above(0)
17 })
18
19 it('Should correctly compare two unstable version', async function () {
20 expect(compareSemVer('3.4.0-alpha', '3.4.0-beta.1')).to.be.below(0)
21 expect(compareSemVer('3.4.0-alpha.1', '3.4.0-beta.1')).to.be.below(0)
22 expect(compareSemVer('3.4.0-beta.1', '3.4.0-beta.2')).to.be.below(0)
23 expect(compareSemVer('3.4.0-beta.1', '3.5.0-alpha.1')).to.be.below(0)
24 })
25
26 it('Should correctly compare a stable and unstable versions', async function () {
27 expect(compareSemVer('3.4.0', '3.4.1-beta.1')).to.be.below(0)
28 expect(compareSemVer('3.4.0-beta.1', '3.4.0-beta.2')).to.be.below(0)
29 expect(compareSemVer('3.4.0-beta.1', '3.4.0')).to.be.below(0)
30 })
31})
diff --git a/shared/core-utils/common/version.ts b/shared/core-utils/common/version.ts
index 8a64f8c4d..305287233 100644
--- a/shared/core-utils/common/version.ts
+++ b/shared/core-utils/common/version.ts
@@ -1,18 +1,9 @@
1// Thanks https://stackoverflow.com/a/16187766 1// Thanks https://gist.github.com/iwill/a83038623ba4fef6abb9efca87ae9ccb
2function compareSemVer (a: string, b: string) { 2function compareSemVer (a: string, b: string) {
3 const regExStrip0 = /(\.0+)+$/ 3 if (a.startsWith(b + '-')) return -1
4 const segmentsA = a.replace(regExStrip0, '').split('.') 4 if (b.startsWith(a + '-')) return 1
5 const segmentsB = b.replace(regExStrip0, '').split('.')
6 5
7 const l = Math.min(segmentsA.length, segmentsB.length) 6 return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'case', caseFirst: 'upper' })
8
9 for (let i = 0; i < l; i++) {
10 const diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10)
11
12 if (diff) return diff
13 }
14
15 return segmentsA.length - segmentsB.length
16} 7}
17 8
18export { 9export {