3

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?

1
  • FYI: globalThis.crypto.randomUUID() is now available in all major browsers. Updated my answer accordingly Commented Apr 17, 2022 at 23:01

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.