From 78e405c29c665e39a69631aedbd0fa1a576b350d Mon Sep 17 00:00:00 2001 From: Andrey Sitnik Date: Thu, 1 Feb 2024 20:52:04 +0100 Subject: [PATCH] Clean up code style --- index.browser.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/index.browser.js b/index.browser.js index 27f4fd7f..bcd2230e 100644 --- a/index.browser.js +++ b/index.browser.js @@ -1,6 +1,8 @@ // This file replaces `index.js` in bundlers like webpack or Rollup, // according to `browser` config in `package.json`. -import { urlAlphabet } from './url-alphabet/index.js'; + +import { urlAlphabet } from './url-alphabet/index.js' + export { urlAlphabet } from './url-alphabet/index.js' export let random = bytes => crypto.getRandomValues(new Uint8Array(bytes)) @@ -47,12 +49,14 @@ export let customRandom = (alphabet, defaultSize, getRandom) => { export let customAlphabet = (alphabet, size = 21) => customRandom(alphabet, size, random) -export let nanoid = (len = 21) => { - let id = ""; - let rand = crypto.getRandomValues(new Uint8Array(len)); - // Using the bitwise AND operator to "cap" the value of - // the random byte from 255 to 63, in that way we can make sure - // that the value will be a valid index for the "chars" string. - for (let i = 0; i < len; i++) id += urlAlphabet[rand[i] & 63]; - return id; +export let nanoid = (size = 21) => { + let id = '' + let rand = crypto.getRandomValues(new Uint8Array(size)) + for (let i = 0; i < size; i++) { + // Using the bitwise AND operator to "cap" the value of + // the random byte from 255 to 63, in that way we can make sure + // that the value will be a valid index for the "chars" string. + id += urlAlphabet[rand[i] & 63] + } + return id }