UNPKG

1.94 kBSource Map (JSON)View Raw
1{"version":3,"file":"random.cjs","sources":["../random.js"],"sourcesContent":["/**\n * Isomorphic module for true random numbers / buffers / uuids.\n *\n * Attention: falls back to Math.random if the browser does not support crypto.\n *\n * @module random\n */\n\nimport * as math from './math.js'\nimport * as binary from './binary.js'\nimport { getRandomValues } from 'lib0/webcrypto'\n\nexport const rand = Math.random\n\nexport const uint32 = () => getRandomValues(new Uint32Array(1))[0]\n\nexport const uint53 = () => {\n const arr = getRandomValues(new Uint32Array(8))\n return (arr[0] & binary.BITS21) * (binary.BITS32 + 1) + (arr[1] >>> 0)\n}\n\n/**\n * @template T\n * @param {Array<T>} arr\n * @return {T}\n */\nexport const oneOf = arr => arr[math.floor(rand() * arr.length)]\n\n// @ts-ignore\nconst uuidv4Template = [1e7] + -1e3 + -4e3 + -8e3 + -1e11\n\n/**\n * @return {string}\n */\nexport const uuidv4 = () => uuidv4Template.replace(/[018]/g, /** @param {number} c */ c =>\n (c ^ uint32() & 15 >> c / 4).toString(16)\n)\n"],"names":["getRandomValues","binary.BITS21","binary.BITS32","math.floor"],"mappings":";;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AAKA;AACY,MAAC,IAAI,GAAG,IAAI,CAAC,OAAM;AAC/B;AACY,MAAC,MAAM,GAAG,MAAMA,yBAAe,CAAC,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAC;AAClE;AACY,MAAC,MAAM,GAAG,MAAM;AAC5B,EAAE,MAAM,GAAG,GAAGA,yBAAe,CAAC,IAAI,WAAW,CAAC,CAAC,CAAC,EAAC;AACjD,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,GAAGC,aAAa,KAAKC,aAAa,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AACxE,EAAC;AACD;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,KAAK,GAAG,GAAG,IAAI,GAAG,CAACC,UAAU,CAAC,IAAI,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC,EAAC;AAChE;AACA;AACA,MAAM,cAAc,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,KAAI;AACzD;AACA;AACA;AACA;AACY,MAAC,MAAM,GAAG,MAAM,cAAc,CAAC,OAAO,CAAC,QAAQ,2BAA2B,CAAC;AACvF,EAAE,CAAC,CAAC,GAAG,MAAM,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,EAAE,CAAC;AAC3C;;;;;;;;"}
\No newline at end of file