All files / utils thumbs.js

100% Statements 25/25
87.5% Branches 7/8
100% Functions 3/3
100% Lines 25/25
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        15x 15x 15x 15x 15x     1x 1x       1x 1x   1x 3x   3x 1x     2x 2x   2x 1x 1x     2x 2x   2x 2x       1x         2x   2x              
/* @flow */
 
import type { thumbB64 } from "../client/types";
 
export const THUMB_FILEID = 0;
export const THUMB_RESULT = 1;
export const THUMB_SIZE = 2;
export const THUMB_LINEID = 2;
export const THUMB_URL = 3;
 
export default function createParser() {
  let lastLinePos = 0;
  let thumbs = [];
  let nextLinePos;
  //let currentLine;
 
  return (text: string): Array<thumbB64> => {
    let setThumbs = [];
 
    while (1) {
      nextLinePos = text.indexOf("\n", lastLinePos + 1);
 
      if (nextLinePos === -1) {
        break;
      }
 
      let { result, size, url, fileid } = _thumbObj(text.substr(lastLinePos, nextLinePos - lastLinePos));
      lastLinePos = nextLinePos;
 
      if (result === 6001) {
        url = thumbs[parseInt(size, 10)].url;
        result = 0;
      }
 
      Eif (result === 0) {
        const thumb = { url, fileid };
 
        thumbs.push(thumb);
        setThumbs.push(thumb);
      }
    }
 
    return setThumbs;
  };
}
 
function _thumbObj(line: string) {
  const obj = line.split("|");
 
  return {
    result: parseInt(obj[THUMB_RESULT], 10),
    url: THUMB_URL in obj ? obj[THUMB_URL].trim() : "",
    fileid: parseInt(obj[THUMB_FILEID], 10),
    size: obj[THUMB_SIZE]
  };
}