Created
May 4, 2017 03:50
-
-
Save zason1001/c18274fbc2d652033b81398ee9e8df40 to your computer and use it in GitHub Desktop.
version_compare.js
This file contains hidden or 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
| /** | |
| * 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) - https://github.com/alexey-bass | |
| * @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; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
App版本比较