All files / base64/src createToBase64.ts

97.29% Statements 36/37
87.5% Branches 7/8
100% Functions 2/2
100% Lines 33/33

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 465x 5x   5x 6x   6x 6x   6x 384x 24576x 24576x       6x 6x   6x 442x 442x 442x 442x 5037x 5037x 5037x 5037x 5037x 5037x   442x 302x 154x 154x   148x 148x 148x 148x 148x   302x      
import {flatstr} from './util/strings/flatstr';
import {alphabet} from './constants';
 
export const createToBase64 = (chars: string = alphabet, pad: string = '=') => {
  Iif (chars.length !== 64) throw new Error('chars must be 64 characters long');
 
  const table = chars.split('');
  const table2: string[] = [];
 
  for (const c1 of table) {
    for (const c2 of table) {
      const two = flatstr(c1 + c2);
      table2.push(two);
    }
  }
 
  const E: string = pad;
  const EE: string = flatstr(pad + pad);
 
  return (uint8: Uint8Array, length: number): string => {
    let out = '';
    const extraLength = length % 3;
    const baseLength = length - extraLength;
    for (let i = 0; i < baseLength; i += 3) {
      const o1 = uint8[i];
      const o2 = uint8[i + 1];
      const o3 = uint8[i + 2];
      const v1 = (o1 << 4) | (o2 >> 4);
      const v2 = ((o2 & 0b1111) << 8) | o3;
      out += table2[v1] + table2[v2];
    }
    if (!extraLength) return out;
    if (extraLength === 1) {
      const o1 = uint8[baseLength];
      out += table2[o1 << 4] + EE;
    } else {
      const o1 = uint8[baseLength];
      const o2 = uint8[baseLength + 1];
      const v1 = (o1 << 4) | (o2 >> 4);
      const v2 = (o2 & 0b1111) << 2;
      out += table2[v1] + table[v2] + E;
    }
    return out;
  };
};