Simplify code
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Andrey Sitnik · committed by Andrey Sitnik · parent cc08873 · Browse files · 3 files·+41 added−38 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+41 added−38 removed
- other:+41 added−38 removed
Provenance
- Commit
14eec75- Tree
104b4cd- Author
Andrey Sitnik <andrey@sitnik.ru>- Committer
Andrey Sitnik <andrey@sitnik.ru>- Parent
cc08873
3 files changed+41 added−38 removed
modified:async/index.js+8 added-6 removed
2 unmodified lines3456787891011124 unmodified lines17181920212222232425262727 unmodified lines555657585859596061626364656666676869707172732 unmodified linesimport { 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. randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {4 unmodified lines resolve(buf) } }) })}
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. // For example, the bitmask for the alphabet size 30 is 31 (00011111).27 unmodified lines
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 })}modified:async/index.native.js+26 added-26 removed
2 unmodified lines34567789101111 unmodified lines232425262728293031323334353637272829303132333435363738394041424344454647484950515253545542434445464748495051525354552 unmodified linesimport { 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. // For example, the bitmask for the alphabet size 30 is 31 (00011111).11 unmodified lines // alphabet size, and magic number 1.6 (using 1.6 peaks at performance // 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}modified:index.js+7 added-6 removed
10 unmodified lines11 // requests exceed the maximum buffer size.12 const POOL_SIZE_MULTIPLIER = 12813 let pool, poolOffset14 15-let fillPool = bytes => {15+function fillPool(bytes) {16 if (!pool || pool.length < bytes) {17 pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)18 randomFillSync(pool)19 poolOffset = 03 unmodified lines23 }24 poolOffset += bytes25 }26 27-export let random = bytes => {27+export function random(bytes) {28 // `-=` convert `bytes` to number to prevent `valueOf` abusing29 fillPool((bytes -= 0))30 return pool.subarray(poolOffset - bytes, poolOffset)31 }32 33-export let customRandom = (alphabet, defaultSize, getRandom) => {33+export function customRandom(alphabet, defaultSize, getRandom) {34 // First, a bitmask is necessary to generate the ID. The bitmask makes bytes35 // values closer to the alphabet size. The bitmask calculates the closest36 // `2^31 - 1` number, which exceeds the alphabet size.37 // For example, the bitmask for the alphabet size 30 is 31 (00011111).26 unmodified lines64 }65 }66 }67 68-export let customAlphabet = (alphabet, size = 21) =>69- customRandom(alphabet, size, random)68+export function customAlphabet(alphabet, size = 21) {69+ return customRandom(alphabet, size, random)70+}71 71-export let nanoid = (size = 21) => {72+export function nanoid(size = 21) {73 // `-=` convert `size` to number to prevent `valueOf` abusing74 fillPool((size -= 0))75 let id = ''76 // We are reading directly from the random pool to avoid creating new array10 unmodified lines