Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Mar 25, 2023
1 parent edd3e97 commit cabeabe
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
14 changes: 8 additions & 6 deletions async/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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
})
}
52 changes: 26 additions & 26 deletions async/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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 = ''
Expand Down

0 comments on commit cabeabe

Please # to comment.