Last active
February 12, 2025 02:41
-
-
Save nillpo/bc2386a7a0f2a5785084e3f545cb0f08 to your computer and use it in GitHub Desktop.
KEEB_PDの開催日付とURLを生成するやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { assertEquals } from "jsr:@std/assert@^1.0.11"; | |
| // KEEB_PDの初回からのURLを生成する | |
| const KEEB_PD_START_DATE = "2020-07-19"; | |
| const KEEB_PD = "KEEB_PD"; | |
| type Schedules = { | |
| round: string; | |
| since: Temporal.PlainDate; | |
| until: Temporal.PlainDate; | |
| }[]; | |
| const EVENT_BREAK_WEEKS: Map<number, { weeks: number; reason: string }> = | |
| new Map([ | |
| [155, { weeks: 3, reason: "イーロンによる障害のためスキップ" }], | |
| [172, { weeks: 2, reason: "忘れちゃった" }], | |
| ]); | |
| const calculateNextDate = ( | |
| round: number, | |
| date: Temporal.PlainDate, | |
| ): Temporal.PlainDate => { | |
| const breakInfo = EVENT_BREAK_WEEKS.get(round); | |
| return date.add({ weeks: breakInfo ? breakInfo.weeks : 1 }); | |
| }; | |
| export const generateScheduledRounds = (rounds: number) => { | |
| return Array.from({ length: rounds }, (_, roundCount) => roundCount + 1) | |
| .reduce<Schedules>( | |
| (events, currentRound) => { | |
| const lastEvent = events.at(-1); | |
| const since = lastEvent | |
| ? calculateNextDate(currentRound, lastEvent.since) | |
| : Temporal.PlainDate.from(KEEB_PD_START_DATE); | |
| const until = since.add({ days: 1 }); | |
| return [ | |
| ...events, | |
| { | |
| round: `#${KEEB_PD}${ | |
| currentRound === 1 ? "" : `_R${currentRound}` | |
| }`, | |
| since, | |
| until, | |
| }, | |
| ]; | |
| }, | |
| [], | |
| ); | |
| }; | |
| const convertRoundToUrl = (events: Schedules) => { | |
| return events.map(({ round, since, until }) => { | |
| const params = new URLSearchParams({ | |
| src: "typed_query", | |
| f: "live", | |
| q: `${round} since:${since} until:${until}`, | |
| }); | |
| return `https://x.com/search?${params.toString()}`; | |
| }); | |
| }; | |
| if (import.meta.main) { | |
| const round = Number(Deno.args[0]); | |
| if (isNaN(round)) throw new Error(`${Deno.args[0]} is Invalid Value.`); | |
| convertRoundToUrl(generateScheduledRounds(round)).forEach((url) => | |
| console.log(url) | |
| ); | |
| } | |
| Deno.bench(function bench1() { | |
| convertRoundToUrl(generateScheduledRounds(10000)).join("\n"); | |
| }); | |
| Deno.test(function test1() { | |
| assertEquals(generateScheduledRounds(1), [{ | |
| round: "#KEEB_PD", | |
| since: Temporal.PlainDate.from(KEEB_PD_START_DATE), | |
| until: Temporal.PlainDate.from(KEEB_PD_START_DATE).add({ days: 1 }), | |
| }]); | |
| }); | |
| Deno.test(function test2() { | |
| const rounds = generateScheduledRounds(236); | |
| assertEquals(rounds.length, 236); | |
| assertEquals(rounds[235], { | |
| round: "#KEEB_PD_R236", | |
| since: Temporal.PlainDate.from("2025-02-09"), | |
| until: Temporal.PlainDate.from("2025-02-10"), | |
| }); | |
| }); | |
| Deno.test(function test3() { | |
| const rounds = generateScheduledRounds(0); | |
| assertEquals(rounds, []); | |
| }); | |
| Deno.test(function test4() { | |
| const rounds = generateScheduledRounds(-1); | |
| assertEquals(rounds, []); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment