-
-
Save alexey-bass/1115557 to your computer and use it in GitHub Desktop.
/** | |
* 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); | |
}); |
if (typeof left + typeof right != 'stringstring') 'stringstring' what does it means?
@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.
Why don't you just use localeCompare() Method?
@roygwsidev '10.1.1'.localeCompare('3.4.10')
if (typeof left + typeof right != 'stringstring') 'stringstring' what does it means?
That check your value type let's say you will pass likeversionCompare(1.2, "1.2")
it will send false value becausestringstring
not match
@zebra0303 can we do implement that like typeof left !=='string' && typeof right!== 'string'
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