Question

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)

Était-ce utile?

La solution

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.

Autres conseils

var types = {"txt":"document.jpg", "doc":"document.jpg"}
return types[file.extension]
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';
 }

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";
    }
}

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";
}

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.

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";
    }

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";

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";
    }
}

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";
}

}

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top