javascript - Is there a better solution than using an if/then statement for this? -


i have extension detection function, relies heavily on numerous lines of if/then statements. there different method use same results? (all of variables defined @ higher level). want deal less code, , increase performance speeds.

if (extt == 'app') { doctype = 'app'; docdesc = 'application'; }         else if (extt == 'exe') { doctype = 'exe'; docdesc = 'executable file'; }         else if (extt == 'vwx') { doctype = 'vwx'; docdesc = 'vectorworks file'; }         else if (extt == 'torrent') { doctype = 'torrent'; docdesc = 'bittorrent document'; }         else if (extt == 'ipsw') { doctype = 'ipsw'; docdesc = 'apple device software update file'; }         else if (extt == 'mpkg') { doctype = 'mpkg'; docdesc = 'installer package'; }         else if (extt == 'prefpane') { doctype = 'prefpane'; docdesc = 'mac os x preference pane'; }         else if (extt == 'ogg') { doctype = 'ogg_audio'; docdesc = 'ogg vorbis'; }         else if (extt == 'ogv') { doctype = 'ogv'; docdesc = 'ogg vorbis'; }         else if (extt == 'flac') { doctype = 'flac_audio'; docdesc = 'flac audio file'; }         else if (extt == 'ttf') { doctype = 'ttf'; docdesc = 'truetype font'; }         else if (extt == 'otf') { doctype = 'otf'; docdesc = 'opentype font'; }         else if (extt == 'air') { doctype = 'air'; docdesc = 'rich internet application'; }         else if (extt == 'csproj') { doctype = 'csproj'; docdesc = '.net source code'; }         else if (extt == 'download') { doctype = 'download'; docdesc = 'file download'; }         else if (extt == 'log') { doctype = 'log'; docdesc = 'log file'; }         else if (extt == 'conf') { doctype = 'conf'; docdesc = 'irc related configuration file'; }         else if (extt == 'command') { doctype = 'command'; docdesc = 'terminal shell script'; }         else if (extt == 'plist') { doctype = 'plist'; docdesc = 'property list xml file'; }         else if (extt == 'mobileprovision') { doctype = 'developer provisioning profile'; docdesc = ''; }         else if (extt == 'ds_store') { doctype = 'ds_store'; docdesc = 'desktop services store'; }         else if (extt == 'asp') { doctype = 'asp'; docdesc = 'active server pages'; }         else if (extt == 'safariextz') { doctype = 'safariextz'; docdesc = 'safari extension'; }         else if (extt == 'qlab2license') { doctype = 'qlab2license'; docdesc = 'qlab license file'; }         else if (extt == 'qlablicense') { doctype = 'qlablicense'; docdesc = 'qlab license file'; }         else if (extt == 'qlab1license') { doctype = 'qlab1license'; docdesc = 'qlab license file'; }         else if (extt == 'qlab3license') { doctype = 'qlab3license'; docdesc = 'qlab license file'; }         else if (extt == 'saver') { doctype = 'saver'; docdesc = 'mac os x preference screen saver'; }         else if (extt == 'pkg') { doctype = 'pkg'; docdesc = 'installer package'; }         else if (extt == 'w3x') { doctype = 'w3x'; docdesc = 'warcraft iii: frozen throne'; }         else if (extt == 'fcp') { doctype = 'fcp'; docdesc = 'final cut project file'; }         else if (extt == 'pages') { doctype = 'pages'; docdesc = 'pages publication'; }         else if (extt == 'numbers') { doctype = 'numbers'; docdesc = 'numbers document'; }         else if (extt == 'key') { doctype = 'key'; docdesc = 'keynote document'; }         else if (extt == 'wwf') { doctype = 'wwf'; docdesc = 'wwf non-printable document'; }         else if (extt == 'swd') { doctype = 'swd'; docdesc = 'shockwave document'; }         else if (extt == 'rtfd') { doctype = 'rtfd'; docdesc = 'next rtf-packet'; }         else if (extt == 'wtpl') { doctype = 'wtpl'; docdesc = 'wiretap studio library'; }         else if (extt == 'abbu') { doctype = 'abbu'; docdesc = 'address book archive file'; }         else if (extt == 'thllibrary') { doctype = 'thllibrary'; docdesc = 'the hit list library'; }         else if (extt == 'logic') { doctype = 'logic'; docdesc = 'logic project file'; }         else if (extt == 'pat') { doctype = 'pat'; docdesc = 'adobe photoshop patterns file'; }         else if (extt == 'dwg') { doctype = 'dwg'; docdesc = 'autocad image'; }         else if (extt == 'ctb') { doctype = 'ctb'; docdesc = 'autocad color-based plot style file'; }         else if (extt == 'stb') { doctype = 'stb'; docdesc = 'autocad plot style table file'; }         else if (extt == 'lck') { doctype = 'lck'; docdesc = 'lock file'; }         else if (extt == 'aep') { doctype = 'aep'; docdesc = 'after effects project'; }         else if (extt == 'prtl') { doctype = 'prtl'; docdesc = 'adobe permiere title file'; }         else if (extt == 'prproj') { doctype = 'prproj'; docdesc = 'adobe premiere project'; }         else if (extt == 'band') { doctype = 'band'; docdesc = 'garageband project file'; }         else if (extt == 'h') { doctype = 'h'; docdesc = 'header file'; }         else if (extt == 'c') { doctype = 'c'; docdesc = 'c/c++ program file'; }         else if (extt == 'r') { doctype = 'r'; docdesc = 'rez source code file'; }         else if (extt == 'xcodeproj') { doctype = 'xcodeproj'; docdesc = 'xcode project'; }         else if (extt == 'xcconfig') { doctype = 'xcconfig'; docdesc = 'xcode configuration settings'; }         else if (extt == 'rcproject') { doctype = 'rcproject'; docdesc = 'imovie project'; }         else if (extt == 'cache') { doctype = 'cache'; docdesc = 'cache file'; }         else if (extt == 'exs') { doctype = 'exs'; docdesc = 'exs24 instrument file'; }         else if (extt == 'sdir') { doctype = 'sdir'; docdesc = 'space designer impulse response'; }         else if (extt == 'ovw') { doctype = 'ovw'; docdesc = 'cool edit pro overview file'; }         else if (extt == 'lwp') { doctype = 'lwp'; docdesc = 'lightwright project'; }         else if (extt == 'bin') { doctype = 'bin'; docdesc = 'binary file'; }         else if (extt == 'ini') { doctype = 'ini'; docdesc = 'initialization/configuration file'; }         else if (extt == 'ex_') { doctype = 'ex_'; docdesc = 'compressed exe file'; }         else if (extt == 'bat') { doctype = 'bat'; docdesc = 'microsoft batch file'; }         else if (extt == 'edml') { doctype = 'edml'; docdesc = 'adobe extension data markup language (edml) document'; }         else if (extt == 'mak') { doctype = 'mak'; docdesc = 'makefile'; }         else if (extt == 'cfm') { doctype = 'cfm'; docdesc = 'adobe coldfusion document'; }         else if (extt == 'indd') { doctype = 'indd'; docdesc = 'adobe indesign document'; }         else if (extt == 'indt') { doctype = 'indt'; docdesc = 'adobe indesign template'; }         else if (extt == 'asnd') { doctype = 'asnd'; docdesc = 'adobe sound document'; }         else if (extt == 'cpp') { doctype = 'cpp'; docdesc = 'c++ source'; }         else if (extt == 'aac') { doctype = 'aac'; docdesc = 'mpeg-2 advanced audio coding file'; }         else if (extt == 'm2v') { doctype = 'm2v'; docdesc = 'mpeg-2 blu-ray file'; }         else if (extt == 'olproj') { doctype = 'olproj'; docdesc = 'adobe onlocation project'; }         else if (extt == 'ipa') { doctype = 'ipa'; docdesc = 'ios application'; }         else if (extt == 'apk') { doctype = 'apk'; docdesc = 'android application'; }         else if (extt == 'scpt') { doctype = 'scpt'; docdesc = 'script file'; }         else if (extt == 'applescript') { doctype = 'applescript'; docdesc = 'applescript file'; }         else if (extt == 'emlxpart') { doctype = 'emlxpart'; docdesc = 'mail message attachment'; }         else if (extt == 'emlx') { doctype = 'emlx'; docdesc = 'mail message'; }         else if (extt == 'webbookmark') { doctype = 'webbookmark'; docdesc = 'safari bookmark'; }         else if (extt == 'jsx') { doctype = 'jsx'; docdesc = 'adobe javascript file'; }         else if (extt == 'icns') { doctype = 'icns'; docdesc = 'apple icon image'; }         else if (extt == 'caf') { doctype = 'caf_audio'; docdesc = 'apple loop'; }         else if (extt == 'cues') { doctype = 'cues'; docdesc = 'qlab workspace'; }         else { doctype = '--'; } 

use object map.

var map = {     "exe": {type:"exe",desc:"executable file"},      "vwx": {type:"vwx",desc:"vectorworks file"} };  var extt = "exe";  var doctype = map[extt].type; var docdesc = map[extt].desc; 

http://jsfiddle.net/zfbhf/1/

advantages

  • lists data actual data, , contains 2 lines of assignment
  • data loaded json file, removing code entirely , making configurable without changing code
  • clear structure data
  • clear logic thats easy follow (no risk of later adding random line of code in 1 of if statements making hard debug)

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -