Skip to content

Commit 3411967

Browse files
authored
Only use duration poly-fill when necessary (#23030)
1 parent 125805d commit 3411967

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

build-scripts/gulp/locale-data.js

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const outDir = join(paths.build_dir, "locale-data");
99

1010
const INTL_POLYFILLS = {
1111
"intl-datetimeformat": "DateTimeFormat",
12-
"intl-durationFormat": "DurationFormat",
1312
"intl-displaynames": "DisplayNames",
1413
"intl-listformat": "ListFormat",
1514
"intl-numberformat": "NumberFormat",

src/common/datetime/format_duration.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { DurationFormat } from "@formatjs/intl-durationformat";
21
import type { DurationInput } from "@formatjs/intl-durationformat/src/types";
32
import memoizeOne from "memoize-one";
43
import type { HaDurationData } from "../../components/ha-duration-input";
@@ -49,7 +48,7 @@ export const formatNumericDuration = (
4948

5049
const formatDurationLongMem = memoizeOne(
5150
(locale: FrontendLocaleData) =>
52-
new DurationFormat(locale.language, {
51+
new Intl.DurationFormat(locale.language, {
5352
style: "long",
5453
})
5554
);
@@ -61,7 +60,7 @@ export const formatDurationLong = (
6160

6261
const formatDigitalDurationMem = memoizeOne(
6362
(locale: FrontendLocaleData) =>
64-
new DurationFormat(locale.language, {
63+
new Intl.DurationFormat(locale.language, {
6564
style: "digital",
6665
hoursDisplay: "auto",
6766
})
@@ -78,39 +77,39 @@ type DurationUnit = (typeof DURATION_UNITS)[number];
7877

7978
const formatDurationDayMem = memoizeOne(
8079
(locale: FrontendLocaleData) =>
81-
new DurationFormat(locale.language, {
80+
new Intl.DurationFormat(locale.language, {
8281
style: "narrow",
8382
daysDisplay: "always",
8483
})
8584
);
8685

8786
const formatDurationHourMem = memoizeOne(
8887
(locale: FrontendLocaleData) =>
89-
new DurationFormat(locale.language, {
88+
new Intl.DurationFormat(locale.language, {
9089
style: "narrow",
9190
hoursDisplay: "always",
9291
})
9392
);
9493

9594
const formatDurationMinuteMem = memoizeOne(
9695
(locale: FrontendLocaleData) =>
97-
new DurationFormat(locale.language, {
96+
new Intl.DurationFormat(locale.language, {
9897
style: "narrow",
9998
minutesDisplay: "always",
10099
})
101100
);
102101

103102
const formatDurationSecondMem = memoizeOne(
104103
(locale: FrontendLocaleData) =>
105-
new DurationFormat(locale.language, {
104+
new Intl.DurationFormat(locale.language, {
106105
style: "narrow",
107106
secondsDisplay: "always",
108107
})
109108
);
110109

111110
const formatDurationMillisecondMem = memoizeOne(
112111
(locale: FrontendLocaleData) =>
113-
new DurationFormat(locale.language, {
112+
new Intl.DurationFormat(locale.language, {
114113
style: "narrow",
115114
millisecondsDisplay: "always",
116115
})

src/types.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { DurationFormatConstructor } from "@formatjs/intl-durationformat/src/types";
12
import type {
23
Auth,
34
Connection,
@@ -22,15 +23,15 @@ import type { Themes } from "./data/ws-themes";
2223
import type { ExternalMessaging } from "./external_app/external_messaging";
2324

2425
declare global {
25-
/* eslint-disable no-var, no-redeclare */
26+
/* eslint-disable no-var */
2627
var __DEV__: boolean;
2728
var __DEMO__: boolean;
2829
var __BUILD__: "modern" | "legacy";
2930
var __VERSION__: string;
3031
var __STATIC_PATH__: string;
3132
var __BACKWARDS_COMPAT__: boolean;
3233
var __SUPERVISOR__: boolean;
33-
/* eslint-enable no-var, no-redeclare */
34+
/* eslint-enable no-var */
3435

3536
interface Window {
3637
// Custom panel entry point url
@@ -64,6 +65,12 @@ declare global {
6465
interface ImportMeta {
6566
url: string;
6667
}
68+
69+
// Intl.DurationFormat is not yet part of the TypeScript standard
70+
// eslint-disable-next-line @typescript-eslint/no-namespace
71+
namespace Intl {
72+
const DurationFormat: DurationFormatConstructor;
73+
}
6774
}
6875

6976
export interface ValueChangedEvent<T> extends CustomEvent {

test/common/datetime/format_duration.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import "@formatjs/intl-durationformat/polyfill-force";
12
import { assert, describe, it } from "vitest";
2-
33
import { formatDuration } from "../../../src/common/datetime/format_duration";
44
import type { FrontendLocaleData } from "../../../src/data/translation";
55
import {

0 commit comments

Comments
 (0)