start backend work

This commit is contained in:
Lee
2024-10-08 15:32:02 +01:00
parent 04ce91b459
commit aa0a0c4c16
445 changed files with 367 additions and 11413 deletions

View File

@ -0,0 +1,13 @@
import { PlayerHistory } from "../types/player/player-history";
/**
* Sorts the player history based on date,
* so the most recent date is first
*
* @param history the player history
*/
export function sortPlayerHistory(history: Map<string, PlayerHistory>) {
return Array.from(history.entries()).sort(
(a, b) => Date.parse(b[0]) - Date.parse(a[0]) // Sort in descending order
);
}

View File

@ -0,0 +1,95 @@
/**
* This function returns the time ago of the input date
*
* @param input Date | number (timestamp)
* @returns the format of the time ago
*/
export function timeAgo(input: Date) {
const inputDate = new Date(input).getTime(); // Convert input to a Date object if it's not already
const now = new Date().getTime();
const deltaSeconds = Math.floor((now - inputDate) / 1000); // Get time difference in seconds
if (deltaSeconds <= 60) {
return "just now";
}
const timeUnits = [
{ unit: "y", seconds: 60 * 60 * 24 * 365 }, // years
{ unit: "mo", seconds: 60 * 60 * 24 * 30 }, // months
{ unit: "d", seconds: 60 * 60 * 24 }, // days
{ unit: "h", seconds: 60 * 60 }, // hours
{ unit: "m", seconds: 60 }, // minutes
];
const result = [];
let remainingSeconds = deltaSeconds;
for (const { unit, seconds } of timeUnits) {
const count = Math.floor(remainingSeconds / seconds);
if (count > 0) {
result.push(`${count}${unit}`);
remainingSeconds -= count * seconds;
}
// Stop after two units have been added
if (result.length === 2) break;
}
// Return formatted result with at most two units
return result.join(", ") + " ago";
}
/**
* Formats the date in the format "DD MMMM YYYY"
*
* @param date the date
*/
export function formatDateMinimal(date: Date) {
return date.toLocaleString("en-US", {
timeZone: "Europe/London",
day: "numeric",
month: "short",
year: "numeric",
});
}
/**
* Gets the midnight aligned date
*
* @param date the date
*/
export function getMidnightAlignedDate(date: Date) {
return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()));
}
/**
* Gets the date X days ago
*
* @param days the number of days to go back
* @returns {Date} A Date object representing the date X days ago
*/
export function getDaysAgoDate(days: number): Date {
const date = new Date();
date.setDate(date.getDate() - days);
return date;
}
/**
* Gets the amount of days ago a date was
*
* @param date the date
* @returns the amount of days
*/
export function getDaysAgo(date: Date): number {
const now = new Date();
const diffTime = Math.abs(now.getTime() - date.getTime());
return Math.ceil(diffTime / (1000 * 60 * 60 * 24)) - 1;
}
/**
* Parses a date from a string
*
* @param date the date
*/
export function parseDate(date: string): Date {
return new Date(date);
}

View File

@ -0,0 +1,6 @@
/**
* Checks if we're in production
*/
export function isProduction() {
return process.env.NODE_ENV === "production";
}