I know how to do that in node.js, with npm-managed libraries like uuid
. But what's a decent way to do this in browser-based SAP UI5 JavaScript code?
const myUniversallyUniqueID = globalThis.crypto.randomUUID();
console.log(myUniversallyUniqueID); // e.g.: "a38aa6d5-bf7e-4caa-afe1-0144507e215c"
crypto.randomUUID
is now supported by all major JS engines. I.e. globalThis.crypto.randomUUID()
can be used in browsers as well as in Deno / Node.js.
Not a UUID, but UI5 provides the function module sap/base/util/uid
which returns a string that is unique within the same JavaScript context (I.e. it is very possible to generate the exact same UIDs when two iframes run uid()
at the same time).
const myUniqueID = uid(); // uid required from "sap/base/util/uid"
console.log(myUniqueID); // e.g.: "id-1650235945092-39"
The implementation is fairly simple and easy to understand.