All files / src/helpers get-short-token.ts

90% Statements 9/10
75% Branches 3/4
100% Functions 1/1
90% Lines 9/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 21 22  1x 1x   1x       120x 106x     14x 14x   14x       14x    
 
import randomBytes from './random-bytes';
import randomDigits from './random-digits';
 
export default async function getShortToken (
  len: number,
  ifDigits: boolean
): Promise<string> {
  if (ifDigits) {
    return randomDigits(len);
  }
 
  const str1 = await randomBytes(Math.floor(len / 2) + 1);
  let str = str1.substr(0, len);
 
  Iif (str.match(/^[0-9]+$/)) { // tests will fail on all digits
    str = `q${str.substr(1)}`; // shhhh, secret.
  }
 
  return str;
}