Guest User

Bad Code Coding Challenge #43 - UUID Generator

a guest
Aug 23rd, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * #43
  3.  * Add generateUUIDAsync() to the global scope. Compatible in Node and most web browsers.
  4.  */
  5. (() => {
  6.     if (typeof window !== "undefined") {
  7.         window.global = window;
  8.     }
  9.  
  10.     global.thorough = false; // NOTE: Setting to true may crash most browsers
  11.     global.previousUUIDs = [];
  12.     global.numberOfuuids = global.previousUUIDs.length;
  13.  
  14.     global.uuidlength = Object.freeze(36);
  15.  
  16.     var uuid;
  17.     var currentPosition;
  18.  
  19.     /**
  20.      * Asynchronously generates a UUID with extra checking for uniqueness
  21.      *
  22.      * @param {boolean} toSpec if true UUID is in lower case, as per specification 6.5.4
  23.      *
  24.      * Promise *usually* resolves with a UUID within 30 seconds
  25.      */
  26.     global.generateUUIDAsync = function generateUUIDAsync(toSpec) {
  27.         try {
  28.             // Async for more efficienciness.
  29.             return new Promise((resolve, reject) => {
  30.                 try {
  31.                     currentPosition = 0;
  32.                     uuid = "";
  33.                     function checkForUUID() {
  34.                         if (global.previousUUIDs.length > global.numberOfuuids) {
  35.                             console.log("UUID is ready!")
  36.                             resolve(global.previousUUIDs[global.previousUUIDs.length - 1]);
  37.                             global.numberOfuuids = global.numberOfuuids + 1;
  38.                         } else {
  39.                             console.warn("UUID not ready yet");
  40.                             setTimeout(checkForUUID, 1000);
  41.                         }
  42.                     }
  43.                     setTimeout(checkForUUID);
  44.  
  45.                     while (currentPosition++ < uuidlength - 1) {
  46.                         uuid = uuid + generateUUIDCharacter();
  47.                         // console.log(uuid);
  48.                         if (uuid.charAt(uuid.length - 1) === "-") {
  49.                             if (currentPosition !== 8) {
  50.                                 if (currentPosition != 13) {
  51.                                     if (currentPosition != 18) {
  52.                                         if (currentPosition != 23) {
  53.                                             resetLoopPhase();
  54.                                         }
  55.                                     }
  56.                                 }
  57.                             }
  58.                         } else if (uuid.charAt(uuid.length - 1) !== "-") {
  59.                             if (currentPosition === 8) {
  60.                                 resetLoopPhase();
  61.                             } else {
  62.                                 if (currentPosition == 13) {
  63.                                     resetLoopPhase();
  64.                                 } else {
  65.                                     if (currentPosition == 18) {
  66.                                         resetLoopPhase();
  67.                                     } else {
  68.                                         if (currentPosition == 23) {
  69.                                             resetLoopPhase();
  70.                                         }
  71.                                     }
  72.                                 }
  73.                             }
  74.                         } else {
  75.                             reject("Unexpected logic block");
  76.                         }
  77.                         if (uuid.length === uuidlength) {
  78.                             console.log("UUID found. Checking for uniqueness of " + uuid)
  79.                             isUUIDUnique(uuid).then(isUnique => {
  80.                                 if (isUnique) {
  81.                                     if (toSpec == true) {
  82.                                         global.previousUUIDs.push(uuid.toLowerCase());
  83.                                     } else if (toSpec != true) {
  84.                                         global.previousUUIDs.push(uuid);
  85.                                     } else {
  86.                                         reject(new Error("Unexpected input"));
  87.                                     }
  88.                                 } else {
  89.                                     reject("UUID is not unique" + uuid);
  90.                                 }
  91.                             })
  92.                                 .catch(e => console.error(e))
  93.                         }
  94.                     }
  95.                 } catch (e) {
  96.                     reject(e);
  97.                 }
  98.             });
  99.         } catch (e) {
  100.             console.error("Failed to generate UUID" + e);
  101.             throw e;
  102.         }
  103.     }
  104.  
  105.     function isUUIDUnique(uuid) { // Better safe than sorry.
  106.         return new Promise((resolve, reject) => {
  107.             for (var i; i < global.previousUUIDs.length; ++i) {
  108.                 if (global.previousUUIDs[i] == uuid) {
  109.                     reject("ERROR UUID IS NOT UNIQUE");
  110.                 }
  111.             }
  112.             resolve(true); // phew!
  113.         })
  114.     }
  115.  
  116.     function generateUUIDCharacter(numberBetweenZeroAnd1 = Math.random()) {
  117.         if (numberBetweenZeroAnd1 < 1 / 23) {
  118.             return "a";
  119.         }
  120.         if (numberBetweenZeroAnd1 < 2 / 23) {
  121.             return "b";
  122.         }
  123.         if (numberBetweenZeroAnd1 < 3 / 23) {
  124.             return "c";
  125.         }
  126.         if (numberBetweenZeroAnd1 < 4 / 23) {
  127.             return "d";
  128.         }
  129.         if (numberBetweenZeroAnd1 < 5 / 23) {
  130.             return "e";
  131.         }
  132.         if (numberBetweenZeroAnd1 < 6 / 23) {
  133.             return "f";
  134.         }
  135.         if (numberBetweenZeroAnd1 < 12 / 23) {
  136.             return generateUUIDCharacter(numberBetweenZeroAnd1 - (6 / 23)).toUpperCase();
  137.         }
  138.         if (numberBetweenZeroAnd1 < 13 / 23) {
  139.             return "9";
  140.         }
  141.         if (numberBetweenZeroAnd1 < 14 / 23) {
  142.             return "8";
  143.         }
  144.         if (numberBetweenZeroAnd1 < 15 / 23) {
  145.             return "7";
  146.         }
  147.         if (numberBetweenZeroAnd1 < 16 / 23) {
  148.             return "6";
  149.         }
  150.         if (numberBetweenZeroAnd1 < 17 / 23) {
  151.             return "5";
  152.         }
  153.         if (numberBetweenZeroAnd1 < 18 / 23) {
  154.             return "4";
  155.         }
  156.         if (numberBetweenZeroAnd1 < 19 / 23) {
  157.             return "3";
  158.         }
  159.         if (numberBetweenZeroAnd1 < 20 / 23) {
  160.             return "2";
  161.         }
  162.         if (numberBetweenZeroAnd1 < 21 / 23) {
  163.             return "1";
  164.         }
  165.         if (numberBetweenZeroAnd1 < 22 / 23) {
  166.             return "0";
  167.         }
  168.         if (numberBetweenZeroAnd1 <= 23 / 23) {
  169.             return "-";
  170.         }
  171.         throw new Error("Randome number failed to be between 0 and 1");
  172.     }
  173.  
  174.     function resetLoopPhase() {
  175.         if (currentPosition === 8 && global.thorough == false) { // Prevent weaker machines from flaking out
  176.             uuid = uuid.substr(0, uuid.lastIndexOf(uuid.charAt(uuid.length - 1)));
  177.             currentPosition = uuid.length - 1;
  178.         } else {
  179.             console.error("INVALID UUID: " + uuid + ". Starting again to ensure purity");
  180.             uuid = ""
  181.             currentPosition = 0;
  182.         }
  183.     }
  184. })();
  185.  
  186. /// TEST
  187. console.time();
  188. generateUUIDAsync(true).then(uuid => {
  189.     console.log(uuid);
  190.     console.timeEnd();
  191. }).catch(console.error);
  192.  
Advertisement
Add Comment
Please, Sign In to add comment