-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement version check and compare functions
Closes: #253
- Loading branch information
1 parent
f73deed
commit 0be3319
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict'; | ||
|
||
// Compare versions | ||
// v1 - <string>, with format `a.b.c` | ||
// v2 - <string>, with format `x.y.z` | ||
// Returns: <number> | ||
// - `0` if `v1` and `v2` are equal | ||
// - `1` if `v1` > `v2` | ||
// - `-1` if `v1` < `v2` | ||
const compareVersions = (v1, v2) => { | ||
if (v1 === v2) return 0; | ||
const arr1 = v1.split('.').map(num => parseInt(num)); | ||
const arr2 = v2.split('.').map(num => parseInt(num)); | ||
|
||
if (arr1.length < arr2.length) { | ||
arr1.push(...Array(arr2.length - arr1.length).fill(0)); | ||
} else if (arr2.length < arr1.length) { | ||
arr2.push(...Array(arr1.length - arr2.length).fill(0)); | ||
} | ||
|
||
for (let i = 0; i < arr1.length; i++) { | ||
if (arr1[i] < arr2[i]) return -1; | ||
if (arr1[i] > arr2[i]) return 1; | ||
} | ||
return 0; | ||
}; | ||
|
||
// Check if first version is equal or bigger than the second one | ||
// v1 - <string>, with format `a.b.c` | ||
// v2 - <string>, with format `x.y.z` | ||
// Returns: <boolean> | ||
// - `true` if `v1` and `v2` are equal or `v1` < `v2` | ||
// - `false` if `v1` > `v2` | ||
const checkVersion = (v1, v2) => compareVersions(v1, v2) <= 0; | ||
|
||
// Check if `process.versions.node` is equal or bigger than the specified one | ||
// version - <string>, with format `a.b.c` | ||
// Returns: <boolean> | ||
// - `true` if `version` is less or equal than `process.versions.node` | ||
// - `false` if `version` is bigger than `process.versions.node` | ||
const checkNodeVersion = version => | ||
compareVersions(version, process.versions.node) <= 0; | ||
|
||
// Check if `process.versions.v8` is equal or bigger than the specified one | ||
// version - <string>, with format `a.b.c` | ||
// Returns: <boolean> | ||
// - `true` if `version` is less or equal than `process.versions.v8` | ||
// - `false` if `version` is bigger than `process.versions.v8` | ||
const checkV8Version = version => | ||
compareVersions(version, process.versions.v8) <= 0; | ||
|
||
module.exports = { | ||
compareVersions, | ||
checkVersion, | ||
checkNodeVersion, | ||
checkV8Version, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict'; | ||
|
||
const metatests = require('metatests'); | ||
const common = require('..'); | ||
|
||
metatests.case( | ||
'Common / version', | ||
{ common }, | ||
{ | ||
'common.compareVersions': [ | ||
['0', '0', 0], | ||
['00', '0', 0], | ||
['0', '00', 0], | ||
['0.0', '0', 0], | ||
['0', '0.0', 0], | ||
|
||
['1', '1', 0], | ||
['01', '1', 0], | ||
['1', '01', 0], | ||
['1.0', '1', 0], | ||
['1', '1.0', 0], | ||
|
||
['0', '1', -1], | ||
['1.2', '1.3', -1], | ||
['1.2.1', '1.3', -1], | ||
['1.2', '1.2.3', -1], | ||
['1.2', '12', -1], | ||
['2.1', '21', -1], | ||
|
||
['1', '0', 1], | ||
['1.3', '1.2', 1], | ||
['1.3', '1.2.1', 1], | ||
['1.2.3', '1.2', 1], | ||
['12', '1.2', 1], | ||
['21', '2.1', 1], | ||
], | ||
'common.checkVersion': [ | ||
['0', '0', true], | ||
['00', '0', true], | ||
['0', '00', true], | ||
['0.0', '0', true], | ||
['0', '0.0', true], | ||
|
||
['1', '1', true], | ||
['01', '1', true], | ||
['1', '01', true], | ||
['1.0', '1', true], | ||
['1', '1.0', true], | ||
|
||
['0', '1', true], | ||
['1.2', '1.3', true], | ||
['1.2.1', '1.3', true], | ||
['1.2', '1.2.3', true], | ||
['1.2', '12', true], | ||
['2.1', '21', true], | ||
|
||
['1', '0', false], | ||
['1.3', '1.2', false], | ||
['1.3', '1.2.1', false], | ||
['1.2.3', '1.2', false], | ||
['12', '1.2', false], | ||
['21', '2.1', false], | ||
], | ||
'common.checkNodeVersion': [[process.versions.node, true]], | ||
'common.checkV8Version': [[process.versions.v8, true]], | ||
} | ||
); |