Change all occurrences to use Intl.ListFormat (#16897)

This commit is contained in:
Simon Lamon 2023-06-19 12:52:28 +02:00 committed by GitHub
parent 41310007fe
commit 80f3d6aacb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -90,22 +90,18 @@ export const describeTrigger = (
// Event Trigger // Event Trigger
if (trigger.platform === "event" && trigger.event_type) { if (trigger.platform === "event" && trigger.event_type) {
let eventTypes = ""; const eventTypes: string[] = [];
if (Array.isArray(trigger.event_type)) { if (Array.isArray(trigger.event_type)) {
for (const [index, state] of trigger.event_type.entries()) { for (const state of trigger.event_type.values()) {
eventTypes += `${index > 0 ? "," : ""} ${ eventTypes.push(state);
trigger.event_type.length > 1 &&
index === trigger.event_type.length - 1
? "or"
: ""
} ${state}`;
} }
} else { } else {
eventTypes = trigger.event_type.toString(); eventTypes.push(trigger.event_type);
} }
return `When ${eventTypes} event is fired`; const eventTypesString = disjunctionFormatter.format(eventTypes);
return `When ${eventTypesString} event is fired`;
} }
// Home Assistant Trigger // Home Assistant Trigger
@ -157,7 +153,7 @@ export const describeTrigger = (
// State Trigger // State Trigger
if (trigger.platform === "state") { if (trigger.platform === "state") {
let base = "When"; let base = "When";
let entities = ""; const entities: string[] = [];
const states = hass.states; const states = hass.states;
if (trigger.attribute) { if (trigger.attribute) {
@ -173,25 +169,22 @@ export const describeTrigger = (
} }
if (Array.isArray(trigger.entity_id)) { if (Array.isArray(trigger.entity_id)) {
for (const [index, entity] of trigger.entity_id.entries()) { for (const entity of trigger.entity_id.values()) {
if (states[entity]) { if (states[entity]) {
entities += `${index > 0 ? "," : ""} ${ entities.push(computeStateName(states[entity]) || entity);
trigger.entity_id.length > 1 &&
index === trigger.entity_id.length - 1
? "or"
: ""
} ${computeStateName(states[entity]) || entity}`;
} }
} }
} else if (trigger.entity_id) { } else if (trigger.entity_id) {
entities = states[trigger.entity_id] entities.push(
? computeStateName(states[trigger.entity_id]) states[trigger.entity_id]
: trigger.entity_id; ? computeStateName(states[trigger.entity_id])
: trigger.entity_id
);
} }
if (!entities) { if (entities.length === 0) {
// no entity_id or empty array // no entity_id or empty array
entities = "something"; entities.push("something");
} }
base += ` ${entities} changes`; base += ` ${entities} changes`;
@ -208,13 +201,9 @@ export const describeTrigger = (
base += " from any state"; base += " from any state";
} }
} else if (Array.isArray(trigger.from)) { } else if (Array.isArray(trigger.from)) {
let from = ""; const from: string[] = [];
for (const [index, state] of trigger.from.entries()) { for (const state of trigger.from.values()) {
from += `${index > 0 ? "," : ""} ${ from.push(
trigger.from.length > 1 && index === trigger.from.length - 1
? "or"
: ""
} '${
trigger.attribute trigger.attribute
? computeAttributeValueDisplay( ? computeAttributeValueDisplay(
hass.localize, hass.localize,
@ -224,7 +213,7 @@ export const describeTrigger = (
hass.entities, hass.entities,
trigger.attribute, trigger.attribute,
state state
) ).toString()
: computeStateDisplay( : computeStateDisplay(
hass.localize, hass.localize,
stateObj, stateObj,
@ -233,13 +222,14 @@ export const describeTrigger = (
hass.entities, hass.entities,
state state
) )
}'`; );
} }
if (from) { if (from.length !== 0) {
base += ` from ${from}`; const fromString = disjunctionFormatter.format(from);
base += ` from ${fromString}`;
} }
} else { } else {
base += ` from '${ base += ` from ${
trigger.attribute trigger.attribute
? computeAttributeValueDisplay( ? computeAttributeValueDisplay(
hass.localize, hass.localize,
@ -258,7 +248,7 @@ export const describeTrigger = (
hass.entities, hass.entities,
trigger.from.toString() trigger.from.toString()
).toString() ).toString()
}'`; }`;
} }
} }
@ -268,11 +258,9 @@ export const describeTrigger = (
base += " to any state"; base += " to any state";
} }
} else if (Array.isArray(trigger.to)) { } else if (Array.isArray(trigger.to)) {
let to = ""; const to: string[] = [];
for (const [index, state] of trigger.to.entries()) { for (const state of trigger.to.values()) {
to += `${index > 0 ? "," : ""} ${ to.push(
trigger.to.length > 1 && index === trigger.to.length - 1 ? "or" : ""
} '${
trigger.attribute trigger.attribute
? computeAttributeValueDisplay( ? computeAttributeValueDisplay(
hass.localize, hass.localize,
@ -291,13 +279,14 @@ export const describeTrigger = (
hass.entities, hass.entities,
state state
).toString() ).toString()
}'`; );
} }
if (to) { if (to.length !== 0) {
base += ` to ${to}`; const toString = disjunctionFormatter.format(to);
base += ` to ${toString}`;
} }
} else { } else {
base += ` to '${ base += ` to ${
trigger.attribute trigger.attribute
? computeAttributeValueDisplay( ? computeAttributeValueDisplay(
hass.localize, hass.localize,
@ -315,8 +304,8 @@ export const describeTrigger = (
hass.config, hass.config,
hass.entities, hass.entities,
trigger.to.toString() trigger.to.toString()
).toString() )
}'`; }`;
} }
} }
@ -501,9 +490,9 @@ export const describeTrigger = (
const states = hass.states; const states = hass.states;
if (Array.isArray(trigger.entity_id)) { if (Array.isArray(trigger.entity_id)) {
for (const [entity] of trigger.entity_id.entries()) { for (const entity of trigger.entity_id.values()) {
if (states[entity]) { if (states[entity]) {
entities.push(`${computeStateName(states[entity]) || entity}`); entities.push(computeStateName(states[entity]) || entity);
} }
} }
} else { } else {
@ -515,9 +504,9 @@ export const describeTrigger = (
} }
if (Array.isArray(trigger.zone)) { if (Array.isArray(trigger.zone)) {
for (const [zone] of trigger.zone.entries()) { for (const zone of trigger.zone.values()) {
if (states[zone]) { if (states[zone]) {
zones.push(`${computeStateName(states[zone]) || zone}`); zones.push(computeStateName(states[zone]) || zone);
} }
} }
} else { } else {
@ -537,47 +526,39 @@ export const describeTrigger = (
// Geo Location Trigger // Geo Location Trigger
if (trigger.platform === "geo_location" && trigger.source && trigger.zone) { if (trigger.platform === "geo_location" && trigger.source && trigger.zone) {
let sources = ""; const sources: string[] = [];
let zones = ""; const zones: string[] = [];
let zonesPlural = false;
const states = hass.states; const states = hass.states;
if (Array.isArray(trigger.source)) { if (Array.isArray(trigger.source)) {
for (const [index, source] of trigger.source.entries()) { for (const source of trigger.source.values()) {
sources += `${index > 0 ? "," : ""} ${ sources.push(source);
trigger.source.length > 1 && index === trigger.source.length - 1
? "or"
: ""
} ${source}`;
} }
} else { } else {
sources = trigger.source; sources.push(trigger.source);
} }
if (Array.isArray(trigger.zone)) { if (Array.isArray(trigger.zone)) {
if (trigger.zone.length > 1) { for (const zone of trigger.zone.values()) {
zonesPlural = true;
}
for (const [index, zone] of trigger.zone.entries()) {
if (states[zone]) { if (states[zone]) {
zones += `${index > 0 ? "," : ""} ${ zones.push(computeStateName(states[zone]) || zone);
trigger.zone.length > 1 && index === trigger.zone.length - 1
? "or"
: ""
} ${computeStateName(states[zone]) || zone}`;
} }
} }
} else { } else {
zones = states[trigger.zone] zones.push(
? computeStateName(states[trigger.zone]) states[trigger.zone]
: trigger.zone; ? computeStateName(states[trigger.zone])
: trigger.zone
);
} }
return `When ${sources} ${trigger.event}s ${zones} ${ const sourcesString = disjunctionFormatter.format(sources);
zonesPlural ? "zones" : "zone" const zonesString = disjunctionFormatter.format(zones);
return `When ${sourcesString} ${trigger.event}s ${zonesString} ${
zones.length > 1 ? "zones" : "zone"
}`; }`;
} }
// MQTT Trigger // MQTT Trigger
if (trigger.platform === "mqtt") { if (trigger.platform === "mqtt") {
return "When an MQTT message has been received"; return "When an MQTT message has been received";
@ -634,6 +615,10 @@ export const describeCondition = (
return condition.alias; return condition.alias;
} }
const conjunctionFormatter = new Intl.ListFormat("en", {
style: "long",
type: "conjunction",
});
const disjunctionFormatter = new Intl.ListFormat("en", { const disjunctionFormatter = new Intl.ListFormat("en", {
style: "long", style: "long",
type: "disjunction", type: "disjunction",
@ -708,21 +693,20 @@ export const describeCondition = (
} }
if (Array.isArray(condition.entity_id)) { if (Array.isArray(condition.entity_id)) {
let entities = ""; const entities: string[] = [];
for (const [index, entity] of condition.entity_id.entries()) { for (const entity of condition.entity_id.values()) {
if (hass.states[entity]) { if (hass.states[entity]) {
entities += `${index > 0 ? "," : ""} ${ entities.push(computeStateName(hass.states[entity]) || entity);
condition.entity_id.length > 1 &&
index === condition.entity_id.length - 1
? condition.match === "any"
? "or"
: "and"
: ""
} ${computeStateName(hass.states[entity]) || entity}`;
} }
} }
if (entities) { if (entities.length !== 0) {
base += ` ${entities} ${condition.entity_id.length > 1 ? "are" : "is"}`; const entitiesString =
condition.match === "any"
? disjunctionFormatter.format(entities)
: conjunctionFormatter.format(entities);
base += ` ${entitiesString} ${
condition.entity_id.length > 1 ? "are" : "is"
}`;
} else { } else {
// no entity_id or empty array // no entity_id or empty array
base += " an entity"; base += " an entity";
@ -735,7 +719,7 @@ export const describeCondition = (
} is`; } is`;
} }
let states = ""; const states: string[] = [];
const stateObj = const stateObj =
hass.states[ hass.states[
Array.isArray(condition.entity_id) Array.isArray(condition.entity_id)
@ -743,12 +727,8 @@ export const describeCondition = (
: condition.entity_id : condition.entity_id
]; ];
if (Array.isArray(condition.state)) { if (Array.isArray(condition.state)) {
for (const [index, state] of condition.state.entries()) { for (const state of condition.state.values()) {
states += `${index > 0 ? "," : ""} ${ states.push(
condition.state.length > 1 && index === condition.state.length - 1
? "or"
: ""
} '${
condition.attribute condition.attribute
? computeAttributeValueDisplay( ? computeAttributeValueDisplay(
hass.localize, hass.localize,
@ -758,7 +738,7 @@ export const describeCondition = (
hass.entities, hass.entities,
condition.attribute, condition.attribute,
state state
) ).toString()
: computeStateDisplay( : computeStateDisplay(
hass.localize, hass.localize,
stateObj, stateObj,
@ -767,10 +747,10 @@ export const describeCondition = (
hass.entities, hass.entities,
state state
) )
}'`; );
} }
} else if (condition.state !== "") { } else if (condition.state !== "") {
states = `'${ states.push(
condition.attribute condition.attribute
? computeAttributeValueDisplay( ? computeAttributeValueDisplay(
hass.localize, hass.localize,
@ -788,15 +768,16 @@ export const describeCondition = (
hass.config, hass.config,
hass.entities, hass.entities,
condition.state.toString() condition.state.toString()
).toString() )
}'`; );
} }
if (!states) { if (states.length === 0) {
states = "a state"; states.push("a state");
} }
base += ` ${states}`; const statesString = disjunctionFormatter.format(states);
base += ` ${statesString}`;
if (condition.for) { if (condition.for) {
const duration = describeDuration(condition.for); const duration = describeDuration(condition.for);
@ -885,17 +866,7 @@ export const describeCondition = (
`ui.panel.config.automation.editor.conditions.type.time.weekdays.${d}` `ui.panel.config.automation.editor.conditions.type.time.weekdays.${d}`
) )
); );
const last = localizedDays.pop(); result += " day is " + disjunctionFormatter.format(localizedDays);
result += " day is " + localizedDays.join(", ");
if (localizedDays.length) {
if (localizedDays.length > 1) {
result += ",";
}
result += " or ";
}
result += last;
} }
return result; return result;
@ -947,9 +918,9 @@ export const describeCondition = (
const states = hass.states; const states = hass.states;
if (Array.isArray(condition.entity_id)) { if (Array.isArray(condition.entity_id)) {
for (const [entity] of condition.entity_id.entries()) { for (const entity of condition.entity_id.values()) {
if (states[entity]) { if (states[entity]) {
entities.push(`${computeStateName(states[entity]) || entity}`); entities.push(computeStateName(states[entity]) || entity);
} }
} }
} else { } else {
@ -961,9 +932,9 @@ export const describeCondition = (
} }
if (Array.isArray(condition.zone)) { if (Array.isArray(condition.zone)) {
for (const [zone] of condition.zone.entries()) { for (const zone of condition.zone.values()) {
if (states[zone]) { if (states[zone]) {
zones.push(`${computeStateName(states[zone]) || zone}`); zones.push(computeStateName(states[zone]) || zone);
} }
} }
} else { } else {