From b651e544f009cd2fb332fa99bee78e9860b2cabe Mon Sep 17 00:00:00 2001 From: Mo7art Date: Sun, 18 Oct 2020 01:15:07 +0200 Subject: [PATCH 1/2] add generateUUID.js --- String/generateUUID.js | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 String/generateUUID.js diff --git a/String/generateUUID.js b/String/generateUUID.js new file mode 100644 index 0000000000..426be4b88d --- /dev/null +++ b/String/generateUUID.js @@ -0,0 +1,45 @@ +/* Generates a UUID/GUID in Node.Js or browser. + +In the browser the script uses the `window.crypto` or `window.msCrypto` (IE11) API +On server-side the script uses `Math.random` in combination with the timestamp for better randomness. +The function generate an RFC4122 (https://www.ietf.org/rfc/rfc4122.txt) version 4 UUID/GUID + +*/ +const Guid = () => { + const crypto = typeof window || window.crypto || window.msCrypto || null + const pattern = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' + + const _padLeft = (paddingString, width, replacementChar) => + paddingString.length >= width + ? paddingString + : _padLeft(replacementChar + paddingString, width, replacementChar || ' ') + + const _quart = number => { + const hexadecimalResult = number.toString(16) + return _padLeft(hexadecimalResult, 4, '0') + } + const _cryptoGuid = () => { + const buffer = new window.Uint16Array(8) + window.crypto.getRandomValues(buffer) + return [_quart(buffer[0]) + + _quart(buffer[1]), _quart(buffer[2]), _quart(buffer[3]), _quart(buffer[4]), _quart(buffer[5]) + + _quart(buffer[6]) + + _quart(buffer[7])].join('-') + } + const _guid = () => { + let currentDateMilliseconds = new Date().getTime() + return pattern.replace(/[xy]/g, currentChar => { + const randomChar = (currentDateMilliseconds + Math.random() * 16) % 16 | 0 + currentDateMilliseconds = Math.floor(currentDateMilliseconds / 16) + return (currentChar === 'x' ? randomChar : (randomChar & 0x7 | 0x8)).toString(16) + }) + } + + return crypto !== 'undefined' && crypto !== null + ? typeof (window.crypto.getRandomValues) !== 'undefined' + ? _cryptoGuid() + : _guid() + : _guid() +} + +console.log(Guid()) // 'edc848db-3478-1760-8b55-7986003d895f' From f455cc3b09abab52a39ac2b25ba4cafc18a5f7c2 Mon Sep 17 00:00:00 2001 From: Lukas Kleybolte Date: Sun, 18 Oct 2020 19:37:10 +0200 Subject: [PATCH 2/2] Only Node.js version (delete crypto version (Browser)) --- String/GenerateGUID.js | 17 ++++++++++++++++ String/generateUUID.js | 45 ------------------------------------------ 2 files changed, 17 insertions(+), 45 deletions(-) create mode 100644 String/GenerateGUID.js delete mode 100644 String/generateUUID.js diff --git a/String/GenerateGUID.js b/String/GenerateGUID.js new file mode 100644 index 0000000000..8688199975 --- /dev/null +++ b/String/GenerateGUID.js @@ -0,0 +1,17 @@ +/* +Generates a UUID/GUID in Node.Js. +The script uses `Math.random` in combination with the timestamp for better randomness. +The function generate an RFC4122 (https://www.ietf.org/rfc/rfc4122.txt) version 4 UUID/GUID +*/ + +const Guid = () => { + const pattern = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' + let currentDateMilliseconds = new Date().getTime() + return pattern.replace(/[xy]/g, currentChar => { + const randomChar = (currentDateMilliseconds + Math.random() * 16) % 16 | 0 + currentDateMilliseconds = Math.floor(currentDateMilliseconds / 16) + return (currentChar === 'x' ? randomChar : (randomChar & 0x7 | 0x8)).toString(16) + }) +} + +console.log(Guid()) // 'edc848db-3478-1760-8b55-7986003d895f' diff --git a/String/generateUUID.js b/String/generateUUID.js deleted file mode 100644 index 426be4b88d..0000000000 --- a/String/generateUUID.js +++ /dev/null @@ -1,45 +0,0 @@ -/* Generates a UUID/GUID in Node.Js or browser. - -In the browser the script uses the `window.crypto` or `window.msCrypto` (IE11) API -On server-side the script uses `Math.random` in combination with the timestamp for better randomness. -The function generate an RFC4122 (https://www.ietf.org/rfc/rfc4122.txt) version 4 UUID/GUID - -*/ -const Guid = () => { - const crypto = typeof window || window.crypto || window.msCrypto || null - const pattern = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' - - const _padLeft = (paddingString, width, replacementChar) => - paddingString.length >= width - ? paddingString - : _padLeft(replacementChar + paddingString, width, replacementChar || ' ') - - const _quart = number => { - const hexadecimalResult = number.toString(16) - return _padLeft(hexadecimalResult, 4, '0') - } - const _cryptoGuid = () => { - const buffer = new window.Uint16Array(8) - window.crypto.getRandomValues(buffer) - return [_quart(buffer[0]) + - _quart(buffer[1]), _quart(buffer[2]), _quart(buffer[3]), _quart(buffer[4]), _quart(buffer[5]) + - _quart(buffer[6]) + - _quart(buffer[7])].join('-') - } - const _guid = () => { - let currentDateMilliseconds = new Date().getTime() - return pattern.replace(/[xy]/g, currentChar => { - const randomChar = (currentDateMilliseconds + Math.random() * 16) % 16 | 0 - currentDateMilliseconds = Math.floor(currentDateMilliseconds / 16) - return (currentChar === 'x' ? randomChar : (randomChar & 0x7 | 0x8)).toString(16) - }) - } - - return crypto !== 'undefined' && crypto !== null - ? typeof (window.crypto.getRandomValues) !== 'undefined' - ? _cryptoGuid() - : _guid() - : _guid() -} - -console.log(Guid()) // 'edc848db-3478-1760-8b55-7986003d895f'