Skip to content

Commit

Permalink
perf: simplify control flow
Browse files Browse the repository at this point in the history
  • Loading branch information
jy95 committed May 1, 2024
1 parent 50557fb commit 64bfabc
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 103 deletions.
12 changes: 4 additions & 8 deletions src/translators/boundsDuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
34 changes: 17 additions & 17 deletions src/translators/boundsRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
36 changes: 18 additions & 18 deletions src/translators/countCountMax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
36 changes: 18 additions & 18 deletions src/translators/durationDurationMax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ");
}
36 changes: 18 additions & 18 deletions src/translators/frequencyFrequencyMax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
48 changes: 24 additions & 24 deletions src/translators/periodPeriodMax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
});
}

0 comments on commit 64bfabc

Please sign in to comment.