The Problem Statement
GUID is synonym with .Net platforms, Instantiating it from the normal C# syntax in .Net environment is relatively easy, but instantiating it at the front end using Typescript/Javascript is somehow quite tricky, Assuming that we have a frontend logging/crashreport mechanism that is reporting directly to our data store, and to differentiate each logging/crashreport, we have standardized it to use GUID. We are how required to create a GUID generator before assigning its value alongside with our logging/CrashReport.
The Code Snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public guid() { | |
function s4() { | |
return Math.floor((1 + Math.random()) * 0x10000) | |
.toString(16) | |
.substring(1); | |
} | |
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); | |
} | |
// Credit Stack Overflow - https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript |
The Consideration
Based on StackOverflow, The code snipped does not follow the RFC4122 compliance in generating an UUID. To meet the Compliance we might need to tweak our code like below
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generateUUID() { // Public Domain/MIT | |
var d = new Date().getTime(); | |
if (typeof performance !== 'undefined' && typeof performance.now === 'function'){ | |
d += performance.now(); //use high-precision timer if available | |
} | |
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | |
var r = (d + Math.random() * 16) % 16 | 0; | |
d = Math.floor(d / 16); | |
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16); | |
}); | |
} |
Conclusion
From the article, we may conclude that there are many ways to generate an unique identifier for our system, There are certain criteria that has to follow to make it compliance with the industry standard. We may understand more on the discussion on any changes at the Stack Overflow link.
Published on : 4-Jan-2019
Ref no : DG-WPUB-000004
About Author

Comments
Post a Comment