JavaScript Comparison semantic version number implementation code _javascript tips

Source: Internet
Author: User

Objective

The so-called "semantic version number" is to show the semantics in the version number, or the version number itself is meaningful, no longer a random number. This specification makes a more rigorous description of the meaning of each number in the version number. Of course, most of the content is in the same industry as we said before, so it's easy to accept this specification for developers with version number awareness.

In Mobile end app Third-party Development often encounter some because of different versions of the JS SDK or JS API differences or support problems. For mobile front-end, it may be necessary to do some special business logic processing according to the version number of the app in the actual third party development.

Semantic version number

The general semantic version number is usually defined as this:

major_version_number.minor_version_number[. revision_number[. Build_number]]
 
major version number   . Sub version number    [. Revised version number  [. Compiled version number]]

Delimiters are generally used.

Semantic Version Number comparison method

So we can do version number comparison, here is a humble way:

The/**
 * version compares versioncompare
 * @param {String} currver the current version.
 * @param {String} promotever compare versions.
 * @return {Boolean} False the current version is less 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 simple to use:

Versioncompare ("6.3", "5.2.5"); False.
Versioncompare ("6.1", "6.1"); False.
Versioncompare ("6.1.5", "6.2"); True.

The point to note here is that according to my own business logic, the current version is less than the comparison version returns TRUE. You can modify the code according to your business logic.

For example, we want to obtain the version number of the micro-letter, you can write:

var wechatinfo = Navigator.userAgent.match (/micromessenger\/([\d\.] +)/i);
var currver = wechatinfo[1];
if (Versioncompare (Currver, "6.2.5")) {
 //Your business logic
}

Attention:

In a hurry, this method is simply a common method, without providing a way to compare only the major version or the sub version, but rather to compare the final version number.

Summarize

The above is the entire content of this article, I hope that the study or work can bring some help, if you have questions you can message exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.