Skip to content

Instantly share code, notes, and snippets.

@alexey-bass
Created July 30, 2011 14:03
Show Gist options
  • Save alexey-bass/1115557 to your computer and use it in GitHub Desktop.
Save alexey-bass/1115557 to your computer and use it in GitHub Desktop.
JavaScript version compare
/**
* Simply compares two string version values.
*
* Example:
* versionCompare('1.1', '1.2') => -1
* versionCompare('1.1', '1.1') => 0
* versionCompare('1.2', '1.1') => 1
* versionCompare('2.23.3', '2.22.3') => 1
*
* Returns:
* -1 = left is LOWER than right
* 0 = they are equal
* 1 = left is GREATER = right is LOWER
* And FALSE if one of input versions are not valid
*
* @function
* @param {String} left Version #1
* @param {String} right Version #2
* @return {Integer|Boolean}
* @author Alexey Bass (albass)
* @since 2011-07-14
*/
versionCompare = function(left, right) {
if (typeof left + typeof right != 'stringstring')
return false;
var a = left.split('.')
, b = right.split('.')
, i = 0, len = Math.max(a.length, b.length);
for (; i < len; i++) {
if ((a[i] && !b[i] && parseInt(a[i]) > 0) || (parseInt(a[i]) > parseInt(b[i]))) {
return 1;
} else if ((b[i] && !a[i] && parseInt(b[i]) > 0) || (parseInt(a[i]) < parseInt(b[i]))) {
return -1;
}
}
return 0;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>QUnit Test Suite</title>
<link rel="stylesheet" href="qunit/qunit.css">
<script src="qunit/qunit.js"></script>
<script src="compare.js"></script>
<script src="tests.qunit.js"></script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture">test markup</div>
</body>
</html>
test('invalid input', function() {
equal(versionCompare(1, '2'), false);
equal(versionCompare('2', 1), false);
equal(versionCompare(3, 3), false);
equal(versionCompare(true, false), false);
equal(versionCompare(window, document), false);
});
test('one digit', function() {
equal(versionCompare('1', '2'), -1);
equal(versionCompare('2', '1'), 1);
equal(versionCompare('3', '3'), 0);
});
test('many sections', function() {
equal(versionCompare('2.22', '2.22'), 0);
equal(versionCompare('2.23', '2.22'), 1);
equal(versionCompare('2.22', '2.23'), -1);
equal(versionCompare('2.22.0', '2.22.0'), 0);
equal(versionCompare('2.22.1', '2.22.0'), 1);
equal(versionCompare('2.22.1', '2.23.1'), -1);
});
test('not equal sections', function() {
equal(versionCompare('2', '2.0'), 0);
equal(versionCompare('2', '2.1'), -1);
equal(versionCompare('2.1', '2'), 1);
});
test('LarryKim comment 120330', function() {
equal(versionCompare('5', '3000'), -1);
equal(versionCompare('3000', '5'), 1);
});
@alexey-bass
Copy link
Author

Thanks, LarryKim.
Updated func and tests.

@minghiuyau
Copy link

Hi. Can I have your permission to use the function for our webapp (https://nous.net/app) ?

@metaskills
Copy link

Thanks! Love it!

@sandeeptharayilGit
Copy link

this is superb!!

@xmnboy
Copy link

xmnboy commented Aug 12, 2016

Would be helpful if you included a reference to license terms in the header. For example, something like

MIT License (MIT)
Copyright (c) 2011-2016, Alexey Bass
see https://tldrlegal.com/license/mit-license#fulltext

@ghostcode
Copy link

if (typeof left + typeof right != 'stringstring') 'stringstring' what does it means?

@faridv
Copy link

faridv commented Nov 13, 2016

@ghostcode typeof variable will return a string containing type of variable like "string" or "function" or whatever.
"stringstring" is the concatenation of types of two variables and the if block is checking whether type of both of left and right are string or not.

@roygwsidev
Copy link

Why don't you just use localeCompare() Method?

https://www.w3schools.com/jsref/jsref_localecompare.asp

@WangYang-Rex
Copy link

@roygwsidev '10.1.1'.localeCompare('3.4.10')

@gajjardarshithasmukhbhai

if (typeof left + typeof right != 'stringstring') 'stringstring' what does it means?
That check your value type let's say you will pass like versionCompare(1.2, "1.2") it will send false value because stringstring not match

@zebra0303 can we do implement that like typeof left !=='string' && typeof right!== 'string'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment