0

I have a function that returns a string indicating the image to show for a particular file extension:

getImage = function (file) {
    switch (file.extension) {
        case "txt":
            return "document.jpg";
        case "doc":
            return "document.jpg";
        case "jpg":
            return "image.jpg";
        case "gif":
            return "image.jpg";
        case "png":
            return "image.jpg";
        case "mpg":
            return "video.jpg";

       // And so on for 50+ file extensions...

        default:
            return "file.jpg";
    }
}

I need to support a large number of file extensions, so my switch statement is huge. But since a lot of the file extensions share the same image, I feel like there may be a way to group extensions together to make things more concise.

So how can I re-write this in a more concise manner? (Any answer needs to be compatible with IE8)

XåpplI'-I0llwlg'I -
  • 21,649
  • 28
  • 102
  • 151
  • 2
    In relation to the 700 identical answers - some more than likely inspired by others - you should consider using the same file name for all your extensions. That way you wont have to worry about babysitting 50+ extensions. – Birb Dec 16 '13 at 07:08

10 Answers10

4

You can combine cases:

getImage = function (file) {
    switch (file.extension) {
        case "txt":
        case "doc":
            return "document.jpg";
        case "jpg":
        case "gif":
        case "png":
            return "image.jpg";
        case "mpg":
            return "video.jpg";

       // And so on for 50+ file extensions...

        default:
            return "file.jpg";
    }
}

You can also use a map instead:

var extmap = {
    "txt": "document.jpg",
    "doc": "document.jpg",
    "jpg": "image.jpg",
    "gif": "image.jpg",
    "png": "image.jpg",
    "mpg": "video.jpg",
    // And so on for 50+ file extensions...
};
getImage = extmap[file.extension] || "file.jpg";

...but then you can't combine cases.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2
var types = {"txt":"document.jpg", "doc":"document.jpg"}
return types[file.extension]
Igor Benikov
  • 884
  • 6
  • 21
2
getImage = function (file) {
  var img  = ['jpg', 'gif', 'png' ...]
  ,   doc  = ['doc', 'text' ...]
  ,   vid  = ['mpg', 'mp4' ...]
  ,   ext  = file.extension
  ;
  if (img.indexOf(ext) >= 0) return 'image.jpg';
  if (doc.indexOf(ext) >= 0) return 'document.jpg';
  if (vid.indexOf(ext) >= 0) return 'video.jpg';

  return 'file.jpg';
 }
Jay Harris
  • 4,201
  • 17
  • 21
1

You can combine case for same return value

getImage = function (file) {
    switch (file.extension) {
        case "txt":
        case "doc":
            return "document.jpg";
        case "jpg":
        case "gif":
        case "png":
            return "image.jpg";
        case "mpg":
            return "video.jpg";

       // And so on for 50+ file extensions...

        default:
            return "file.jpg";
    }
}
Satpal
  • 132,252
  • 13
  • 159
  • 168
1

As mentioned in other answers, you can combine cases. This would be better in your case since one image maps to multiple extensions. Other possibility is to use an object literal:

getImage = function (file) {
    var imageList = {
        txt: "document.jpg",
        doc: "document.jpg",
        // ...
    };
    return imageList[file.extension] || "file.jpg";
}
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • Thank you for breaking the pattern of copy/paste people, and providing a much nicer way of doing things. I at least prefer to have as little actual code (logic) as possible. – Birb Dec 16 '13 at 07:10
0

One simple solution could be instead of switch use if

if( file.extension == "txt" || file.extension == "doc" || ... )
   return "image.jpg";


The switch statement is used to perform different action based on different conditions

in your case I think if is better.

A Paul
  • 8,113
  • 3
  • 31
  • 61
0

Can you try this, You can group the case statement

getImage = function (file) {
    switch (file.extension) {
        case "txt":            
        case "doc":
            return "document.jpg";
        case "jpg":            
        case "gif":            
        case "png":
            return "image.jpg";
        case "mpg":
            return "video.jpg";

       // And so on for 50+ file extensions...

        default:
            return "file.jpg";
    }
}
Krish R
  • 22,583
  • 7
  • 50
  • 59
0

If you want to use switch to group the extension, you could use this:

switch (file.extension) {
        case "txt":
        case "doc":
            return "document.jpg";
        case "jpg":
        case "gif":
        case "png":
            return "image.jpg";
        case "mpg":
            return "video.jpg";

       // And so on for 50+ file extensions...

        default:
            return "file.jpg";
    }
RRikesh
  • 14,112
  • 5
  • 49
  • 70
0

you can use a dictionary,

var format_dict = {"txt":"document.jpg", "doc":"document.jpg","mpg":"video.jpg"......};

if (file.extension in format_dict) {

      return format_dict[file.extension];
}

return "file.jpg";
levi
  • 22,001
  • 7
  • 73
  • 74
-2

You can just add commas in your cases as follows:

getImage = function (file) {
switch (file.extension) {
    case "txt","doc":
        return "document.jpg";        
    case "jpg","gif","png":
        return "image.jpg";                
    case "mpg":
        return "video.jpg";

   // And so on for 50+ file extensions...

    default:
        return "file.jpg";
}

}

Kunal Valecha
  • 207
  • 1
  • 8