js-版本判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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;
}