Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * #43
- * Add generateUUIDAsync() to the global scope. Compatible in Node and most web browsers.
- */
- (() => {
- if (typeof window !== "undefined") {
- window.global = window;
- }
- global.thorough = false; // NOTE: Setting to true may crash most browsers
- global.previousUUIDs = [];
- global.numberOfuuids = global.previousUUIDs.length;
- global.uuidlength = Object.freeze(36);
- var uuid;
- var currentPosition;
- /**
- * Asynchronously generates a UUID with extra checking for uniqueness
- *
- * @param {boolean} toSpec if true UUID is in lower case, as per specification 6.5.4
- *
- * Promise *usually* resolves with a UUID within 30 seconds
- */
- global.generateUUIDAsync = function generateUUIDAsync(toSpec) {
- try {
- // Async for more efficienciness.
- return new Promise((resolve, reject) => {
- try {
- currentPosition = 0;
- uuid = "";
- function checkForUUID() {
- if (global.previousUUIDs.length > global.numberOfuuids) {
- console.log("UUID is ready!")
- resolve(global.previousUUIDs[global.previousUUIDs.length - 1]);
- global.numberOfuuids = global.numberOfuuids + 1;
- } else {
- console.warn("UUID not ready yet");
- setTimeout(checkForUUID, 1000);
- }
- }
- setTimeout(checkForUUID);
- while (currentPosition++ < uuidlength - 1) {
- uuid = uuid + generateUUIDCharacter();
- // console.log(uuid);
- if (uuid.charAt(uuid.length - 1) === "-") {
- if (currentPosition !== 8) {
- if (currentPosition != 13) {
- if (currentPosition != 18) {
- if (currentPosition != 23) {
- resetLoopPhase();
- }
- }
- }
- }
- } else if (uuid.charAt(uuid.length - 1) !== "-") {
- if (currentPosition === 8) {
- resetLoopPhase();
- } else {
- if (currentPosition == 13) {
- resetLoopPhase();
- } else {
- if (currentPosition == 18) {
- resetLoopPhase();
- } else {
- if (currentPosition == 23) {
- resetLoopPhase();
- }
- }
- }
- }
- } else {
- reject("Unexpected logic block");
- }
- if (uuid.length === uuidlength) {
- console.log("UUID found. Checking for uniqueness of " + uuid)
- isUUIDUnique(uuid).then(isUnique => {
- if (isUnique) {
- if (toSpec == true) {
- global.previousUUIDs.push(uuid.toLowerCase());
- } else if (toSpec != true) {
- global.previousUUIDs.push(uuid);
- } else {
- reject(new Error("Unexpected input"));
- }
- } else {
- reject("UUID is not unique" + uuid);
- }
- })
- .catch(e => console.error(e))
- }
- }
- } catch (e) {
- reject(e);
- }
- });
- } catch (e) {
- console.error("Failed to generate UUID" + e);
- throw e;
- }
- }
- function isUUIDUnique(uuid) { // Better safe than sorry.
- return new Promise((resolve, reject) => {
- for (var i; i < global.previousUUIDs.length; ++i) {
- if (global.previousUUIDs[i] == uuid) {
- reject("ERROR UUID IS NOT UNIQUE");
- }
- }
- resolve(true); // phew!
- })
- }
- function generateUUIDCharacter(numberBetweenZeroAnd1 = Math.random()) {
- if (numberBetweenZeroAnd1 < 1 / 23) {
- return "a";
- }
- if (numberBetweenZeroAnd1 < 2 / 23) {
- return "b";
- }
- if (numberBetweenZeroAnd1 < 3 / 23) {
- return "c";
- }
- if (numberBetweenZeroAnd1 < 4 / 23) {
- return "d";
- }
- if (numberBetweenZeroAnd1 < 5 / 23) {
- return "e";
- }
- if (numberBetweenZeroAnd1 < 6 / 23) {
- return "f";
- }
- if (numberBetweenZeroAnd1 < 12 / 23) {
- return generateUUIDCharacter(numberBetweenZeroAnd1 - (6 / 23)).toUpperCase();
- }
- if (numberBetweenZeroAnd1 < 13 / 23) {
- return "9";
- }
- if (numberBetweenZeroAnd1 < 14 / 23) {
- return "8";
- }
- if (numberBetweenZeroAnd1 < 15 / 23) {
- return "7";
- }
- if (numberBetweenZeroAnd1 < 16 / 23) {
- return "6";
- }
- if (numberBetweenZeroAnd1 < 17 / 23) {
- return "5";
- }
- if (numberBetweenZeroAnd1 < 18 / 23) {
- return "4";
- }
- if (numberBetweenZeroAnd1 < 19 / 23) {
- return "3";
- }
- if (numberBetweenZeroAnd1 < 20 / 23) {
- return "2";
- }
- if (numberBetweenZeroAnd1 < 21 / 23) {
- return "1";
- }
- if (numberBetweenZeroAnd1 < 22 / 23) {
- return "0";
- }
- if (numberBetweenZeroAnd1 <= 23 / 23) {
- return "-";
- }
- throw new Error("Randome number failed to be between 0 and 1");
- }
- function resetLoopPhase() {
- if (currentPosition === 8 && global.thorough == false) { // Prevent weaker machines from flaking out
- uuid = uuid.substr(0, uuid.lastIndexOf(uuid.charAt(uuid.length - 1)));
- currentPosition = uuid.length - 1;
- } else {
- console.error("INVALID UUID: " + uuid + ". Starting again to ensure purity");
- uuid = ""
- currentPosition = 0;
- }
- }
- })();
- /// TEST
- console.time();
- generateUUIDAsync(true).then(uuid => {
- console.log(uuid);
- console.timeEnd();
- }).catch(console.error);
Advertisement
Add Comment
Please, Sign In to add comment