JavaScript implementation generates GUID (global uniform identifier)

  • 2020-03-30 03:54:41
  • OfStack

A GUID (global uniform identifier) is a number generated on a single machine that is guaranteed to be unique to all machines in the same space and time. Usually the platform provides an API for generating guids. The algorithm is interesting, using Ethernet card addresses, nanoseconds, chip ID codes, and many possible Numbers. The only drawback with GUID is that the resulting strings are large.

The format of GUID is: XXXXXXXX - XXXX - XXXX - XXXX - XXXXXXXXXXXX

You all know that GUID is not very useful in front-end development, but if you need to insert an ID, and this ID corresponds to the background and other operations that require GUID, you can still generate a GUID for convenience.

In general, it is very simple to generate GUID in SQL, Java, C# and other background or database languages, and the front end does not directly generate GUID method, can only write one by hand. But because GUID needs to get the Ethernet card's address, nanosecond time, and so on. However, it is difficult for the front end to obtain these information (please tell me if you know children's shoes), and we can simulate the realization of generating GUID, the code is as follows:


/*
 *  Function: generate one GUID Code, GUID In order to 14 Below the date and time and 18 More than one 16 Base random number composition, GUID There is a certain probability of repetition, but the probability of repetition is very low 10ms There are 1/(16^18) , i.e., 16 the 18 The power of 1 , the repetition probability is so low as to be negligible 
 *  Disclaimer: this code is for the author to learn, such as in the use of the user in the process of code problems caused by the loss, and the author has no relationship 
 *  Date: 2014 years 9 month 4 day 
 *  The author: wyc
 */

 
function GUID() {
  this.date = new Date();

  
  if (typeof this.newGUID != 'function') {
    
    
    GUID.prototype.newGUID = function() {
      this.date = new Date();
      var guidStr = '';
        sexadecimalDate = this.hexadecimal(this.getGUIDDate(), 16);
        sexadecimalTime = this.hexadecimal(this.getGUIDTime(), 16);
      for (var i = 0; i < 9; i++) {
        guidStr += Math.floor(Math.random()*16).toString(16);
      }
      guidStr += sexadecimalDate;
      guidStr += sexadecimalTime;
      while(guidStr.length < 32) {
        guidStr += Math.floor(Math.random()*16).toString(16);
      }
      return this.formatGUID(guidStr);
    }

    
    GUID.prototype.getGUIDDate = function() {
      return this.date.getFullYear() + this.addZero(this.date.getMonth() + 1) + this.addZero(this.date.getDay());
    }

    
    GUID.prototype.getGUIDTime = function() {
      return this.addZero(this.date.getHours()) + this.addZero(this.date.getMinutes()) + this.addZero(this.date.getSeconds()) + this.addZero( parseInt(this.date.getMilliseconds() / 10 ));
    }

    
    GUID.prototype.addZero = function(num) {
      if (Number(num).toString() != 'NaN' && num >= 0 && num < 10) {
        return '0' + Math.floor(num);
      } else {
        return num.toString();
      }
    }

    
    GUID.prototype.hexadecimal = function(num, x, y) {
      if (y != undefined) {
        return parseInt(num.toString(), y).toString(x);
      } else {
        return parseInt(num.toString()).toString(x);
      }
    }

    
    GUID.prototype.formatGUID = function(guidStr) {
      var str1 = guidStr.slice(0, 8) + '-',
        str2 = guidStr.slice(8, 12) + '-',
        str3 = guidStr.slice(12, 16) + '-',
        str4 = guidStr.slice(16, 20) + '-',
        str5 = guidStr.slice(20);
      return str1 + str2 + str3 + str4 + str5;
    }
  }
}

GUID object

All you need to do is save it in a JS file and reference it.

And then we just have to.


  var guid = new GUID();   alert(guid.newGUID()); 

You can get the GUID code.

The implementation principle is very simple, here only used the system time and more than 18 hexadecimal random number composition, and the system time conversion to hexadecimal, although it is still possible to repeat, but the probability of repetition is very low, can be ignored.

The above method for generating GUID written for myself, if there is a better method can tell me, thank you!


Related articles: