🏠 back to Observable

Import nanoid and uuid

Hi,

it seems that both nanoid and uuid libraries are available through dynamic import only: https://observablehq.com/@severo/import-uuid-and-nanoid.
Is there another way with a better browser support?

(note to myself: another problem is the dependency on jsdeliver, but it can be solved by hosting the module as a notebook attachment)

This polyfill (just copying the work by “uupaa” here) might be useful:

1 Like

OK, thanks!

Here’s a hacky UUID function based on Math.random, which might suffice for some use cases:

uuid = function uuid() {
  var template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
  return template.replace(/[xy]/g, (c) => {
    var r = Math.random() * 16 | 0;
    if (c === 'y') r = r & 3 | 8;
    return r.toString(16);
  });
}

Edit: I had this in a file somewhere on my computer, but I think I cribbed the idea from somewhere online. I can’t claim this is my code.

2 Likes

thanks! I added it to the notebook