All files base64.ts

90.28% Statements 65/72
90% Branches 18/20
100% Functions 4/4
90.28% Lines 65/72

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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141              10x                   10x 109x 109x 109x 109x 522x 522x 522x 522x 522x 522x 522x 522x 50x 472x 35x   522x             109x                     10x 2x 2x   2x   2x 134x 134x 134x 134x   134x 134x 134x   134x   134x 133x   134x 133x       2x   2x             10x 109x 109x   109x 1422x   1422x 1413x 9x 9x 9x               109x             10x 2x 2x 2x 2x 2x 2x 391x   391x 382x 382x 9x 9x 9x 9x                   2x    
/**
 * @documentation true
 * @sectionName Base64
 */
 
/* tslint:disable */
const keyStr =
  'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
 
/**
 * Encode string with base64 encoding.
 * @signature
 *    P.base64encode(str)
 * @example
 *    P.base64encode("ts-prime is awesome") //=> dHMtcHJpbWUgaXMgYXdlc29tZQ==
 * @category Utility
 */
export function base64encode(input: string): string {
  let output = '';
  let i = 0;
  input = encodeUTF8(input);
  while (i < input.length) {
    const chr1 = input.charCodeAt(i++);
    const chr2 = input.charCodeAt(i++);
    const chr3 = input.charCodeAt(i++);
    const enc1 = chr1 >> 2;
    const enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
    let enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
    let enc4 = chr3 & 63;
    if (isNaN(chr2)) {
      enc3 = enc4 = 64;
    } else if (isNaN(chr3)) {
      enc4 = 64;
    }
    output =
      output +
      keyStr.charAt(enc1) +
      keyStr.charAt(enc2) +
      keyStr.charAt(enc3) +
      keyStr.charAt(enc4);
  }
  return output;
}
 
/**
 * Decode base64 encoded string.
 * @signature
 *    P.base64decode(str)
 * @example
 *    P.base64decode("dHMtcHJpbWUgaXMgYXdlc29tZQ==") //=> ts-prime is awesome
 * @category Utility
 */
export function base64decode(input: string): string {
  let output = '';
  let i = 0;
 
  input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
 
  while (i < input.length) {
    const enc1 = keyStr.indexOf(input.charAt(i++));
    const enc2 = keyStr.indexOf(input.charAt(i++));
    const enc3 = keyStr.indexOf(input.charAt(i++));
    const enc4 = keyStr.indexOf(input.charAt(i++));
 
    const chr1 = (enc1 << 2) | (enc2 >> 4);
    const chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
    const chr3 = ((enc3 & 3) << 6) | enc4;
 
    output = output + String.fromCharCode(chr1);
 
    if (enc3 !== 64) {
      output = output + String.fromCharCode(chr2);
    }
    if (enc4 !== 64) {
      output = output + String.fromCharCode(chr3);
    }
  }
 
  output = decodeUTF8(output);
 
  return output;
}
 
/**
 * Encode UTF8 characters
 * @category Utility
 */
export function encodeUTF8(input: string): string {
  input = input.replace(/\r\n/g, '\n');
  let utftext = '';
 
  for (let n = 0; n < input.length; n++) {
    const c = input.charCodeAt(n);
 
    if (c < 128) {
      utftext += String.fromCharCode(c);
    } else Eif (c > 127 && c < 2048) {
      utftext += String.fromCharCode((c >> 6) | 192);
      utftext += String.fromCharCode((c & 63) | 128);
    } else {
      utftext += String.fromCharCode((c >> 12) | 224);
      utftext += String.fromCharCode(((c >> 6) & 63) | 128);
      utftext += String.fromCharCode((c & 63) | 128);
    }
  }
 
  return utftext;
}
 
/**
 * Decode UTF8 encoded characters
 * @category Utility
 */
export function decodeUTF8(utftext: string): string {
  let str = '';
  let i = 0;
  let c1 = 0;
  let c2 = 0;
  let c3 = 0;
  while (i < utftext.length) {
    c1 = utftext.charCodeAt(i);
 
    if (c1 < 128) {
      str += String.fromCharCode(c1);
      i++;
    } else Eif (c1 > 191 && c1 < 224) {
      c2 = utftext.charCodeAt(i + 1);
      str += String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
      i += 2;
    } else {
      c2 = utftext.charCodeAt(i + 1);
      c3 = utftext.charCodeAt(i + 2);
      str += String.fromCharCode(
        ((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)
      );
      i += 3;
    }
  }
  return str;
}