mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-09 02:16:35 +00:00
Don't use duration formatting for second unit (#23132)
Don't use duration formatting for sec unit
This commit is contained in:
parent
3a2cb51f8d
commit
8f4e65d392
@ -71,7 +71,7 @@ export const formatDurationDigital = (
|
|||||||
duration: HaDurationData
|
duration: HaDurationData
|
||||||
) => formatDigitalDurationMem(locale).format(duration);
|
) => formatDigitalDurationMem(locale).format(duration);
|
||||||
|
|
||||||
export const DURATION_UNITS = ["s", "min", "h", "d"] as const;
|
export const DURATION_UNITS = ["min", "h", "d"] as const;
|
||||||
|
|
||||||
type DurationUnit = (typeof DURATION_UNITS)[number];
|
type DurationUnit = (typeof DURATION_UNITS)[number];
|
||||||
|
|
||||||
@ -99,14 +99,6 @@ const formatDurationMinuteMem = memoizeOne(
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
const formatDurationSecondMem = memoizeOne(
|
|
||||||
(locale: FrontendLocaleData) =>
|
|
||||||
new Intl.DurationFormat(locale.language, {
|
|
||||||
style: "narrow",
|
|
||||||
secondsDisplay: "always",
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
export const formatDuration = (
|
export const formatDuration = (
|
||||||
locale: FrontendLocaleData,
|
locale: FrontendLocaleData,
|
||||||
duration: string,
|
duration: string,
|
||||||
@ -146,15 +138,6 @@ export const formatDuration = (
|
|||||||
};
|
};
|
||||||
return formatDurationMinuteMem(locale).format(input);
|
return formatDurationMinuteMem(locale).format(input);
|
||||||
}
|
}
|
||||||
case "s": {
|
|
||||||
const seconds = Math.floor(value);
|
|
||||||
const milliseconds = Math.floor((value - seconds) * 1000);
|
|
||||||
const input: DurationInput = {
|
|
||||||
seconds,
|
|
||||||
milliseconds,
|
|
||||||
};
|
|
||||||
return formatDurationSecondMem(locale).format(input);
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
throw new Error("Invalid duration unit");
|
throw new Error("Invalid duration unit");
|
||||||
}
|
}
|
||||||
|
@ -21,11 +21,6 @@ const LOCALE: FrontendLocaleData = {
|
|||||||
|
|
||||||
describe("formatDuration", () => {
|
describe("formatDuration", () => {
|
||||||
it("works", () => {
|
it("works", () => {
|
||||||
assert.strictEqual(formatDuration(LOCALE, "0.5", "s"), "0s 500ms");
|
|
||||||
assert.strictEqual(formatDuration(LOCALE, "1", "s"), "1s");
|
|
||||||
assert.strictEqual(formatDuration(LOCALE, "1.1", "s"), "1s 100ms");
|
|
||||||
assert.strictEqual(formatDuration(LOCALE, "65", "s"), "65s");
|
|
||||||
|
|
||||||
assert.strictEqual(formatDuration(LOCALE, "0.25", "min"), "0m 15s");
|
assert.strictEqual(formatDuration(LOCALE, "0.25", "min"), "0m 15s");
|
||||||
assert.strictEqual(formatDuration(LOCALE, "0.5", "min"), "0m 30s");
|
assert.strictEqual(formatDuration(LOCALE, "0.5", "min"), "0m 30s");
|
||||||
assert.strictEqual(formatDuration(LOCALE, "1", "min"), "1m");
|
assert.strictEqual(formatDuration(LOCALE, "1", "min"), "1m");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user