-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (36 loc) · 996 Bytes
/
index.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
'use strict';
/**
* Transform version to string
*
* @param {array} version
* @param {number} [len=10]
*/
const transformVersion = (version, len = 10) => version.map(val => '0'.repeat(len - val.length) + val).join('.');
/**
* Compare Versions
*
* Please make sure the version type is consistent.
*
* @param {string} a version
* @param {string} b another version
* @param {number} c string length, default 10
*
* if a > b, return 1
* if a === b, return 0
* if a < b, return -1
* if (versions type is inconsistent) return null
*/
const compareVersions = (a, b, c) => {
const aArray = a.split('.');
const bArray = b.split('.');
if (aArray.length !== bArray.length) {
console.error(`[CompareVersions]: ${a}/${b}, the Versions type is inconsistent.`);
return null;
}
const aStr = transformVersion(aArray, c);
const bStr = transformVersion(bArray, c);
if (aStr > bStr) return 1;
if (aStr === bStr) return 0;
return -1;
};
export default compareVersions;