From 737344a72d23dc97b0d0e73cc4ab7fdffd0fbf49 Mon Sep 17 00:00:00 2001 From: Brendan F Date: Sun, 14 May 2023 23:12:27 -0700 Subject: Merge in react app code From sluchajfun and youtube-heardle-template --- src/helpers/index.ts | 3 +++ src/helpers/scoreToEmoji.ts | 32 ++++++++++++++++++++++++++++++++ src/helpers/searchSong.ts | 20 ++++++++++++++++++++ src/helpers/todaysSolution.ts | 12 ++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 src/helpers/index.ts create mode 100644 src/helpers/scoreToEmoji.ts create mode 100644 src/helpers/searchSong.ts create mode 100644 src/helpers/todaysSolution.ts (limited to 'src/helpers') diff --git a/src/helpers/index.ts b/src/helpers/index.ts new file mode 100644 index 0000000..5593bc5 --- /dev/null +++ b/src/helpers/index.ts @@ -0,0 +1,3 @@ +export { scoreToEmoji } from "./scoreToEmoji"; +export { searchSong } from "./searchSong"; +export { todaysSolution } from "./todaysSolution"; diff --git a/src/helpers/scoreToEmoji.ts b/src/helpers/scoreToEmoji.ts new file mode 100644 index 0000000..a118830 --- /dev/null +++ b/src/helpers/scoreToEmoji.ts @@ -0,0 +1,32 @@ +import { GuessType } from "../types/guess"; + +export function scoreToEmoji(guesses: GuessType[]): string { + const msInDay = 86400000; + const startDate = new Date('4/15/2022'); + const todaysDate = new Date(); + const index = Math.floor((todaysDate.getTime() - startDate.getTime() )/msInDay) + 1 + const emojis = { + incorrect: "🟥", + correct: "🟩", + skip: "⬜", + empty: "⬛️", + }; + // const todaysDate = new Date(); + const prefix = `HeardleTemplate - #${index} 🎧`; + + let scoreEmoji = ""; + + guesses.forEach((guess: GuessType) => { + if (guess.isCorrect === true) { + scoreEmoji += emojis.correct; + } else if (guess.skipped === true) { + scoreEmoji += emojis.skip; + } else if (guess.isCorrect === false) { + scoreEmoji += emojis.incorrect; + } else { + scoreEmoji += emojis.empty; + } + }); + + return `${prefix} ${scoreEmoji}`; +} diff --git a/src/helpers/searchSong.ts b/src/helpers/searchSong.ts new file mode 100644 index 0000000..24132f6 --- /dev/null +++ b/src/helpers/searchSong.ts @@ -0,0 +1,20 @@ +import { songs } from "../constants"; +import { Song } from "../types/song"; + +export function searchSong(searchTerm: string): Song[] { + searchTerm = searchTerm.toLowerCase(); + + return songs + .filter((song: Song) => { + const songName = song.name.toLowerCase(); + const songArtist = song.artist.toLowerCase(); + + if (songArtist.includes(searchTerm) || songName.includes(searchTerm)) { + return song; + } + }) + .sort((a, b) => + a.artist.toLowerCase().localeCompare(b.artist.toLocaleLowerCase()) + ) + .slice(0, 5); +} diff --git a/src/helpers/todaysSolution.ts b/src/helpers/todaysSolution.ts new file mode 100644 index 0000000..50e75ce --- /dev/null +++ b/src/helpers/todaysSolution.ts @@ -0,0 +1,12 @@ +import { songs } from "../constants"; + +// const epochMs = new Date(2022, 3, 10).valueOf(); +// const now = Date.now(); +// const index = Math.floor((now - epochMs) / msInDay); + +const msInDay = 86400000; +const startDate = new Date('4/15/2022'); +const todaysDate = new Date(); +const index = Math.floor((todaysDate.getTime() - startDate.getTime() )/msInDay) + +export const todaysSolution = songs[index % songs.length]; -- cgit v1.2.3