All files / src/utils images.ts

46.15% Statements 12/26
37.5% Branches 6/16
100% Functions 1/1
46.15% Lines 12/26

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            5x                     5x       51x           51x   10x 10x         41x                           41x 41x               41x   41x 41x        
// s = Small Square (90×90)
// b = Big Square (160×160)
// t = Small Thumbnail (160×160)
// m = Medium Thumbnail (320×320)
// l = Large Thumbnail (640×640)
// h = Huge Thumbnail (1024×1024)
const defaultPicture = "M795H8A.jpg";
 
export enum SizeMapping {
  smallSquare = "s",
  bigSquare = "b",
  small = "t",
  medium = "m",
  large = "l",
  huge = "h",
}
 
export const parseImgur = (
  rawImage: string,
  size: SizeMapping = SizeMapping.large
) => {
  Iif (!rawImage) {
    return `https://i.imgur.com/${defaultPicture}`;
  }
 
  // Don't resize the gif image
  // as there is a transparent bug in imgur
  if (rawImage.match("gif")) {
    // Prevent double http url
    Eif (rawImage.match("http")) {
      return rawImage;
    }
    return `https://i.imgur.com/${rawImage}`;
  }
  let mappedSize: string;
  switch (size) {
    case SizeMapping.smallSquare:
      mappedSize = SizeMapping.smallSquare;
      break;
    case SizeMapping.bigSquare:
      mappedSize = SizeMapping.bigSquare;
      break;
    case SizeMapping.small:
      mappedSize = SizeMapping.small;
      break;
    case SizeMapping.medium:
      mappedSize = SizeMapping.medium;
      break;
    case SizeMapping.large:
      mappedSize = SizeMapping.large;
      break;
    case SizeMapping.huge:
      mappedSize = SizeMapping.huge;
      break;
    default:
      mappedSize = "";
  }
 
  const resizedImage = rawImage.replace(/(.*)\.(.*)/, `$1${mappedSize}.$2`);
  // Prevent double http url
  Eif (resizedImage.match("http")) {
    return resizedImage;
  }
  return `https://i.imgur.com/${resizedImage}`;
};