// This file replaces `format.js` in bundlers like webpack or Rollup, // according to `browser` config in `package.json`. module.exports = function (random, alphabet, size) { // 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. var 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 var step = -~(1.6 * mask * size / alphabet.length) var id = '' while (true) { var bytes = random(step) // Compact alternative for `for (var i = 0; i < step; i++)` var 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 } } }