在打字稿中编写此开关/大小写的更好方法?

时间:2019-06-09 13:25:04

标签: javascript angular typescript design-patterns enums

我正在Angular中开发一个非常简单的气象应用程序,我想问一下您是否认为有更好的方法可以根据天气编码的“类型”选择特定的图像。

enum WeatherCodition {
    Thunderstorm = 0,
    Drizzle,
    Rain,
    Snow,
    Clear,
    Clouds
}

export class Weather {


    getIcon(condition: WeatherCodition): string {

        var iconPath = "";
        switch(condition){
            case WeatherCodition.Thunderstorm:
                iconPath =  "thunderstorm.png";
                break;
            case WeatherCodition.Clouds:
                iconPath =  "clouds.png";
                 break;
            case WeatherCodition.Drizzle:
                iconPath =  "drizzle.png";
                break;
            case WeatherCodition.Rain:
                iconPath =  "rain.png";
                 break;
            case WeatherCodition.Snow:
                iconPath =  "snow.png";
                break;
            default:
                iconPath = "clear.png"
        }

        return iconPath;
    }

}

4 个答案:

答案 0 :(得分:1)

请考虑使用interface KeyValue<K, V>作为数组。我的解决方案:

export enum WeatherCodition {
    Thunderstorm = 0,
    Drizzle,
    Rain,
    Snow,
    Clear,
    Clouds
}

import { KeyValue } from '@angular/common';

export class Weather {

    public keyValueArray: KeyValue<WeatherCodition, string>[] = 
    [
        { key: WeatherCodition.Thunderstorm, value: "thunderstorm.png" },
        { key: WeatherCodition.Drizzle , value: "drizzle.png"},
        { key: WeatherCodition.Rain, value: "rain.png" },
        { key: WeatherCodition.Snow, value: "snow.png" },
        { key: WeatherCodition.Clear, value: "clear.png" },
        { key: WeatherCodition.Clouds, value: "clouds.png" },
    ];

    getIcon(condition: WeatherCodition): string {
        //check if 'condition' exists in array as key
        return this.keyValueArray[condition] ? 
            this.keyValueArray[condition].value : 
            "clear.png"; 
    }
}

祝你有美好的一天!

答案 1 :(得分:0)

您可以基于键创建对象并访问属性

let WeatherCodition = {
  thunderstorm:"thunderstorm.png",
  clouds:"clouds.png",
  drizzle:"drizzle.png",
  rain:"rain.png",
  snow:"snow.png",
  default:"clear.png"
}

function getIcon(condition) {
  condition = condition || ""
  condition = Object.keys(WeatherCodition).find(c=> c.toLowerCase() === condition.toLowerCase()) || 'default'
  return WeatherCodition[condition]
}

console.log(getIcon(''))
console.log(getIcon('Clouds'))
console.log(getIcon())
console.log(getIcon('SnoW'))

答案 2 :(得分:0)

您的方法非常好。无论如何,您都可以在switch语句中对URL进行编码时,也可以创建一个用于恒定时间查找的哈希图。

   interface WeatherIcons {
     Thunderstorm: string;
     Clouds: string;
   }
   const icons: WeatherIcons = {
      Thunderstorm: "Thunderstorm.jpg",
      Clouds: "Clouds.jpg"
   }
   function getIcon(condition: WeatherCondition) {
      return icons[condition] || "default.jpg";
   }

答案 3 :(得分:0)

为什么不定义数组?

iconPaths:string[]=["thunderstorm.png","clouds.png","drizzle.png","rain.png","snow.png",
                   "clear.png"]
iconPath=this.iconPaths[condition];
//or 
iconPath=(condition && condition<6)?this.iconPaths[condition]:"";