Only use duration poly-fill when necessary (#23030)
parent
125805d6d1
commit
3411967fd9
|
@ -9,7 +9,6 @@ const outDir = join(paths.build_dir, "locale-data");
|
||||||
|
|
||||||
const INTL_POLYFILLS = {
|
const INTL_POLYFILLS = {
|
||||||
"intl-datetimeformat": "DateTimeFormat",
|
"intl-datetimeformat": "DateTimeFormat",
|
||||||
"intl-durationFormat": "DurationFormat",
|
|
||||||
"intl-displaynames": "DisplayNames",
|
"intl-displaynames": "DisplayNames",
|
||||||
"intl-listformat": "ListFormat",
|
"intl-listformat": "ListFormat",
|
||||||
"intl-numberformat": "NumberFormat",
|
"intl-numberformat": "NumberFormat",
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import { DurationFormat } from "@formatjs/intl-durationformat";
|
|
||||||
import type { DurationInput } from "@formatjs/intl-durationformat/src/types";
|
import type { DurationInput } from "@formatjs/intl-durationformat/src/types";
|
||||||
import memoizeOne from "memoize-one";
|
import memoizeOne from "memoize-one";
|
||||||
import type { HaDurationData } from "../../components/ha-duration-input";
|
import type { HaDurationData } from "../../components/ha-duration-input";
|
||||||
|
@ -49,7 +48,7 @@ export const formatNumericDuration = (
|
||||||
|
|
||||||
const formatDurationLongMem = memoizeOne(
|
const formatDurationLongMem = memoizeOne(
|
||||||
(locale: FrontendLocaleData) =>
|
(locale: FrontendLocaleData) =>
|
||||||
new DurationFormat(locale.language, {
|
new Intl.DurationFormat(locale.language, {
|
||||||
style: "long",
|
style: "long",
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
@ -61,7 +60,7 @@ export const formatDurationLong = (
|
||||||
|
|
||||||
const formatDigitalDurationMem = memoizeOne(
|
const formatDigitalDurationMem = memoizeOne(
|
||||||
(locale: FrontendLocaleData) =>
|
(locale: FrontendLocaleData) =>
|
||||||
new DurationFormat(locale.language, {
|
new Intl.DurationFormat(locale.language, {
|
||||||
style: "digital",
|
style: "digital",
|
||||||
hoursDisplay: "auto",
|
hoursDisplay: "auto",
|
||||||
})
|
})
|
||||||
|
@ -78,7 +77,7 @@ type DurationUnit = (typeof DURATION_UNITS)[number];
|
||||||
|
|
||||||
const formatDurationDayMem = memoizeOne(
|
const formatDurationDayMem = memoizeOne(
|
||||||
(locale: FrontendLocaleData) =>
|
(locale: FrontendLocaleData) =>
|
||||||
new DurationFormat(locale.language, {
|
new Intl.DurationFormat(locale.language, {
|
||||||
style: "narrow",
|
style: "narrow",
|
||||||
daysDisplay: "always",
|
daysDisplay: "always",
|
||||||
})
|
})
|
||||||
|
@ -86,7 +85,7 @@ const formatDurationDayMem = memoizeOne(
|
||||||
|
|
||||||
const formatDurationHourMem = memoizeOne(
|
const formatDurationHourMem = memoizeOne(
|
||||||
(locale: FrontendLocaleData) =>
|
(locale: FrontendLocaleData) =>
|
||||||
new DurationFormat(locale.language, {
|
new Intl.DurationFormat(locale.language, {
|
||||||
style: "narrow",
|
style: "narrow",
|
||||||
hoursDisplay: "always",
|
hoursDisplay: "always",
|
||||||
})
|
})
|
||||||
|
@ -94,7 +93,7 @@ const formatDurationHourMem = memoizeOne(
|
||||||
|
|
||||||
const formatDurationMinuteMem = memoizeOne(
|
const formatDurationMinuteMem = memoizeOne(
|
||||||
(locale: FrontendLocaleData) =>
|
(locale: FrontendLocaleData) =>
|
||||||
new DurationFormat(locale.language, {
|
new Intl.DurationFormat(locale.language, {
|
||||||
style: "narrow",
|
style: "narrow",
|
||||||
minutesDisplay: "always",
|
minutesDisplay: "always",
|
||||||
})
|
})
|
||||||
|
@ -102,7 +101,7 @@ const formatDurationMinuteMem = memoizeOne(
|
||||||
|
|
||||||
const formatDurationSecondMem = memoizeOne(
|
const formatDurationSecondMem = memoizeOne(
|
||||||
(locale: FrontendLocaleData) =>
|
(locale: FrontendLocaleData) =>
|
||||||
new DurationFormat(locale.language, {
|
new Intl.DurationFormat(locale.language, {
|
||||||
style: "narrow",
|
style: "narrow",
|
||||||
secondsDisplay: "always",
|
secondsDisplay: "always",
|
||||||
})
|
})
|
||||||
|
@ -110,7 +109,7 @@ const formatDurationSecondMem = memoizeOne(
|
||||||
|
|
||||||
const formatDurationMillisecondMem = memoizeOne(
|
const formatDurationMillisecondMem = memoizeOne(
|
||||||
(locale: FrontendLocaleData) =>
|
(locale: FrontendLocaleData) =>
|
||||||
new DurationFormat(locale.language, {
|
new Intl.DurationFormat(locale.language, {
|
||||||
style: "narrow",
|
style: "narrow",
|
||||||
millisecondsDisplay: "always",
|
millisecondsDisplay: "always",
|
||||||
})
|
})
|
||||||
|
|
11
src/types.ts
11
src/types.ts
|
@ -1,3 +1,4 @@
|
||||||
|
import type { DurationFormatConstructor } from "@formatjs/intl-durationformat/src/types";
|
||||||
import type {
|
import type {
|
||||||
Auth,
|
Auth,
|
||||||
Connection,
|
Connection,
|
||||||
|
@ -22,7 +23,7 @@ import type { Themes } from "./data/ws-themes";
|
||||||
import type { ExternalMessaging } from "./external_app/external_messaging";
|
import type { ExternalMessaging } from "./external_app/external_messaging";
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
/* eslint-disable no-var, no-redeclare */
|
/* eslint-disable no-var */
|
||||||
var __DEV__: boolean;
|
var __DEV__: boolean;
|
||||||
var __DEMO__: boolean;
|
var __DEMO__: boolean;
|
||||||
var __BUILD__: "modern" | "legacy";
|
var __BUILD__: "modern" | "legacy";
|
||||||
|
@ -30,7 +31,7 @@ declare global {
|
||||||
var __STATIC_PATH__: string;
|
var __STATIC_PATH__: string;
|
||||||
var __BACKWARDS_COMPAT__: boolean;
|
var __BACKWARDS_COMPAT__: boolean;
|
||||||
var __SUPERVISOR__: boolean;
|
var __SUPERVISOR__: boolean;
|
||||||
/* eslint-enable no-var, no-redeclare */
|
/* eslint-enable no-var */
|
||||||
|
|
||||||
interface Window {
|
interface Window {
|
||||||
// Custom panel entry point url
|
// Custom panel entry point url
|
||||||
|
@ -64,6 +65,12 @@ declare global {
|
||||||
interface ImportMeta {
|
interface ImportMeta {
|
||||||
url: string;
|
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 {
|
export interface ValueChangedEvent<T> extends CustomEvent {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
|
import "@formatjs/intl-durationformat/polyfill-force";
|
||||||
import { assert, describe, it } from "vitest";
|
import { assert, describe, it } from "vitest";
|
||||||
|
|
||||||
import { formatDuration } from "../../../src/common/datetime/format_duration";
|
import { formatDuration } from "../../../src/common/datetime/format_duration";
|
||||||
import type { FrontendLocaleData } from "../../../src/data/translation";
|
import type { FrontendLocaleData } from "../../../src/data/translation";
|
||||||
import {
|
import {
|
||||||
|
|
Loading…
Reference in New Issue