All files / random string.ts

100% Statements 11/11
100% Branches 6/6
100% Functions 1/1
100% Lines 10/10

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 17 18 19 20 214x         4x 18x 1x   17x 17x   154x   154x 154x     17x    
import { isNumber } from '../assert/is-number';
 
/**
 * Generates a random string of length
 */
export function randomString(length: number): string {
  if (!isNumber(length)) {
    throw new Error('randomString must have a length argument');
  }
  let str = '';
  for (let i = 0; i < length; i++) {
    // eslint-disable-next-line: no-magic-numbers
    let rand = Math.floor(Math.random() * 62);
    // eslint-disable-next-line: no-magic-numbers
    const charCode = rand += rand > 9 ? (rand < 36 ? 55 : 61) : 48;
    str += String.fromCharCode(charCode);
  }
 
  return str;
}