diff --git a/async/index.js b/async/index.js index 0602637a..2836fcf2 100644 --- a/async/index.js +++ b/async/index.js @@ -4,8 +4,8 @@ import { urlAlphabet } from '../url-alphabet/index.js' // `crypto.randomFill()` is a little faster than `crypto.randomBytes()`, // because it is possible to use in combination with `Buffer.allocUnsafe()`. -export let random = bytes => - new Promise((resolve, reject) => { +export function random(bytes) { + return new Promise((resolve, reject) => { // `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory. // Memory flushing is unnecessary since the buffer allocation itself resets // the memory with the new bytes. @@ -18,8 +18,9 @@ export let random = bytes => } }) }) +} -export let customAlphabet = (alphabet, defaultSize = 21) => { +export function customAlphabet(alphabet, defaultSize = 21) { // First, a bitmask is necessary to generate the ID. The bitmask makes bytes // values closer to the alphabet size. The bitmask calculates the closest // `2^31 - 1` number, which exceeds the alphabet size. @@ -55,17 +56,18 @@ export let customAlphabet = (alphabet, defaultSize = 21) => { return size => tick('', size) } -export let nanoid = (size = 21) => - random(size).then(bytes => { +export function nanoid(size = 21) { + return random(size).then(bytes => { let id = '' // A compact alternative for `for (var i = 0; i < step; i++)`. while (size--) { // It is incorrect to use bytes exceeding the alphabet size. // The following mask reduces the random byte in the 0-255 value // range to the 0-63 value range. Therefore, adding hacks, such - // as empty string fallback or magic numbers, is unneccessary because + // as empty string fallback or magic numbers, is unnecessary because // the bitmask trims bytes down to the alphabet size. id += urlAlphabet[bytes[size] & 63] } return id }) +} diff --git a/async/index.native.js b/async/index.native.js index b2fbeb43..f35e1b04 100644 --- a/async/index.native.js +++ b/async/index.native.js @@ -4,7 +4,7 @@ import { urlAlphabet } from '../url-alphabet/index.js' export let random = getRandomBytesAsync -export let customAlphabet = (alphabet, defaultSize = 21) => { +export function customAlphabet(alphabet, defaultSize = 21) { // First, a bitmask is necessary to generate the ID. The bitmask makes bytes // values closer to the alphabet size. The bitmask calculates the closest // `2^31 - 1` number, which exceeds the alphabet size. @@ -24,32 +24,32 @@ export let customAlphabet = (alphabet, defaultSize = 21) => { // according to benchmarks). let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length) - let tick = (id, size = defaultSize) => - random(step).then(bytes => { - // A compact alternative for `for (var i = 0; i < step; i++)`. - let i = step - while (i--) { - // Adding `|| ''` refuses a random byte that exceeds the alphabet size. - id += alphabet[bytes[i] & mask] || '' - if (id.length === size) return id - } - return tick(id, size) - }) + async function tick(id, size = defaultSize) { + let bytes = await random(step) + // A compact alternative for `for (var i = 0; i < step; i++)`. + let i = step + while (i--) { + // Adding `|| ''` refuses a random byte that exceeds the alphabet size. + id += alphabet[bytes[i] & mask] || '' + if (id.length === size) return id + } + return tick(id, size) + } return size => tick('', size) } -export let nanoid = (size = 21) => - random(size).then(bytes => { - let id = '' - // A compact alternative for `for (var i = 0; i < step; i++)`. - while (size--) { - // It is incorrect to use bytes exceeding the alphabet size. - // The following mask reduces the random byte in the 0-255 value - // range to the 0-63 value range. Therefore, adding hacks, such - // as empty string fallback or magic numbers, is unneccessary because - // the bitmask trims bytes down to the alphabet size. - id += urlAlphabet[bytes[size] & 63] - } - return id - }) +export async function nanoid(size = 21) { + let bytes = await random(size) + let id = '' + // A compact alternative for `for (var i = 0; i < step; i++)`. + while (size--) { + // It is incorrect to use bytes exceeding the alphabet size. + // The following mask reduces the random byte in the 0-255 value + // range to the 0-63 value range. Therefore, adding hacks, such + // as empty string fallback or magic numbers, is unneccessary because + // the bitmask trims bytes down to the alphabet size. + id += urlAlphabet[bytes[size] & 63] + } + return id +} diff --git a/index.js b/index.js index a7f828a3..f2301745 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,7 @@ export { urlAlphabet } const POOL_SIZE_MULTIPLIER = 128 let pool, poolOffset -let fillPool = bytes => { +function fillPool(bytes) { if (!pool || pool.length < bytes) { pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER) randomFillSync(pool) @@ -24,13 +24,13 @@ let fillPool = bytes => { poolOffset += bytes } -export let random = bytes => { +export function random(bytes) { // `-=` convert `bytes` to number to prevent `valueOf` abusing fillPool((bytes -= 0)) return pool.subarray(poolOffset - bytes, poolOffset) } -export let customRandom = (alphabet, defaultSize, getRandom) => { +export function customRandom(alphabet, defaultSize, getRandom) { // First, a bitmask is necessary to generate the ID. The bitmask makes bytes // values closer to the alphabet size. The bitmask calculates the closest // `2^31 - 1` number, which exceeds the alphabet size. @@ -65,10 +65,11 @@ export let customRandom = (alphabet, defaultSize, getRandom) => { } } -export let customAlphabet = (alphabet, size = 21) => - customRandom(alphabet, size, random) +export function customAlphabet(alphabet, size = 21) { + return customRandom(alphabet, size, random) +} -export let nanoid = (size = 21) => { +export function nanoid(size = 21) { // `-=` convert `size` to number to prevent `valueOf` abusing fillPool((size -= 0)) let id = ''