Skip to content

Commit

Permalink
chore: migrate some functions
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed Jun 4, 2022
1 parent 1b3bf05 commit 47c6c99
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/cards/chips-card/chips/alarm-control-panel-chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { customElement, property, state } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
import { styleMap } from "lit/directives/style-map.js";
import {
actionHandler,
ActionHandlerEvent,
computeRTL,
computeStateDisplay,
handleAction,
hasAction,
HomeAssistant,
} from "../../../ha";
import { actionHandler } from "../../../ha/panels/lovelace/common/directives/action-handler-directive";
import { computeRgbColor } from "../../../utils/colors";
import { animation } from "../../../utils/entity-styles";
import { stateIcon } from "../../../utils/icons/state-icon";
Expand Down
2 changes: 1 addition & 1 deletion src/cards/chips-card/chips/entity-chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { customElement, property, state } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
import { styleMap } from "lit/directives/style-map.js";
import {
actionHandler,
ActionHandlerEvent,
computeRTL,
computeStateDisplay,
Expand All @@ -12,7 +13,6 @@ import {
HomeAssistant,
isActive,
} from "../../../ha";
import { actionHandler } from "../../../ha/panels/lovelace/common/directives/action-handler-directive";
import { computeRgbColor } from "../../../utils/colors";
import { stateIcon } from "../../../utils/icons/state-icon";
import { getInfo } from "../../../utils/info";
Expand Down
2 changes: 1 addition & 1 deletion src/cards/cover-card/cover-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { customElement, state } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
import { styleMap } from "lit/directives/style-map.js";
import {
actionHandler,
ActionHandlerEvent,
computeRTL,
computeStateDisplay,
Expand All @@ -15,7 +16,6 @@ import {
LovelaceCard,
LovelaceCardEditor,
} from "../../ha";
import { actionHandler } from "../../ha/panels/lovelace/common/directives/action-handler-directive";
import "../../shared/badge-icon";
import "../../shared/button";
import "../../shared/card";
Expand Down
2 changes: 1 addition & 1 deletion src/cards/media-player-card/media-player-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
getEntityPicture,
handleAction,
hasAction,
HomeAssistant,
isActive,
LovelaceCard,
LovelaceCardEditor,
MediaPlayerEntity,
} from "../../ha";
import { HomeAssistant } from "../../ha/types";
import "../../shared/badge-icon";
import "../../shared/card";
import "../../shared/shape-avatar";
Expand Down
2 changes: 1 addition & 1 deletion src/cards/update-card/update-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { customElement, state } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
import { styleMap } from "lit/directives/style-map.js";
import {
actionHandler,
ActionHandlerEvent,
computeRTL,
computeStateDisplay,
Expand All @@ -19,7 +20,6 @@ import {
updateIsInstalling,
UPDATE_SUPPORT_INSTALL,
} from "../../ha";
import { actionHandler } from "../../ha/panels/lovelace/common/directives/action-handler-directive";
import "../../shared/badge-icon";
import "../../shared/card";
import "../../shared/shape-icon";
Expand Down
87 changes: 87 additions & 0 deletions src/ha/common/datetime/format_date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import memoizeOne from "memoize-one";
import { FrontendLocaleData } from "../../data/translation";

// Tuesday, August 10
export const formatDateWeekday = (dateObj: Date, locale: FrontendLocaleData) =>
formatDateWeekdayMem(locale).format(dateObj);

const formatDateWeekdayMem = memoizeOne(
(locale: FrontendLocaleData) =>
new Intl.DateTimeFormat(locale.language, {
weekday: "long",
month: "long",
day: "numeric",
})
);

// August 10, 2021
export const formatDate = (dateObj: Date, locale: FrontendLocaleData) =>
formatDateMem(locale).format(dateObj);

const formatDateMem = memoizeOne(
(locale: FrontendLocaleData) =>
new Intl.DateTimeFormat(locale.language, {
year: "numeric",
month: "long",
day: "numeric",
})
);

// 10/08/2021
export const formatDateNumeric = (dateObj: Date, locale: FrontendLocaleData) =>
formatDateNumericMem(locale).format(dateObj);

const formatDateNumericMem = memoizeOne(
(locale: FrontendLocaleData) =>
new Intl.DateTimeFormat(locale.language, {
year: "numeric",
month: "numeric",
day: "numeric",
})
);

// Aug 10
export const formatDateShort = (dateObj: Date, locale: FrontendLocaleData) =>
formatDateShortMem(locale).format(dateObj);

const formatDateShortMem = memoizeOne(
(locale: FrontendLocaleData) =>
new Intl.DateTimeFormat(locale.language, {
day: "numeric",
month: "short",
})
);

// August 2021
export const formatDateMonthYear = (dateObj: Date, locale: FrontendLocaleData) =>
formatDateMonthYearMem(locale).format(dateObj);

const formatDateMonthYearMem = memoizeOne(
(locale: FrontendLocaleData) =>
new Intl.DateTimeFormat(locale.language, {
month: "long",
year: "numeric",
})
);

// August
export const formatDateMonth = (dateObj: Date, locale: FrontendLocaleData) =>
formatDateMonthMem(locale).format(dateObj);

const formatDateMonthMem = memoizeOne(
(locale: FrontendLocaleData) =>
new Intl.DateTimeFormat(locale.language, {
month: "long",
})
);

// 2021
export const formatDateYear = (dateObj: Date, locale: FrontendLocaleData) =>
formatDateYearMem(locale).format(dateObj);

const formatDateYearMem = memoizeOne(
(locale: FrontendLocaleData) =>
new Intl.DateTimeFormat(locale.language, {
year: "numeric",
})
);
61 changes: 61 additions & 0 deletions src/ha/common/datetime/format_date_time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import memoizeOne from "memoize-one";
import { FrontendLocaleData } from "../../data/translation";
import { useAmPm } from "./use_am_pm";

// August 9, 2021, 8:23 AM
export const formatDateTime = (dateObj: Date, locale: FrontendLocaleData) =>
formatDateTimeMem(locale).format(dateObj);

const formatDateTimeMem = memoizeOne(
(locale: FrontendLocaleData) =>
new Intl.DateTimeFormat(
locale.language === "en" && !useAmPm(locale) ? "en-u-hc-h23" : locale.language,
{
year: "numeric",
month: "long",
day: "numeric",
hour: useAmPm(locale) ? "numeric" : "2-digit",
minute: "2-digit",
hour12: useAmPm(locale),
}
)
);

// August 9, 2021, 8:23:15 AM
export const formatDateTimeWithSeconds = (dateObj: Date, locale: FrontendLocaleData) =>
formatDateTimeWithSecondsMem(locale).format(dateObj);

const formatDateTimeWithSecondsMem = memoizeOne(
(locale: FrontendLocaleData) =>
new Intl.DateTimeFormat(
locale.language === "en" && !useAmPm(locale) ? "en-u-hc-h23" : locale.language,
{
year: "numeric",
month: "long",
day: "numeric",
hour: useAmPm(locale) ? "numeric" : "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: useAmPm(locale),
}
)
);

// 9/8/2021, 8:23 AM
export const formatDateTimeNumeric = (dateObj: Date, locale: FrontendLocaleData) =>
formatDateTimeNumericMem(locale).format(dateObj);

const formatDateTimeNumericMem = memoizeOne(
(locale: FrontendLocaleData) =>
new Intl.DateTimeFormat(
locale.language === "en" && !useAmPm(locale) ? "en-u-hc-h23" : locale.language,
{
year: "numeric",
month: "numeric",
day: "numeric",
hour: "numeric",
minute: "2-digit",
hour12: useAmPm(locale),
}
)
);
53 changes: 53 additions & 0 deletions src/ha/common/datetime/format_time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import memoizeOne from "memoize-one";
import { FrontendLocaleData } from "../../data/translation";
import { useAmPm } from "./use_am_pm";

// 9:15 PM || 21:15
export const formatTime = (dateObj: Date, locale: FrontendLocaleData) =>
formatTimeMem(locale).format(dateObj);

const formatTimeMem = memoizeOne(
(locale: FrontendLocaleData) =>
new Intl.DateTimeFormat(
locale.language === "en" && !useAmPm(locale) ? "en-u-hc-h23" : locale.language,
{
hour: "numeric",
minute: "2-digit",
hour12: useAmPm(locale),
}
)
);

// 9:15:24 PM || 21:15:24
export const formatTimeWithSeconds = (dateObj: Date, locale: FrontendLocaleData) =>
formatTimeWithSecondsMem(locale).format(dateObj);

const formatTimeWithSecondsMem = memoizeOne(
(locale: FrontendLocaleData) =>
new Intl.DateTimeFormat(
locale.language === "en" && !useAmPm(locale) ? "en-u-hc-h23" : locale.language,
{
hour: useAmPm(locale) ? "numeric" : "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: useAmPm(locale),
}
)
);

// Tuesday 7:00 PM || Tuesday 19:00
export const formatTimeWeekday = (dateObj: Date, locale: FrontendLocaleData) =>
formatTimeWeekdayMem(locale).format(dateObj);

const formatTimeWeekdayMem = memoizeOne(
(locale: FrontendLocaleData) =>
new Intl.DateTimeFormat(
locale.language === "en" && !useAmPm(locale) ? "en-u-hc-h23" : locale.language,
{
weekday: "long",
hour: useAmPm(locale) ? "numeric" : "2-digit",
minute: "2-digit",
hour12: useAmPm(locale),
}
)
);
13 changes: 13 additions & 0 deletions src/ha/common/datetime/use_am_pm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import memoizeOne from "memoize-one";
import { FrontendLocaleData, TimeFormat } from "../../data/translation";

export const useAmPm = memoizeOne((locale: FrontendLocaleData): boolean => {
if (locale.time_format === TimeFormat.language || locale.time_format === TimeFormat.system) {
const testLanguage =
locale.time_format === TimeFormat.language ? locale.language : undefined;
const test = new Date().toLocaleString(testLanguage);
return test.includes("AM") || test.includes("PM");
}

return locale.time_format === TimeFormat.am_pm;
});
15 changes: 6 additions & 9 deletions src/ha/common/entity/compute-state-display.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import {
formatDate,
formatDateTime,
formatNumber,
formatTime,
FrontendLocaleData,
isNumericState,
LocalizeFunc,
} from "custom-card-helpers";
import { HassEntity } from "home-assistant-js-websocket";
import { UNAVAILABLE, UNKNOWN } from "../../data/entity";
import { FrontendLocaleData } from "../../data/translation";
import { updateIsInstalling, UpdateEntity, UPDATE_SUPPORT_PROGRESS } from "../../data/update";
import { formatDate } from "../datetime/format_date";
import { formatDateTime } from "../datetime/format_date_time";
import { formatTime } from "../datetime/format_time";
import { formatNumber, isNumericState } from "../number/format_number";
import { LocalizeFunc } from "../translations/localize";
import { computeStateDomain } from "./compute-state-domain";
import { supportsFeature } from "./supports-feature";

Expand Down
4 changes: 4 additions & 0 deletions src/ha/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export * from "./common/const";
export * from "./common/datetime/format_date";
export * from "./common/datetime/format_date_time";
export * from "./common/datetime/format_time";
export * from "./common/datetime/use_am_pm";
export * from "./common/dom/fire_event";
export * from "./common/dom/get_main_window";
export * from "./common/entity/compute-domain";
Expand Down
2 changes: 1 addition & 1 deletion src/utils/icons/domain-icon.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HassEntity } from "home-assistant-js-websocket";
import { UpdateEntity, updateIsInstalling } from "../../ha/data/update";
import { UpdateEntity, updateIsInstalling } from "../../ha";
import { alarmPanelIcon } from "./alarm-panel-icon";
import { binarySensorIcon } from "./binary-sensor-icon";
import { coverIcon } from "./cover-icon";
Expand Down
2 changes: 1 addition & 1 deletion src/utils/icons/sensor-icon.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UNIT_C, UNIT_F } from "custom-card-helpers";
import { HassEntity } from "home-assistant-js-websocket";
import { UNIT_C, UNIT_F } from "../../ha";

const FIXED_DEVICE_CLASS_ICONS = {
apparent_power: "mdi:flash",
Expand Down
2 changes: 1 addition & 1 deletion src/utils/icons/state-icon.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { computeDomain } from "custom-card-helpers";
import { HassEntity } from "home-assistant-js-websocket";
import { computeDomain } from "../../ha";
import { domainIcon } from "./domain-icon";

export function stateIcon(entity: HassEntity): string {
Expand Down

0 comments on commit 47c6c99

Please sign in to comment.