Skip to content

Commit

Permalink
Fix textInterval - more accurate calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
st3v3nmw committed Jan 2, 2022
1 parent 20ff441 commit 3b0446a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
16 changes: 6 additions & 10 deletions src/scheduling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,16 @@ export function schedule(
}

export function textInterval(interval: number, isMobile: boolean): string {
const m: number = Math.round(interval / 3) / 10,
const m: number = Math.round(interval / 3.04375) / 10,
y: number = Math.round(interval / 36.525) / 10;

if (isMobile) {
if (interval < 30) return t("DAYS_STR_IVL_MOBILE", { interval });
else if (interval < 365) return t("MONTHS_STR_IVL_MOBILE", { interval: m });
if (m < 1.0) return t("DAYS_STR_IVL_MOBILE", { interval });
else if (y < 1.0) return t("MONTHS_STR_IVL_MOBILE", { interval: m });
else return t("YEARS_STR_IVL_MOBILE", { interval: y });
} else {
if (interval < 30) {
return t("DAYS_STR_IVL", { interval });
} else if (interval < 365) {
return t("MONTHS_STR_IVL", { interval: m });
} else {
return t("YEARS_STR_IVL", { interval: y });
}
if (m < 1.0) return t("DAYS_STR_IVL", { interval });
else if (y < 1.0) return t("MONTHS_STR_IVL", { interval: m });
else return t("YEARS_STR_IVL", { interval: y });
}
}
6 changes: 4 additions & 2 deletions tests/scheduling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,14 @@ test("Test load balancing", () => {

test("Test textInterval - desktop", () => {
expect(textInterval(1, false)).toEqual("1 day(s)");
expect(textInterval(41, false)).toEqual("1.4 month(s)");
expect(textInterval(41, false)).toEqual("1.3 month(s)");
expect(textInterval(366, false)).toEqual("1 year(s)");
expect(textInterval(1000, false)).toEqual("2.7 year(s)");
});

test("Test textInterval - mobile", () => {
expect(textInterval(1, true)).toEqual("1d");
expect(textInterval(41, true)).toEqual("1.4m");
expect(textInterval(41, true)).toEqual("1.3m");
expect(textInterval(366, true)).toEqual("1y");
expect(textInterval(1000, true)).toEqual("2.7y");
});

0 comments on commit 3b0446a

Please sign in to comment.