From 26730cb2e2541579b47c5f389a3f58a9cb0f717f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 21 Jan 2023 17:50:22 -1000 Subject: [PATCH 1/2] Object.keys to for --- src/panels/lovelace/cards/hui-map-card.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/panels/lovelace/cards/hui-map-card.ts b/src/panels/lovelace/cards/hui-map-card.ts index 3392f5d097..08ba908faa 100644 --- a/src/panels/lovelace/cards/hui-map-card.ts +++ b/src/panels/lovelace/cards/hui-map-card.ts @@ -330,7 +330,7 @@ class HuiMapCard extends LitElement implements LovelaceCard { const paths: HaMapPaths[] = []; - Object.keys(history).forEach((entityId) => { + for (const entityId of Object.keys(history)) { const entityStates = history[entityId]; if (entityStates?.length > 1) { // filter location data from states and remove all invalid locations @@ -364,7 +364,7 @@ class HuiMapCard extends LitElement implements LovelaceCard { gradualOpacity: 0.8, }); } - }); + } return paths; } ); From cfdbad504c9dfb3335ebfb77b94d9734a6993d95 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 21 Jan 2023 17:53:38 -1000 Subject: [PATCH 2/2] refactor --- src/panels/lovelace/cards/hui-map-card.ts | 60 +++++++++++------------ 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/src/panels/lovelace/cards/hui-map-card.ts b/src/panels/lovelace/cards/hui-map-card.ts index 08ba908faa..b608e61b3a 100644 --- a/src/panels/lovelace/cards/hui-map-card.ts +++ b/src/panels/lovelace/cards/hui-map-card.ts @@ -332,38 +332,36 @@ class HuiMapCard extends LitElement implements LovelaceCard { for (const entityId of Object.keys(history)) { const entityStates = history[entityId]; - if (entityStates?.length > 1) { - // filter location data from states and remove all invalid locations - const points = entityStates.reduce( - (accumulator: HaMapPathPoint[], entityState) => { - const latitude = entityState.a.latitude; - const longitude = entityState.a.longitude; - if (latitude && longitude) { - const p = {} as HaMapPathPoint; - p.point = [latitude, longitude] as LatLngTuple; - const t = new Date(entityState.lu * 1000); - if (config.hours_to_show! || DEFAULT_HOURS_TO_SHOW > 144) { - // if showing > 6 days in the history trail, show the full - // date and time - p.tooltip = formatDateTime(t, this.hass.locale); - } else if (isToday(t)) { - p.tooltip = formatTime(t, this.hass.locale); - } else { - p.tooltip = formatTimeWeekday(t, this.hass.locale); - } - accumulator.push(p); - } - return accumulator; - }, - [] - ) as HaMapPathPoint[]; - - paths.push({ - points, - color: this._getColor(entityId), - gradualOpacity: 0.8, - }); + if (!entityStates?.length) { + continue; } + // filter location data from states and remove all invalid locations + const points: HaMapPathPoint[] = []; + for (const entityState of entityStates) { + const latitude = entityState.a.latitude; + const longitude = entityState.a.longitude; + if (!latitude || !longitude) { + continue; + } + const p = {} as HaMapPathPoint; + p.point = [latitude, longitude] as LatLngTuple; + const t = new Date(entityState.lu * 1000); + if (config.hours_to_show! || DEFAULT_HOURS_TO_SHOW > 144) { + // if showing > 6 days in the history trail, show the full + // date and time + p.tooltip = formatDateTime(t, this.hass.locale); + } else if (isToday(t)) { + p.tooltip = formatTime(t, this.hass.locale); + } else { + p.tooltip = formatTimeWeekday(t, this.hass.locale); + } + points.push(p); + } + paths.push({ + points, + color: this._getColor(entityId), + gradualOpacity: 0.8, + }); } return paths; }