source: frontend/node_modules/nanoid/index.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.4 KB
Line 
1import crypto from 'crypto'
2import { urlAlphabet } from './url-alphabet/index.js'
3const POOL_SIZE_MULTIPLIER = 128
4let pool, poolOffset
5let fillPool = bytes => {
6 if (bytes < 0 || bytes > 1024) throw new RangeError('Wrong ID size')
7 if (!pool || pool.length < bytes) {
8 pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
9 crypto.randomFillSync(pool)
10 poolOffset = 0
11 } else if (poolOffset + bytes > pool.length) {
12 crypto.randomFillSync(pool)
13 poolOffset = 0
14 }
15 poolOffset += bytes
16}
17let random = bytes => {
18 fillPool((bytes |= 0))
19 return pool.subarray(poolOffset - bytes, poolOffset)
20}
21let customRandom = (alphabet, defaultSize, getRandom) => {
22 let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
23 let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
24 return (size = defaultSize) => {
25 let id = ''
26 while (true) {
27 let bytes = getRandom(step)
28 let i = step
29 while (i--) {
30 id += alphabet[bytes[i] & mask] || ''
31 if (id.length === size) return id
32 }
33 }
34 }
35}
36let customAlphabet = (alphabet, size = 21) =>
37 customRandom(alphabet, size, random)
38let nanoid = (size = 21) => {
39 fillPool((size |= 0))
40 let id = ''
41 for (let i = poolOffset - size; i < poolOffset; i++) {
42 id += urlAlphabet[pool[i] & 63]
43 }
44 return id
45}
46export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
Note: See TracBrowser for help on using the repository browser.