-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtestVersion.js
83 lines (69 loc) · 1.64 KB
/
testVersion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* @module crx3/lib/testVersionRequirement
*/
module.exports = testVersionRequirement;
/**
* Compare version strings and return true if actual >= expected.
*
* @example
* console.log(testVersionRequirement('1.2.3-beta', '1.2.3-alpha')); // Output: true
*
* @alias module:crx3/lib/testVersionRequirement
* @param {string|number} actual
* @param {string|number} expected
* @return {boolean}
*/
function testVersionRequirement (actual, expected) {
if (!expected) {
return true;
}
if (!actual) {
return false;
}
const a = actual.split('.');
const e = expected.split('.');
var ai;
var ei;
for (var index = 0, max = Math.max(a.length, e.length); index < max; index++) {
ai = a[index] || '0';
ei = e[index] || '0';
if (ai === ei) {
continue;
}
if (isNaN(ai) || isNaN(ei)) {
return stringCompare(ai, ei) > 0;
}
return parseInt(ai, 10) > parseInt(ei, 10);
}
return true;
}
/**
* @private
* @param {string|number} actual
* @param {string|number} expected
* @return {number}
*/
function stringCompare (actual = '', expected = '') {
const a = actual.split('-');
const e = expected.split('-');
var ai;
var ei;
for (var index = 0, max = Math.max(a.length, e.length); index < max; index++) {
ai = a[index];
ei = e[index];
if (typeof ai === 'undefined') {
ai = index > 0 && isNaN(ei) ? 'z' : '0';
}
if (typeof ei === 'undefined') {
ei = index > 0 && isNaN(ai) ? 'z' : '0';
}
if (ai === ei) {
continue;
}
if (isNaN(ai) || isNaN(ei)) {
return ai.localeCompare(ei, undefined, {numeric: true}); // eslint-disable-line no-undefined
}
return parseInt(ai, 10) - parseInt(ei, 10);
}
return 0;
}