Skip to content

Commit

Permalink
Only use duration poly-fill when necessary (#23030)
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya authored Nov 27, 2024
1 parent 125805d commit 3411967
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
1 change: 0 additions & 1 deletion build-scripts/gulp/locale-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const outDir = join(paths.build_dir, "locale-data");

const INTL_POLYFILLS = {
"intl-datetimeformat": "DateTimeFormat",
"intl-durationFormat": "DurationFormat",
"intl-displaynames": "DisplayNames",
"intl-listformat": "ListFormat",
"intl-numberformat": "NumberFormat",
Expand Down
15 changes: 7 additions & 8 deletions src/common/datetime/format_duration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DurationFormat } from "@formatjs/intl-durationformat";
import type { DurationInput } from "@formatjs/intl-durationformat/src/types";
import memoizeOne from "memoize-one";
import type { HaDurationData } from "../../components/ha-duration-input";
Expand Down Expand Up @@ -49,7 +48,7 @@ export const formatNumericDuration = (

const formatDurationLongMem = memoizeOne(
(locale: FrontendLocaleData) =>
new DurationFormat(locale.language, {
new Intl.DurationFormat(locale.language, {
style: "long",
})
);
Expand All @@ -61,7 +60,7 @@ export const formatDurationLong = (

const formatDigitalDurationMem = memoizeOne(
(locale: FrontendLocaleData) =>
new DurationFormat(locale.language, {
new Intl.DurationFormat(locale.language, {
style: "digital",
hoursDisplay: "auto",
})
Expand All @@ -78,39 +77,39 @@ type DurationUnit = (typeof DURATION_UNITS)[number];

const formatDurationDayMem = memoizeOne(
(locale: FrontendLocaleData) =>
new DurationFormat(locale.language, {
new Intl.DurationFormat(locale.language, {
style: "narrow",
daysDisplay: "always",
})
);

const formatDurationHourMem = memoizeOne(
(locale: FrontendLocaleData) =>
new DurationFormat(locale.language, {
new Intl.DurationFormat(locale.language, {
style: "narrow",
hoursDisplay: "always",
})
);

const formatDurationMinuteMem = memoizeOne(
(locale: FrontendLocaleData) =>
new DurationFormat(locale.language, {
new Intl.DurationFormat(locale.language, {
style: "narrow",
minutesDisplay: "always",
})
);

const formatDurationSecondMem = memoizeOne(
(locale: FrontendLocaleData) =>
new DurationFormat(locale.language, {
new Intl.DurationFormat(locale.language, {
style: "narrow",
secondsDisplay: "always",
})
);

const formatDurationMillisecondMem = memoizeOne(
(locale: FrontendLocaleData) =>
new DurationFormat(locale.language, {
new Intl.DurationFormat(locale.language, {
style: "narrow",
millisecondsDisplay: "always",
})
Expand Down
11 changes: 9 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DurationFormatConstructor } from "@formatjs/intl-durationformat/src/types";
import type {
Auth,
Connection,
Expand All @@ -22,15 +23,15 @@ import type { Themes } from "./data/ws-themes";
import type { ExternalMessaging } from "./external_app/external_messaging";

declare global {
/* eslint-disable no-var, no-redeclare */
/* eslint-disable no-var */
var __DEV__: boolean;
var __DEMO__: boolean;
var __BUILD__: "modern" | "legacy";
var __VERSION__: string;
var __STATIC_PATH__: string;
var __BACKWARDS_COMPAT__: boolean;
var __SUPERVISOR__: boolean;
/* eslint-enable no-var, no-redeclare */
/* eslint-enable no-var */

interface Window {
// Custom panel entry point url
Expand Down Expand Up @@ -64,6 +65,12 @@ declare global {
interface ImportMeta {
url: string;
}

// Intl.DurationFormat is not yet part of the TypeScript standard
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Intl {
const DurationFormat: DurationFormatConstructor;
}
}

export interface ValueChangedEvent<T> extends CustomEvent {
Expand Down
2 changes: 1 addition & 1 deletion test/common/datetime/format_duration.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "@formatjs/intl-durationformat/polyfill-force";
import { assert, describe, it } from "vitest";

import { formatDuration } from "../../../src/common/datetime/format_duration";
import type { FrontendLocaleData } from "../../../src/data/translation";
import {
Expand Down

0 comments on commit 3411967

Please sign in to comment.