Preface
The so-called "semantic version number" means that the semantics are reflected in the version number, or that the version number itself is meaningful and is no longer a random number. This specification provides a relatively strict explanation of the meaning of each number in the version number. Of course, most of the content still follows the industry agreements we mentioned before, so it is easy for developers with version number awareness to accept this specification.
In the third-party development of mobile APPs, we often encounter some problems that cause differences or support of JS SDK or JS API due to different versions. For mobile front-end, in actual third-party development, some special business logic processing may be required based on the version number of the APP.
Semantic version number
The general semantic version number is usually defined as follows:
Major_Version_Number.Minor_Version_Number[.Revision_Number[.Build_Number]] Major version number. Subversion number [.Revised version number [.Compiled version number]]
Delimiter is generally used.
Semantic version number comparison method
In this way, we can compare version numbers, here is a rough method:
/** * Version Comparison VersionCompare * @param {String} currVer Current version. * @param {String} promoteVer Comparison version. * @return {Boolean} false The current version is smaller than the comparison version returns true. * * Use * VersionCompare("6.3","5.2.5"); // false. * VersionCompare("6.1", "6.1"); // false. * VersionCompare("6.1.5", "6.2"); // true. */var VersionCompare = function (currVer, promoteVer) { currVer = currVer || "0.0.0"; promoteVer = promoteVer || "0.0.0"; if (currVer == promoteVer) return false; var currVerArr = currVer.split("."); var promoteVerArr = promoteVer.split("."); var len = Math.max(currVerArr.length, promoteVerArr.length); for (var i = 0; i < len; i++) { var proVal = ~~promoteVerArr[i], curVal = ~~currVerArr[i]; if (proVal < curVal) { return false; } else if (proVal > curVal) { return true; } } return false;};It is also very easy to use:
VersionCompare("6.3","5.2.5"); // false.VersionCompare("6.1", "6.1"); // false.VersionCompare("6.1.5", "6.2"); // true.What you need to note here is that according to my own business logic, the current version is smaller than the comparison version returns true. You can modify the code according to your business logic.
For example, if we want to get the version number of WeChat, we can write it like this:
var wechatInfo = navigator.userAgent.match(/MicroMessenger//([/d/.]+)/i);var currVer = wechatInfo[1];if (VersionCompare(currVer, "6.2.5")) { //Your business logic}Notice:
Time is rushed, this method is only a common method. It does not provide a method to compare only the main version or sub-version, but rather compare the final entire version number.
Summarize
The above is the entire content of this article. I hope it will be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate.