JAVASCRIPT   25
getGUID
Guest on 2nd February 2023 02:00:27 PM


  1. "use strict";
  2.  
  3. var utils = {
  4.         getGUID: function() {
  5.                 function s4() {
  6.                         return Math.floor((1 + Math.random()) * 0x10000)
  7.                                 .toString(16)
  8.                                 .substring(1);
  9.                 }
  10.  
  11.                 return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
  12.                 s4() + '-' + s4() + s4() + s4();
  13.         },
  14.         indexOf: function(array, searchElement, fromIndex) {
  15.                 if (Array.prototype.indexOf) {
  16.                         return array.indexOf(searchElement, fromIndex);
  17.                 } else {
  18.                         var k;
  19.                         var O = Object(array);
  20.  
  21.                         // 2. Let lenValue be the result of calling the Get
  22.                         //    internal method of O with the argument "length".
  23.                         // 3. Let len be ToUint32(lenValue).
  24.                         var len = O.length >>> 0;
  25.  
  26.                         // 4. If len is 0, return -1.
  27.                         if (len === 0) {
  28.                                 return -1;
  29.                         }
  30.  
  31.                         // 5. If argument fromIndex was passed let n be
  32.                         //    ToInteger(fromIndex); else let n be 0.
  33.                         var n = +fromIndex || 0;
  34.  
  35.                         if (Math.abs(n) === Infinity) {
  36.                                 n = 0;
  37.                         }
  38.  
  39.                         // 6. If n >= len, return -1.
  40.                         if (n >= len) {
  41.                                 return -1;
  42.                         }
  43.  
  44.                         // 7. If n >= 0, then Let k be n.
  45.                         // 8. Else, n<0, Let k be len - abs(n).
  46.                         //    If k is less than 0, then let k be 0.
  47.                         k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
  48.  
  49.                         // 9. Repeat, while k < len
  50.                         while (k < len) {
  51.                                 var kValue;
  52.                                 // a. Let Pk be ToString(k).
  53.                                 //   This is implicit for LHS operands of the in operator
  54.                                 // b. Let kPresent be the result of calling the
  55.                                 //    HasProperty internal method of O with argument Pk.
  56.                                 //   This step can be combined with c
  57.                                 // c. If kPresent is true, then
  58.                                 //    i.  Let elementK be the result of calling the Get
  59.                                 //        internal method of O with the argument ToString(k).
  60.                                 //   ii.  Let same be the result of applying the
  61.                                 //        Strict Equality Comparison Algorithm to
  62.                                 //        searchElement and elementK.
  63.                                 //  iii.  If same is true, return k.
  64.                                 if (k in O && O[k] === searchElement) {
  65.                                         return k;
  66.                                 }
  67.                                 k++;
  68.                         }
  69.                         return -1;
  70.                 }
  71.         },
  72.  
  73.         // from http://ejohn.org/projects/flexible-javascript-events/
  74.         addEvent: function( obj, type, fn ) {
  75.                 if ( obj.attachEvent ) {
  76.                         obj['e'+type+fn] = fn;
  77.                         obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
  78.                         obj.attachEvent( 'on'+type, obj[type+fn] );
  79.                 } else
  80.                         obj.addEventListener( type, fn, false );
  81.         },
  82.  
  83.         // from http://ejohn.org/projects/flexible-javascript-events/
  84.         removeEvent: function( obj, type, fn ) {
  85.                 if ( obj.detachEvent ) {
  86.                         obj.detachEvent( 'on'+type, obj[type+fn] );
  87.                         obj[type+fn] = null;
  88.                 } else
  89.                         obj.removeEventListener( type, fn, false );
  90.         }
  91.  
  92. };

Raw Paste

Login or Register to edit or fork this paste. It's free.