UNPKG

rx-player

Version:
30 lines (29 loc) 1.03 kB
import globalScope from "./global_scope"; import getMonotonicTimeStamp from "./monotonic_timestamp"; /** * Create and return a Universally Unique IDentifier (UUID) as defined by * RFC4122. * Depending on browser API availability, we may be generating an approximation * of what the RFC indicates instead. * @returns {string} */ export default function createUuid() { var _a; if (typeof ((_a = globalScope.crypto) === null || _a === void 0 ? void 0 : _a.randomUUID) === "function") { return globalScope.crypto.randomUUID(); } let ts1 = new Date().getTime(); let ts2 = getMonotonicTimeStamp(); return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) { let r = Math.random() * 16; if (ts1 > 0) { r = (ts1 + r) % 16 | 0; ts1 = Math.floor(ts1 / 16); } else { r = (ts2 + r) % 16 | 0; ts2 = Math.floor(ts2 / 16); } return (c === "x" ? r : (r & 0x3) | 0x8).toString(16); }); }