Skip to content
Myr

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

3 files+41 added−38 removed

modified:async/index.js+8 added-6 removed
modified:async/index.native.js+26 added-26 removed
modified:index.js+7 added-6 removed
10 unmodified lines
11// requests exceed the maximum buffer size.12const POOL_SIZE_MULTIPLIER = 12813let pool, poolOffset1415let fillPool = bytes => {15function fillPool(bytes) {16 if (!pool || pool.length < bytes) {17 pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)18 randomFillSync(pool)19 poolOffset = 0
3 unmodified lines
23 }24 poolOffset += bytes25}2627export let random = bytes => {27export function random(bytes) {28 // `-=` convert `bytes` to number to prevent `valueOf` abusing29 fillPool((bytes -= 0))30 return pool.subarray(poolOffset - bytes, poolOffset)31}3233export let customRandom = (alphabet, defaultSize, getRandom) => {33export 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 lines
64 }65 }66}6768export let customAlphabet = (alphabet, size = 21) =>69 customRandom(alphabet, size, random)68export function customAlphabet(alphabet, size = 21) {69 return customRandom(alphabet, size, random)70}7171export let nanoid = (size = 21) => {72export 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 array
10 unmodified lines