'How do I generate a UUID in SAP UI5 code? [duplicate]
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?
Solution 1:[1]
Generating UUID in plain JavaScript
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.
Generating UID in SAPUI5
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |