Improve pluralization in automation descriptions (#14080)

Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
Brandon Rothweiler 2022-10-17 03:50:11 -04:00 committed by GitHub
parent 0d623794ed
commit f627e98902
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 45 deletions

View File

@ -10,7 +10,7 @@ export const formatDuration = (duration: HaDurationData) => {
const ms = duration.milliseconds || 0; const ms = duration.milliseconds || 0;
if (d > 0) { if (d > 0) {
return `${d} days ${h}:${leftPad(m)}:${leftPad(s)}`; return `${d} day${d === 1 ? "" : "s"} ${h}:${leftPad(m)}:${leftPad(s)}`;
} }
if (h > 0) { if (h > 0) {
return `${h}:${leftPad(m)}:${leftPad(s)}`; return `${h}:${leftPad(m)}:${leftPad(s)}`;
@ -19,10 +19,10 @@ export const formatDuration = (duration: HaDurationData) => {
return `${m}:${leftPad(s)}`; return `${m}:${leftPad(s)}`;
} }
if (s > 0) { if (s > 0) {
return `${s} seconds`; return `${s} second${s === 1 ? "" : "s"}`;
} }
if (ms > 0) { if (ms > 0) {
return `${ms} milliseconds`; return `${ms} millisecond${ms === 1 ? "" : "s"}`;
} }
return null; return null;
}; };

View File

@ -374,35 +374,35 @@ export const describeCondition = (
if (condition.condition === "or") { if (condition.condition === "or") {
const conditions = ensureArray(condition.conditions); const conditions = ensureArray(condition.conditions);
let count = "condition"; if (!conditions || conditions.length === 0) {
return "Test if any condition matches";
if (conditions && conditions.length > 0) {
count = `of ${conditions.length} conditions`;
} }
const count = conditions.length;
return `Test if any ${count} matches`; return `Test if any of ${count} condition${count === 1 ? "" : "s"} matches`;
} }
if (condition.condition === "and") { if (condition.condition === "and") {
const conditions = ensureArray(condition.conditions); const conditions = ensureArray(condition.conditions);
const count = if (!conditions || conditions.length === 0) {
conditions && conditions.length > 0 return "Test if multiple conditions match";
? `${conditions.length} ` }
: "multiple"; const count = conditions.length;
return `Test if ${count} condition${count === 1 ? "" : "s"} match${
return `Test if ${count} conditions match`; count === 1 ? "es" : ""
}`;
} }
if (condition.condition === "not") { if (condition.condition === "not") {
const conditions = ensureArray(condition.conditions); const conditions = ensureArray(condition.conditions);
const what = if (!conditions || conditions.length === 0) {
conditions && conditions.length > 0 return "Test if no condition matches";
? `none of ${conditions.length} conditions match` }
: "no condition matches"; if (conditions.length === 1) {
return "Test if 1 condition does not match";
return `Test if ${what}`; }
return `Test if none of ${conditions.length} conditions match`;
} }
// State Condition // State Condition

View File

@ -185,7 +185,7 @@ interface BaseRepeat extends BaseAction {
} }
export interface CountRepeat extends BaseRepeat { export interface CountRepeat extends BaseRepeat {
count: number; count: number | string;
} }
export interface WhileRepeat extends BaseRepeat { export interface WhileRepeat extends BaseRepeat {

View File

@ -231,32 +231,37 @@ export const describeAction = <T extends ActionType>(
if (actionType === "choose") { if (actionType === "choose") {
const config = action as ChooseAction; const config = action as ChooseAction;
return config.choose if (config.choose) {
? `Choose between ${ const numActions =
ensureArray(config.choose).length + (config.default ? 1 : 0) ensureArray(config.choose).length + (config.default ? 1 : 0);
} actions` return `Choose between ${numActions} action${
: "Choose an action"; numActions === 1 ? "" : "s"
}`;
}
return "Choose an action";
} }
if (actionType === "repeat") { if (actionType === "repeat") {
const config = action as RepeatAction; const config = action as RepeatAction;
return `Repeat an action ${
"count" in config.repeat ? `${config.repeat.count} times` : "" let base = "Repeat an action";
}${ if ("count" in config.repeat) {
"while" in config.repeat const count = config.repeat.count;
? `while ${ensureArray(config.repeat.while) base += ` ${count} time${Number(count) === 1 ? "" : "s"}`;
.map((condition) => describeCondition(condition, hass)) } else if ("while" in config.repeat) {
.join(", ")} is true` base += ` while ${ensureArray(config.repeat.while)
: "until" in config.repeat .map((condition) => describeCondition(condition, hass))
? `until ${ensureArray(config.repeat.until) .join(", ")} is true`;
.map((condition) => describeCondition(condition, hass)) } else if ("until" in config.repeat) {
.join(", ")} is true` base += ` until ${ensureArray(config.repeat.until)
: "for_each" in config.repeat .map((condition) => describeCondition(condition, hass))
? `for every item: ${ensureArray(config.repeat.for_each) .join(", ")} is true`;
.map((item) => JSON.stringify(item)) } else if ("for_each" in config.repeat) {
.join(", ")}` base += ` for every item: ${ensureArray(config.repeat.for_each)
: "" .map((item) => JSON.stringify(item))
}`; .join(", ")}`;
}
return base;
} }
if (actionType === "check_condition") { if (actionType === "check_condition") {
@ -280,7 +285,8 @@ export const describeAction = <T extends ActionType>(
if (actionType === "parallel") { if (actionType === "parallel") {
const config = action as ParallelAction; const config = action as ParallelAction;
return `Run ${ensureArray(config.parallel).length} actions in parallel`; const numActions = ensureArray(config.parallel).length;
return `Run ${numActions} action${numActions === 1 ? "" : "s"} in parallel`;
} }
return actionType; return actionType;