All files / src random-identifier.ts

100% Statements 10/10
0% Branches 0/2
100% Functions 3/3
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1722x     22x     22x 32x 32x       22x 32x   22x  
import identifier from './identifier.js'
import type Sid from './sanitised-identifier.js'
 
const alphabet = 'abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ'
 
const randomLetter =
  (): string => {
    const index = Math.min(alphabet.length - 1, Math.floor(Math.random() * alphabet.length))
    return alphabet[index]
  }
 
const randomIdentifier =
  (prefix = '', length = 32): Sid =>
    identifier([ prefix, Array.from(Array(length), () => randomLetter()).join('') ].join(''))
 
export default randomIdentifier