feat: create routes to load popular titles
This commit is contained in:
69
src/libs/getCurrentAndNextSeason.spec.ts
Normal file
69
src/libs/getCurrentAndNextSeason.spec.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { DateTime } from "luxon";
|
||||
|
||||
import { describe, expect, it } from "bun:test";
|
||||
|
||||
import { getCurrentAndNextSeason } from "./getCurrentAndNextSeason";
|
||||
|
||||
describe("getCurrentAndNextSeason", () => {
|
||||
it("now is current year, next is current year", () => {
|
||||
const now = DateTime.local(2023, 4, 1) as DateTime<true>;
|
||||
|
||||
const { current, next } = getCurrentAndNextSeason(now);
|
||||
|
||||
expect(current.year).toEqual(2023);
|
||||
expect(next.year).toEqual(2023);
|
||||
});
|
||||
|
||||
it("now is current year, next is next year", () => {
|
||||
const now = DateTime.local(2023, 11, 1) as DateTime<true>;
|
||||
|
||||
const { current, next } = getCurrentAndNextSeason(now);
|
||||
|
||||
expect(current.year).toEqual(2023);
|
||||
expect(next.year).toEqual(2024);
|
||||
});
|
||||
|
||||
it("months 1 - 3 are currently winter, next is spring", () => {
|
||||
[1, 2, 3].forEach((month) => {
|
||||
const now = DateTime.local(2023, month, 1) as DateTime<true>;
|
||||
|
||||
const { current, next } = getCurrentAndNextSeason(now);
|
||||
|
||||
expect(current.season).toEqual("WINTER");
|
||||
expect(next.season).toEqual("SPRING");
|
||||
});
|
||||
});
|
||||
|
||||
it("months 4 - 6 are currently spring, next is summer", () => {
|
||||
[4, 5, 6].forEach((month) => {
|
||||
const now = DateTime.local(2023, month, 1) as DateTime<true>;
|
||||
|
||||
const { current, next } = getCurrentAndNextSeason(now);
|
||||
|
||||
expect(current.season).toEqual("SPRING");
|
||||
expect(next.season).toEqual("SUMMER");
|
||||
});
|
||||
});
|
||||
|
||||
it("months 7 - 9 are currently summer, next is fall", () => {
|
||||
[7, 8, 9].forEach((month) => {
|
||||
const now = DateTime.local(2023, month, 1) as DateTime<true>;
|
||||
|
||||
const { current, next } = getCurrentAndNextSeason(now);
|
||||
|
||||
expect(current.season).toEqual("SUMMER");
|
||||
expect(next.season).toEqual("FALL");
|
||||
});
|
||||
});
|
||||
|
||||
it("months 10 - 12 are currently fall, next is winter", () => {
|
||||
[10, 11, 12].forEach((month) => {
|
||||
const now = DateTime.local(2023, month, 1) as DateTime<true>;
|
||||
|
||||
const { current, next } = getCurrentAndNextSeason(now);
|
||||
|
||||
expect(current.season).toEqual("FALL");
|
||||
expect(next.season).toEqual("WINTER");
|
||||
});
|
||||
});
|
||||
});
|
||||
35
src/libs/getCurrentAndNextSeason.ts
Normal file
35
src/libs/getCurrentAndNextSeason.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { DateTime } from "luxon";
|
||||
|
||||
type Season = "WINTER" | "SPRING" | "SUMMER" | "FALL";
|
||||
|
||||
interface SeasonInfo {
|
||||
season: Season;
|
||||
year: number;
|
||||
}
|
||||
|
||||
export function getCurrentAndNextSeason(now = DateTime.now()): {
|
||||
current: SeasonInfo;
|
||||
next: SeasonInfo;
|
||||
} {
|
||||
const seasons = ["WINTER", "SPRING", "SUMMER", "FALL"] as const;
|
||||
const seasonStartingMonth = [1, 4, 7, 10];
|
||||
const currentSeasonIndex = seasonStartingMonth.findLastIndex(
|
||||
(month) => now.month >= month,
|
||||
);
|
||||
const nextSeasonIndex = currentSeasonIndex + 1;
|
||||
|
||||
const currentSeason = seasons[currentSeasonIndex];
|
||||
const nextSeason = seasons[nextSeasonIndex % seasons.length];
|
||||
const nextYear = nextSeasonIndex === seasons.length ? now.year + 1 : now.year;
|
||||
|
||||
return {
|
||||
current: {
|
||||
season: currentSeason,
|
||||
year: now.year,
|
||||
},
|
||||
next: {
|
||||
season: nextSeason,
|
||||
year: nextYear,
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user