Skip to content

Commit 0be3319

Browse files
Implement version check and compare functions
Closes: #253
1 parent f73deed commit 0be3319

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

common.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const submodules = [
2525
'uint64', // Uint64
2626
'units', // Units conversion
2727
'utilities', // Common utilities
28+
'version', // Version compare and compatibility check
2829
].map(path => require('./lib/' + path));
2930

3031
module.exports = Object.assign({}, ...submodules);

lib/version.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
// Compare versions
4+
// v1 - <string>, with format `a.b.c`
5+
// v2 - <string>, with format `x.y.z`
6+
// Returns: <number>
7+
// - `0` if `v1` and `v2` are equal
8+
// - `1` if `v1` > `v2`
9+
// - `-1` if `v1` < `v2`
10+
const compareVersions = (v1, v2) => {
11+
if (v1 === v2) return 0;
12+
const arr1 = v1.split('.').map(num => parseInt(num));
13+
const arr2 = v2.split('.').map(num => parseInt(num));
14+
15+
if (arr1.length < arr2.length) {
16+
arr1.push(...Array(arr2.length - arr1.length).fill(0));
17+
} else if (arr2.length < arr1.length) {
18+
arr2.push(...Array(arr1.length - arr2.length).fill(0));
19+
}
20+
21+
for (let i = 0; i < arr1.length; i++) {
22+
if (arr1[i] < arr2[i]) return -1;
23+
if (arr1[i] > arr2[i]) return 1;
24+
}
25+
return 0;
26+
};
27+
28+
// Check if first version is equal or bigger than the second one
29+
// v1 - <string>, with format `a.b.c`
30+
// v2 - <string>, with format `x.y.z`
31+
// Returns: <boolean>
32+
// - `true` if `v1` and `v2` are equal or `v1` < `v2`
33+
// - `false` if `v1` > `v2`
34+
const checkVersion = (v1, v2) => compareVersions(v1, v2) <= 0;
35+
36+
// Check if `process.versions.node` is equal or bigger than the specified one
37+
// version - <string>, with format `a.b.c`
38+
// Returns: <boolean>
39+
// - `true` if `version` is less or equal than `process.versions.node`
40+
// - `false` if `version` is bigger than `process.versions.node`
41+
const checkNodeVersion = version =>
42+
compareVersions(version, process.versions.node) <= 0;
43+
44+
// Check if `process.versions.v8` is equal or bigger than the specified one
45+
// version - <string>, with format `a.b.c`
46+
// Returns: <boolean>
47+
// - `true` if `version` is less or equal than `process.versions.v8`
48+
// - `false` if `version` is bigger than `process.versions.v8`
49+
const checkV8Version = version =>
50+
compareVersions(version, process.versions.v8) <= 0;
51+
52+
module.exports = {
53+
compareVersions,
54+
checkVersion,
55+
checkNodeVersion,
56+
checkV8Version,
57+
};

test/version.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
3+
const metatests = require('metatests');
4+
const common = require('..');
5+
6+
metatests.case(
7+
'Common / version',
8+
{ common },
9+
{
10+
'common.compareVersions': [
11+
['0', '0', 0],
12+
['00', '0', 0],
13+
['0', '00', 0],
14+
['0.0', '0', 0],
15+
['0', '0.0', 0],
16+
17+
['1', '1', 0],
18+
['01', '1', 0],
19+
['1', '01', 0],
20+
['1.0', '1', 0],
21+
['1', '1.0', 0],
22+
23+
['0', '1', -1],
24+
['1.2', '1.3', -1],
25+
['1.2.1', '1.3', -1],
26+
['1.2', '1.2.3', -1],
27+
['1.2', '12', -1],
28+
['2.1', '21', -1],
29+
30+
['1', '0', 1],
31+
['1.3', '1.2', 1],
32+
['1.3', '1.2.1', 1],
33+
['1.2.3', '1.2', 1],
34+
['12', '1.2', 1],
35+
['21', '2.1', 1],
36+
],
37+
'common.checkVersion': [
38+
['0', '0', true],
39+
['00', '0', true],
40+
['0', '00', true],
41+
['0.0', '0', true],
42+
['0', '0.0', true],
43+
44+
['1', '1', true],
45+
['01', '1', true],
46+
['1', '01', true],
47+
['1.0', '1', true],
48+
['1', '1.0', true],
49+
50+
['0', '1', true],
51+
['1.2', '1.3', true],
52+
['1.2.1', '1.3', true],
53+
['1.2', '1.2.3', true],
54+
['1.2', '12', true],
55+
['2.1', '21', true],
56+
57+
['1', '0', false],
58+
['1.3', '1.2', false],
59+
['1.3', '1.2.1', false],
60+
['1.2.3', '1.2', false],
61+
['12', '1.2', false],
62+
['21', '2.1', false],
63+
],
64+
'common.checkNodeVersion': [[process.versions.node, true]],
65+
'common.checkV8Version': [[process.versions.v8, true]],
66+
}
67+
);

0 commit comments

Comments
 (0)