1

私はこの拡張子検出機能を持っていますが、if/then ステートメントの多数の行に大きく依存しています。同じ結果を得るために使用できる別の方法はありますか? (すべての変数はより高いレベルで定義されます)。処理するコードを減らし、パフォーマンスを向上させたい。

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: The 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 = '--'; }
4

3 に答える 3

11

オブジェクトをマップとして使用します。

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/

利点

  • データを実際のデータとしてリストし、2 行の割り当てのみを含めます。
  • JSON ファイルからデータをロードして、コードから完全に削除し、コードを変更せずに構成可能にすることができます。
  • 明確な構造からデータへ
  • 従うのが簡単な明確なロジック (後で誰かが if ステートメントの 1 つにランダムなコード行を追加して、デバッグを困難にするリスクはありません)
于 2013-08-09T04:04:53.387 に答える