CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
index.native.js36 linesDownload Raw Back to async
1import { getRandomBytesAsync } from 'expo-random'2 3import { urlAlphabet } from '../url-alphabet/index.js'4 5let random = getRandomBytesAsync6 7let customAlphabet = (alphabet, defaultSize = 21) => {8  let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 19 10 11  let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)12 13  let tick = (id, size = defaultSize) =>14    random(step).then(bytes => {15      let i = step16      while (i--) {17        id += alphabet[bytes[i] & mask] || ''18        if (id.length >= size) return id19      }20      return tick(id, size)21    })22 23  return size => tick('', size)24}25 26let nanoid = (size = 21) =>27  random((size |= 0)).then(bytes => {28    let id = ''29    while (size--) {30      id += urlAlphabet[bytes[size] & 63]31    }32    return id33  })34 35export { nanoid, customAlphabet, random }36