diff options
| author | Factiven <[email protected]> | 2023-09-13 00:45:53 +0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-09-13 00:45:53 +0700 |
| commit | 7327a69b55a20b99b14ee0803d6cf5f8b88c45ef (patch) | |
| tree | cbcca777593a8cc4b0282e7d85a6fc51ba517e25 /utils | |
| parent | Update issue templates (diff) | |
| download | moopa-7327a69b55a20b99b14ee0803d6cf5f8b88c45ef.tar.xz moopa-7327a69b55a20b99b14ee0803d6cf5f8b88c45ef.zip | |
Update v4 - Merge pre-push to main (#71)
* Create build-test.yml
* initial v4 commit
* update: github workflow
* update: push on branch
* Update .github/ISSUE_TEMPLATE/bug_report.md
* configuring next.config.js file
Diffstat (limited to 'utils')
| -rw-r--r-- | utils/getFormat.js | 17 | ||||
| -rw-r--r-- | utils/getGreetings.js | 16 | ||||
| -rw-r--r-- | utils/getTimes.js | 51 | ||||
| -rw-r--r-- | utils/schedulesUtils.js | 83 |
4 files changed, 167 insertions, 0 deletions
diff --git a/utils/getFormat.js b/utils/getFormat.js new file mode 100644 index 0000000..9a2e3e3 --- /dev/null +++ b/utils/getFormat.js @@ -0,0 +1,17 @@ +const data = [ + { name: "TV Show", value: "TV" }, + { name: "TV Short", value: "TV_SHORT" }, + { name: "Movie", value: "MOVIE" }, + { name: "Special", value: "SPECIAL" }, + { name: "OVA", value: "OVA" }, + { name: "ONA", value: "ONA" }, + { name: "Music", value: "MUSIC" }, + { name: "Manga", value: "MANGA" }, + { name: "Novel", value: "NOVEL" }, + { name: "One Shot", value: "ONE_SHOT" }, +]; + +export function getFormat(format) { + const results = data.find((item) => item.value === format); + return results?.name; +} diff --git a/utils/getGreetings.js b/utils/getGreetings.js new file mode 100644 index 0000000..1dd2a53 --- /dev/null +++ b/utils/getGreetings.js @@ -0,0 +1,16 @@ +export const getGreetings = () => { + const time = new Date().getHours(); + let greeting = ""; + + if (time >= 5 && time < 12) { + greeting = "Good morning"; + } else if (time >= 12 && time < 18) { + greeting = "Good afternoon"; + } else if (time >= 18 && time < 22) { + greeting = "Good evening"; + } else if (time >= 22 || time < 5) { + greeting = "Good night"; + } + + return greeting; +}; diff --git a/utils/getTimes.js b/utils/getTimes.js index 4bb8031..8bbc2ee 100644 --- a/utils/getTimes.js +++ b/utils/getTimes.js @@ -34,10 +34,34 @@ export function getCurrentSeason() { } } +export function convertUnixToCountdown(time) { + let date = new Date(time * 1000); + let days = date.getDay(); + let hours = date.getHours(); + let minutes = date.getMinutes(); + + let countdown = ""; + + if (days > 0) { + countdown += `${days}d `; + } + + if (hours > 0) { + countdown += `${hours}h `; + } + + if (minutes > 0) { + countdown += `${minutes}m `; + } + + return countdown.trim(); +} + export function convertSecondsToTime(sec) { let days = Math.floor(sec / (3600 * 24)); let hours = Math.floor((sec % (3600 * 24)) / 3600); let minutes = Math.floor((sec % 3600) / 60); + let seconds = Math.floor(sec % 60); let time = ""; @@ -53,5 +77,32 @@ export function convertSecondsToTime(sec) { time += `${minutes}m `; } + if (days <= 0) { + time += `${seconds}s `; + } + return time.trim(); } + +// Function to convert timestamp to AM/PM time format +export const timeStamptoAMPM = (timestamp) => { + const date = new Date(timestamp * 1000); + const hours = date.getHours(); + const minutes = date.getMinutes(); + const ampm = hours >= 12 ? "PM" : "AM"; + const formattedHours = hours % 12 || 12; // Convert to 12-hour format + + return `${formattedHours}:${minutes.toString().padStart(2, "0")} ${ampm}`; +}; + +export const timeStamptoHour = (timestamp) => { + const options = { hour: "numeric", minute: "numeric", hour12: true }; + const currentTime = new Date().getTime() / 1000; + const formattedTime = new Date(timestamp * 1000).toLocaleTimeString( + undefined, + options + ); + const status = timestamp <= currentTime ? "aired" : "airing"; + + return `${status} at ${formattedTime}`; +}; diff --git a/utils/schedulesUtils.js b/utils/schedulesUtils.js new file mode 100644 index 0000000..cb8c474 --- /dev/null +++ b/utils/schedulesUtils.js @@ -0,0 +1,83 @@ +// Function to transform the schedule data into the desired format +export const transformSchedule = (schedule) => { + const formattedSchedule = {}; + + for (const day of Object.keys(schedule)) { + formattedSchedule[day] = {}; + + for (const scheduleItem of schedule[day]) { + const time = scheduleItem.airingAt; + + if (!formattedSchedule[day][time]) { + formattedSchedule[day][time] = []; + } + + formattedSchedule[day][time].push(scheduleItem); + } + } + + return formattedSchedule; +}; + +export const sortScheduleByDay = (schedule) => { + const daysOfWeek = [ + "Saturday", + "Sunday", + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + ]; + + // Get the current day of the week (0 = Sunday, 1 = Monday, ...) + const currentDay = new Date().getDay(); + + // Reorder days of the week to start with today + const orderedDays = [ + ...daysOfWeek.slice(currentDay), + ...daysOfWeek.slice(0, currentDay), + ]; + + // Create a new object with sorted days + const sortedSchedule = {}; + orderedDays.forEach((day) => { + if (schedule[day]) { + sortedSchedule[day] = schedule[day]; + } + }); + + return sortedSchedule; +}; + +export const filterScheduleByDay = (sortedSchedule, filterDay) => { + if (filterDay === "All") return sortedSchedule; + // Create a new object to store the filtered schedules + const filteredSchedule = {}; + + // Iterate through the keys (days) in sortedSchedule + for (const day in sortedSchedule) { + // Check if the current day matches the filterDay + if (day === filterDay) { + // If it matches, add the schedules for that day to the filteredSchedule object + filteredSchedule[day] = sortedSchedule[day]; + } + } + + // Return the filtered schedule + return filteredSchedule; +}; + +export const filterFormattedSchedule = (formattedSchedule, filterDay) => { + if (filterDay === "All") return formattedSchedule; + + // Check if the selected day exists in the formattedSchedule + if (formattedSchedule.hasOwnProperty(filterDay)) { + return { + [filterDay]: formattedSchedule[filterDay], + }; + } + + // If the selected day does not exist, return an empty object + return {}; +}; |