这是另一种递归算法。
这段代码只使用了Array。shift和递归,这意味着它可以在Internet Explorer 6+中运行。如果你有任何疑问,你可以访问我的GitHub页面。
(function(root, factory) {
if (typeof exports === 'object') {
return module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
return define(factory);
} else {
return root.compareVer = factory();
}
})(this, function() {
'use strict';
var _compareVer;
_compareVer = function(newVer, oldVer) {
var VER_RE, compareNum, isTrue, maxLen, newArr, newLen, newMatch, oldArr, oldLen, oldMatch, zerofill;
VER_RE = /(\d+\.){1,9}\d+/;
if (arguments.length !== 2) {
return -100;
}
if (typeof newVer !== 'string') {
return -2;
}
if (typeof oldVer !== 'string') {
return -3;
}
newMatch = newVer.match(VER_RE);
if (!newMatch || newMatch[0] !== newVer) {
return -4;
}
oldMatch = oldVer.match(VER_RE);
if (!oldMatch || oldMatch[0] !== oldVer) {
return -5;
}
newVer = newVer.replace(/^0/, '');
oldVer = oldVer.replace(/^0/, '');
if (newVer === oldVer) {
return 0;
} else {
newArr = newVer.split('.');
oldArr = oldVer.split('.');
newLen = newArr.length;
oldLen = oldArr.length;
maxLen = Math.max(newLen, oldLen);
zerofill = function() {
newArr.length < maxLen && newArr.push('0');
oldArr.length < maxLen && oldArr.push('0');
return newArr.length !== oldArr.length && zerofill();
};
newLen !== oldLen && zerofill();
if (newArr.toString() === oldArr.toString()) {
if (newLen > oldLen) {
return 1;
} else {
return -1;
}
} else {
isTrue = -1;
compareNum = function() {
var _new, _old;
_new = ~~newArr.shift();
_old = ~~oldArr.shift();
_new > _old && (isTrue = 1);
return _new === _old && newArr.length > 0 && compareNum();
};
compareNum();
return isTrue;
}
}
};
return _compareVer;
});
好吧,我希望这段代码能帮助到一些人。
下面是测试。
console.log(compareVer("0.0.2","0.0.1"));//1
console.log(compareVer("0.0.10","0.0.1")); //1
console.log(compareVer("0.0.10","0.0.2")); //1
console.log(compareVer("0.9.0","0.9")); //1
console.log(compareVer("0.10.0","0.9.0")); //1
console.log(compareVer("1.7", "1.07")); //1
console.log(compareVer("1.0.07", "1.0.007")); //1
console.log(compareVer("0.3","0.3")); //0
console.log(compareVer("0.0.3","0.0.3")); //0
console.log(compareVer("0.0.3.0","0.0.3.0")); //0
console.log(compareVer("00.3","0.3")); //0
console.log(compareVer("00.3","00.3")); //0
console.log(compareVer("01.0.3","1.0.3")); //0
console.log(compareVer("1.0.3","01.0.3")); //0
console.log(compareVer("0.2.0","1.0.0")); //-1
console.log(compareVer('0.0.2.2.0',"0.0.2.3")); //-1
console.log(compareVer('0.0.2.0',"0.0.2")); //-1
console.log(compareVer('0.0.2',"0.0.2.0")); //-1
console.log(compareVer("1.07", "1.7")); //-1
console.log(compareVer("1.0.007", "1.0.07")); //-1
console.log(compareVer()); //-100
console.log(compareVer("0.0.2")); //-100
console.log(compareVer("0.0.2","0.0.2","0.0.2")); //-100
console.log(compareVer(1212,"0.0.2")); //-2
console.log(compareVer("0.0.2",1212)); //-3
console.log(compareVer('1.abc.2',"1.0.2")); //-4
console.log(compareVer('1.0.2',"1.abc.2")); //-5