如何获取有角度的图像扩展?

时间:2018-12-22 15:47:16

标签: javascript angular

在这里,我使用以下代码检查图像扩展名。 imageName.split(".").pop()使用split and pop得到了图像扩展名,但是在此之后,我必须检查此扩展名是否与此匹配,因为我必须使用倍数或条件进行检查,因此代码的生成过程很慢,因此有可能减少代码并减少OR条件。

例如如果只有一个条件(extn == jpeg)时的jpEg扩展名图像被选中,则所有类型的小写大写扩展名 ('jpeg','Jpeg','jPeg','jpEg','jpeG','JPeg','JpEg','JpeG','jPEg','jPeG','jpEG','jPEG','JPeG','JPEg','JpEG','JPEG')赚钱或赚钱

previewFiles(imageName){
 console.log(imagename);  // 125.jpEg

 var extn = imageName.split(".").pop();

   console.log(extn);  // jpEg

  if((extn == 'pdf') || (extn == 'PDF') || (extn == 'pdF') || (extn == 'pDf') ||
     (extn == 'pDF') || (extn == 'PDf') || (extn == 'PdF') || (extn == 'Pdf') ||
     (extn == 'jpg') || (extn == 'JPG') || (extn == 'jpG') || (extn == 'jPg') ||
     (extn == 'jPG') || (extn == 'JPg') || (extn == 'JpG') || (extn == 'Jpg') ||
     (extn == 'gif') || (extn == 'GIF') || (extn == 'giF') || (extn == 'gIf') ||
     (extn == 'gIF') || (extn == 'GIf') || (extn == 'GiF') || (extn == 'Gif') ||
     (extn == 'png') || (extn == 'PNG') || (extn == 'pnG') || (extn == 'pNg') ||
     (extn == 'pNG') || (extn == 'PNg') || (extn == 'PnG') || (extn == 'Png') ||
     (extn == 'tif') || (extn == 'TIF') || (extn == 'tiF') || (extn == 'tIf') ||
     (extn == 'tIF') || (extn == 'TIf') || (extn == 'TiF') || (extn == 'Tif') ||
     (extn == 'jpeg') || (extn == 'Jpeg') || (extn == 'jPeg') || (extn == 'jpEg') ||
     (extn == 'jpeG') || (extn == 'JPeg') || (extn == 'JpEg') || (extn == 'JpeG') ||
     (extn == 'jPEg') || (extn == 'jPeG' ) || (extn == 'jpEG' ) || (extn == 'jPEG' ) ||
     (extn == 'JPeG' ) || (extn == 'JPEg') || (extn == 'JpEG') || (extn == 'JPEG') ||
     (extn == 'tiff') || (extn == 'Tiff' ) || (extn == 'tIff') || (extn == 'tiFf' ) ||
    (extn == 'tifF') || (extn == 'TIff') || (extn == 'TiFf') || (extn == 'TifF') ||
     (extn == 'tIFf') || (extn == 'tIfF') || (extn == 'tiFF') || (extn == 'tIFF') ||
     (extn == 'TIfF') || (extn == 'TIFf') || (extn == 'TiFF') || (extn == 'TIFF')){
      console.log("extension of image is matched");    
   }else{
      console.log("extension of image is not matched");
   }
}

2 个答案:

答案 0 :(得分:3)

您可以简单地在具有每个extn.toLowerCase()JpEg用小写jpEg表示形式的字符串上调用jpeg

您还可以做的是在另一个变量中使用可用的扩展名,并检查给定的扩展名是否与数组中的扩展名匹配

const availableExtensions = new Set(['jpeg', 'tiff', 'gif'])

if(availableExtensions.has(extn.toLowerCase())) {
    console.log("extension of image is matched")
}

答案 1 :(得分:3)

您可以使用带正则表达式的正则表达式和/i进行不区分大小写的比较,或使用toLowerCase进行字符串比较:

let extn = "TIFf";
const regex = /(?:pdf|jpeg|jpg|tiff?|gif|png)/i;
if (regex.test(extn)) {
  console.log(extn);
}

let valid = [
  "pdf", "tif", "tiff", "jpg", "jpeg", "gif", "png"
];

if (valid.includes(extn.toLowerCase())) {
  console.log(extn);
}