I need help to refactor this code, I think I can avoid the first two functions that filter the array. Also in the third part where I use the result of these two filters:
let currentWarnings = f.emplacementsForecastForAlerts.nextHoursVariableValues
.filter((x) =>
RiskService.getInstance()
.listDojoVaribleAlertsToShowInMap.map((x) => x.id)
.includes(x.variable)
)
.filter((x) => x.isCurrentHourWarning);
let icon = SvgStrings.SITE_ICON_BASE.replace("$width", iconWidth.toString())
.replaceAll(
"$currentAlert",
currentAlerts.length > 0 || currentWarnings.length > 0
? SvgStrings.CIRCLES_CURRENT_ALERTS.replaceAll(
"$colorCurrentBorderRisk",
currentAlerts.length > 0
? this.calculateColorBoolean(true)
: this.calculateColorBoolean(false)
)
: ""
)
.replace(
"$alerts",
f.emplacementsForecastForAlerts.hasAlerts > 0 ||
f.emplacementsForecastForAlerts.hasWarnings > 0
? SvgStrings.CIRCLE_NUM_ALERTS.replace(
"$colorBorderRisk",
this.calculateColor(f.emplacementsForecastForAlerts.hasAlerts, true)
).replaceAll(
"$colorBodyRisk",
this.calculateColor(f.emplacementsForecastForAlerts.hasAlerts, false)
)
: ""
);
private calculateColor(valueAlert: number, isBorder: boolean = false) {
if (valueAlert > 0) {
return isBorder ? COLOR_RED_BORDER : COLOR_RED;
} else {
return isBorder ? COLOR_YELLOW_BORDER : COLOR_YELLOW;
}
}
private calculateColorBoolean(hastAlert: boolean) {
return hastAlert ? COLOR_RED_BORDER : COLOR_YELLOW_BORDER;
}
I'd like to avoid those initial filters and find a more recursive way to get the proper color for the marker.
f
, and where does it come from? (And why such a short name?) There are some other things that don't seem to be defined (unless your language/library provides them?) \$\endgroup\$