1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | import * as math from './math.js'
|
11 | import * as binary from './binary.js'
|
12 | import { cryptoRandomBuffer } from './isomorphic.js'
|
13 |
|
14 | export const rand = Math.random
|
15 |
|
16 | export const uint32 = () => new Uint32Array(cryptoRandomBuffer(4))[0]
|
17 |
|
18 | export const uint53 = () => {
|
19 | const arr = new Uint32Array(cryptoRandomBuffer(8))
|
20 | return (arr[0] & binary.BITS21) * (binary.BITS32 + 1) + (arr[1] >>> 0)
|
21 | }
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | export const oneOf = arr => arr[math.floor(rand() * arr.length)]
|
29 |
|
30 |
|
31 | const uuidv4Template = [1e7] + -1e3 + -4e3 + -8e3 + -1e11
|
32 | export const uuidv4 = () => uuidv4Template.replace(/[018]/g, c =>
|
33 | (c ^ uint32() & 15 >> c / 4).toString(16)
|
34 | )
|