{"id":7898,"date":"2021-02-14T01:23:00","date_gmt":"2021-02-14T07:23:00","guid":{"rendered":"https:\/\/upmostly.com\/?p=7898"},"modified":"2022-11-01T09:47:41","modified_gmt":"2022-11-01T14:47:41","slug":"generating-a-uuid-guid-in-javascript-angular","status":"publish","type":"post","link":"http:\/\/upmostly.com\/angular\/generating-a-uuid-guid-in-javascript-angular","title":{"rendered":"Generating A UUID\/GUID In JavaScript\/Angular"},"content":{"rendered":"\n<p>One of the more frustrating limitations of vanilla JavaScript is that there is actually no GUID or UUID generator in the language. That\u2019s right, out of the box, you cannot generate a GUID with a nice single line of code! Very annoying.<\/p>\n\n\n\n<p>Luckily, there are two ways that I\u2019ve used in the past to generate GUID or GUID-like strings.<\/p>\n\n\n\n<p>This first piece of code is one I use in places where the randomness isn\u2019t such a big deal. It might be just generating correlation ids, or just as a way to generate some randomized letters\/numbers, but not global uniqueness and\/or collisions aren\u2019t such a big deal.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">generateGuid() : string {\n  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(\/[xy]\/g, function(c) {\n    var r = Math.random() * 16 | 0,\n      v = c == 'x' ? r : (r &amp; 0x3 | 0x8);\n    return v.toString(16);\n  });\n}<\/code><\/pre>\n\n\n\n<p>In most cases where I\u2019m generating a GUID on the front end, I\u2019m not worried too much about how secure things are. You shouldn\u2019t be too concerned on collisions in the browser anyway because a user could just as easily inject JavaScript and\/or modify an API call that uses the GUID into using their own string.<\/p>\n\n\n\n<p>However, for a more secure GUID,&nbsp;<a href=\"https:\/\/www.npmjs.com\/package\/uuid\" target=\"_blank\" rel=\"noreferrer noopener\">there is obviously an NPM package for that<\/a>!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">npm install uuid<\/code><\/pre>\n\n\n\n<p>Then use the uuidv4 function to generate guids as you need them!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">import { v4 as uuidv4 } from 'uuid';\nlet myGuid = uuidv4();<\/code><\/pre>\n\n\n\n<p>Again, in most cases I just use the first function. But I know people come up with all sorts of links on how \u201cMath.random()\u201d isn\u2019t truly random etc. So both options are there!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the more frustrating limitations of vanilla JavaScript is that there is actually no GUID or UUID generator in the language. That\u2019s right, out of the box, you cannot generate a GUID with a nice single line of code! Very annoying. Luckily, there are two ways that I\u2019ve used in the past to generate [&hellip;]<\/p>\n","protected":false},"author":131,"featured_media":7901,"comment_status":"open","ping_status":"open","sticky":false,"template":"post-tutorial-new.php","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[150],"tags":[],"class_list":["post-7898","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular"],"acf":[],"_links":{"self":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts\/7898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/users\/131"}],"replies":[{"embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/comments?post=7898"}],"version-history":[{"count":4,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts\/7898\/revisions"}],"predecessor-version":[{"id":13563,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts\/7898\/revisions\/13563"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/media\/7901"}],"wp:attachment":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/media?parent=7898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/categories?post=7898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/tags?post=7898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}