CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
index.browser.js43 linesDownload Raw Back to async
1let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes))2 3let customAlphabet = (alphabet, defaultSize = 21) => {4  let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 15 6 7 8  let step = -~((1.6 * mask * defaultSize) / alphabet.length)9 10  return async (size = defaultSize) => {11    let id = ''12    while (true) {13      let bytes = crypto.getRandomValues(new Uint8Array(step))14      let i = step | 015      while (i--) {16        id += alphabet[bytes[i] & mask] || ''17        if (id.length === size) return id18      }19    }20  }21}22 23let nanoid = async (size = 21) => {24  let id = ''25  let bytes = crypto.getRandomValues(new Uint8Array((size |= 0)))26 27  while (size--) {28    let byte = bytes[size] & 6329    if (byte < 36) {30      id += byte.toString(36)31    } else if (byte < 62) {32      id += (byte - 26).toString(36).toUpperCase()33    } else if (byte < 63) {34      id += '_'35    } else {36      id += '-'37    }38  }39  return id40}41 42export { nanoid, customAlphabet, random }43