Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Framasoft
PeerTube
PeerTube
Commits
6e06694f
Verified
Commit
6e06694f
authored
Jan 19, 2023
by
Chocobozzz
Browse files
Fix semver comparison
parent
f008e9f3
Changes
3
Hide whitespace changes
Inline
Side-by-side
server/tests/helpers/index.ts
View file @
6e06694f
...
...
@@ -6,3 +6,4 @@ import './image'
import
'
./markdown
'
import
'
./request
'
import
'
./validator
'
import
'
./version
'
server/tests/helpers/version.ts
0 → 100644
View file @
6e06694f
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
import
{
expect
}
from
'
chai
'
import
{
compareSemVer
}
from
'
@shared/core-utils
'
describe
(
'
Version
'
,
function
()
{
it
(
'
Should correctly compare two stable versions
'
,
async
function
()
{
expect
(
compareSemVer
(
'
3.4.0
'
,
'
3.5.0
'
)).
to
.
be
.
below
(
0
)
expect
(
compareSemVer
(
'
3.5.0
'
,
'
3.4.0
'
)).
to
.
be
.
above
(
0
)
expect
(
compareSemVer
(
'
3.4.0
'
,
'
4.1.0
'
)).
to
.
be
.
below
(
0
)
expect
(
compareSemVer
(
'
4.1.0
'
,
'
3.4.0
'
)).
to
.
be
.
above
(
0
)
expect
(
compareSemVer
(
'
3.4.0
'
,
'
3.4.1
'
)).
to
.
be
.
below
(
0
)
expect
(
compareSemVer
(
'
3.4.1
'
,
'
3.4.0
'
)).
to
.
be
.
above
(
0
)
})
it
(
'
Should correctly compare two unstable version
'
,
async
function
()
{
expect
(
compareSemVer
(
'
3.4.0-alpha
'
,
'
3.4.0-beta.1
'
)).
to
.
be
.
below
(
0
)
expect
(
compareSemVer
(
'
3.4.0-alpha.1
'
,
'
3.4.0-beta.1
'
)).
to
.
be
.
below
(
0
)
expect
(
compareSemVer
(
'
3.4.0-beta.1
'
,
'
3.4.0-beta.2
'
)).
to
.
be
.
below
(
0
)
expect
(
compareSemVer
(
'
3.4.0-beta.1
'
,
'
3.5.0-alpha.1
'
)).
to
.
be
.
below
(
0
)
})
it
(
'
Should correctly compare a stable and unstable versions
'
,
async
function
()
{
expect
(
compareSemVer
(
'
3.4.0
'
,
'
3.4.1-beta.1
'
)).
to
.
be
.
below
(
0
)
expect
(
compareSemVer
(
'
3.4.0-beta.1
'
,
'
3.4.0-beta.2
'
)).
to
.
be
.
below
(
0
)
expect
(
compareSemVer
(
'
3.4.0-beta.1
'
,
'
3.4.0
'
)).
to
.
be
.
below
(
0
)
})
})
shared/core-utils/common/version.ts
View file @
6e06694f
// Thanks https://
stackoverflow.com/a/16187766
// Thanks https://
gist.github.com/iwill/a83038623ba4fef6abb9efca87ae9ccb
function
compareSemVer
(
a
:
string
,
b
:
string
)
{
const
regExStrip0
=
/
(\.
0+
)
+$/
const
segmentsA
=
a
.
replace
(
regExStrip0
,
''
).
split
(
'
.
'
)
const
segmentsB
=
b
.
replace
(
regExStrip0
,
''
).
split
(
'
.
'
)
if
(
a
.
startsWith
(
b
+
'
-
'
))
return
-
1
if
(
b
.
startsWith
(
a
+
'
-
'
))
return
1
const
l
=
Math
.
min
(
segmentsA
.
length
,
segmentsB
.
length
)
for
(
let
i
=
0
;
i
<
l
;
i
++
)
{
const
diff
=
parseInt
(
segmentsA
[
i
],
10
)
-
parseInt
(
segmentsB
[
i
],
10
)
if
(
diff
)
return
diff
}
return
segmentsA
.
length
-
segmentsB
.
length
return
a
.
localeCompare
(
b
,
undefined
,
{
numeric
:
true
,
sensitivity
:
'
case
'
,
caseFirst
:
'
upper
'
})
}
export
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment