From 64bfabc350179a8610b58d92ae35b028304b9421 Mon Sep 17 00:00:00 2001 From: jy95 Date: Wed, 1 May 2024 14:10:26 +0200 Subject: [PATCH] perf: simplify control flow --- src/translators/boundsDuration.ts | 12 ++---- src/translators/boundsRange.ts | 34 ++++++++--------- src/translators/countCountMax.ts | 36 +++++++++--------- src/translators/durationDurationMax.ts | 36 +++++++++--------- src/translators/frequencyFrequencyMax.ts | 36 +++++++++--------- src/translators/periodPeriodMax.ts | 48 ++++++++++++------------ 6 files changed, 99 insertions(+), 103 deletions(-) diff --git a/src/translators/boundsDuration.ts b/src/translators/boundsDuration.ts index 25223797..713ad2c6 100644 --- a/src/translators/boundsDuration.ts +++ b/src/translators/boundsDuration.ts @@ -44,13 +44,9 @@ export function transformBoundsDurationToText({ // Do nothing if no boundsDuration, I am not a wizard if (boundsDuration === undefined) { return undefined; - } else { - let durationText = transformDurationToString( - i18next, - boundsDuration, - config, - ); - - return i18next.t("fields.boundsDuration", { durationText: durationText }); } + + let durationText = transformDurationToString(i18next, boundsDuration, config); + + return i18next.t("fields.boundsDuration", { durationText: durationText }); } diff --git a/src/translators/boundsRange.ts b/src/translators/boundsRange.ts index 3cc70535..0aec3d28 100644 --- a/src/translators/boundsRange.ts +++ b/src/translators/boundsRange.ts @@ -23,22 +23,22 @@ export function transformBoundsRangeToText({ // Do nothing if no boundsRange, I am not a wizard if (boundsRange === undefined) { return undefined; - } else { - // Turn range into a text - const rangeText = fromRangeToString({ - range: boundsRange, - config, - i18next, - }); - - // Reject if empty - if (rangeText === undefined) { - return undefined; - } - - // return the final string - return i18next.t("fields.boundsRange", { - rangeText: rangeText, - }); } + + // Turn range into a text + const rangeText = fromRangeToString({ + range: boundsRange, + config, + i18next, + }); + + // Reject if empty + if (rangeText === undefined) { + return undefined; + } + + // return the final string + return i18next.t("fields.boundsRange", { + rangeText: rangeText, + }); } diff --git a/src/translators/countCountMax.ts b/src/translators/countCountMax.ts index fd49bddc..32a7b084 100644 --- a/src/translators/countCountMax.ts +++ b/src/translators/countCountMax.ts @@ -20,23 +20,23 @@ export function transformCountCountMaxToText({ // Do nothing if no count, I am not a wizard if (count === undefined && countMax === undefined) { return undefined; - } else { - // Three cases - - // 1. Both count & countMax are present - if (count !== undefined && countMax !== undefined) { - return i18next.t("fields.countMax.countMax", { - count: countMax, - low: count, - }); - } - - // 2. Only countMax is present - if (countMax !== undefined) { - return i18next.t("fields.count.count", { count: countMax }); - } - - // 3. Only count is present - return i18next.t("fields.count.count", { count: count }); } + + // Three cases + + // 1. Both count & countMax are present + if (count !== undefined && countMax !== undefined) { + return i18next.t("fields.countMax.countMax", { + count: countMax, + low: count, + }); + } + + // 2. Only countMax is present + if (countMax !== undefined) { + return i18next.t("fields.count.count", { count: countMax }); + } + + // 3. Only count is present + return i18next.t("fields.count.count", { count: count }); } diff --git a/src/translators/durationDurationMax.ts b/src/translators/durationDurationMax.ts index d658dfd2..f990672d 100644 --- a/src/translators/durationDurationMax.ts +++ b/src/translators/durationDurationMax.ts @@ -21,24 +21,24 @@ export function transformDurationDurationMaxToText({ // Do nothing if no unit, I am not a wizard if (unit === undefined) { return undefined; - } else { - return [ - // duration - duration !== undefined && - i18next.t("fields.duration", { - durationText: i18next.t(`unitsOfTime:withCount.${unit}`, { - count: duration, - }), + } + + return [ + // duration + duration !== undefined && + i18next.t("fields.duration", { + durationText: i18next.t(`unitsOfTime:withCount.${unit}`, { + count: duration, }), - // durationMax - max !== undefined && - i18next.t("fields.durationMax", { - durationMaxText: i18next.t(`unitsOfTime:withCount.${unit}`, { - count: max, - }), + }), + // durationMax + max !== undefined && + i18next.t("fields.durationMax", { + durationMaxText: i18next.t(`unitsOfTime:withCount.${unit}`, { + count: max, }), - ] - .filter((s) => s !== false) - .join(" "); - } + }), + ] + .filter((s) => s !== false) + .join(" "); } diff --git a/src/translators/frequencyFrequencyMax.ts b/src/translators/frequencyFrequencyMax.ts index ec082230..25621c9a 100644 --- a/src/translators/frequencyFrequencyMax.ts +++ b/src/translators/frequencyFrequencyMax.ts @@ -20,23 +20,23 @@ export function transformFrequencyFrequencyMaxToText({ // Do nothing if no frequency / frequencyMax, I am not a wizard if (frequency === undefined && max === undefined) { return undefined; - } else { - // Three cases - - // 1. Frequency and frequencyMax are present - if (frequency !== undefined && max !== undefined) { - return i18next.t("fields.frequency.withfrequencyMax", { - count: max, - frequency: frequency, - }); - } - - // 2. Only frequencyMax is present - if (max !== undefined) { - return i18next.t("fields.frequencyMax.frequencyMax", { count: max }); - } - - // 3. Only frequency is present - return i18next.t("fields.frequency.onlyFrequency", { count: frequency }); } + + // Three cases + + // 1. Frequency and frequencyMax are present + if (frequency !== undefined && max !== undefined) { + return i18next.t("fields.frequency.withfrequencyMax", { + count: max, + frequency: frequency, + }); + } + + // 2. Only frequencyMax is present + if (max !== undefined) { + return i18next.t("fields.frequencyMax.frequencyMax", { count: max }); + } + + // 3. Only frequency is present + return i18next.t("fields.frequency.onlyFrequency", { count: frequency }); } diff --git a/src/translators/periodPeriodMax.ts b/src/translators/periodPeriodMax.ts index 8b634102..7fbb7c40 100644 --- a/src/translators/periodPeriodMax.ts +++ b/src/translators/periodPeriodMax.ts @@ -21,30 +21,30 @@ export function transformPeriodPeriodMaxToText({ // Do nothing if no unit, I am not a wizard if (unit === undefined) { return undefined; - } else { - // Three cases - - // 1. period and periodMax are present - if (period !== undefined && max !== undefined) { - return i18next.t("fields.periodMax.withPeriod", { - period: period, - count: max, - unit: i18next.t(`unitsOfTime:withoutCount.${unit}`, { count: max }), - }); - } - - // 2. Only periodMax is present - if (max !== undefined) { - return i18next.t("fields.periodMax.onlyPeriodMax", { - count: max, - unit: i18next.t(`unitsOfTime:withoutCount.${unit}`, { count: max }), - }); - } - - // 3. Only period present - return i18next.t("fields.period.period", { - count: period, - unit: i18next.t(`unitsOfTime:withoutCount.${unit}`, { count: period }), + } + + // Three cases + + // 1. period and periodMax are present + if (period !== undefined && max !== undefined) { + return i18next.t("fields.periodMax.withPeriod", { + period: period, + count: max, + unit: i18next.t(`unitsOfTime:withoutCount.${unit}`, { count: max }), }); } + + // 2. Only periodMax is present + if (max !== undefined) { + return i18next.t("fields.periodMax.onlyPeriodMax", { + count: max, + unit: i18next.t(`unitsOfTime:withoutCount.${unit}`, { count: max }), + }); + } + + // 3. Only period present + return i18next.t("fields.period.period", { + count: period, + unit: i18next.t(`unitsOfTime:withoutCount.${unit}`, { count: period }), + }); }