-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.browser.ts
39 lines (34 loc) · 1.85 KB
/
format.browser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Borrowed from https://github.com/ai/nanoid/blob/master/format.browser.js
export default function (random: (bytes: number) => Uint8Array, alphabet: string, size: number) {
// We can’t use bytes bigger than the alphabet. To make bytes values closer
// to the alphabet, we apply bitmask on them. We look for the closest
// `2 ** x - 1` number, which will be bigger than alphabet size. If we have
// 30 symbols in the alphabet, we will take 31 (00011111).
// We do not use faster Math.clz32, because it is not available in browsers.
const mask = (2 << Math.log(alphabet.length - 1) / Math.LN2) - 1
// Bitmask is not a perfect solution (in our example it will pass 31 bytes,
// which is bigger than the alphabet). As a result, we will need more bytes,
// than ID size, because we will refuse bytes bigger than the alphabet.
// Every hardware random generator call is costly,
// because we need to wait for entropy collection. This is why often it will
// be faster to ask for few extra bytes in advance, to avoid additional calls.
// Here we calculate how many random bytes should we call in advance.
// It depends on ID length, mask / alphabet size and magic number 1.6
// (which was selected according benchmarks).
// -~f => Math.ceil(f) if n is float number
// -~i => i + 1 if n is integer number
const step = -~(1.6 * mask * size / alphabet.length)
let id = ""
while (true) {
const bytes = random(step)
// Compact alternative for `for (var i = 0; i < step; i++)`
let i = step
while (i--) {
// If random byte is bigger than alphabet even after bitmask,
// we refuse it by `|| ''`.
id += alphabet[bytes[i] & mask] || ""
// More compact than `id.length + 1 === size`
if (id.length === +size) return id
}
}
}