sendou.ink/utils/strings.ts
2021-05-30 15:55:36 +03:00

83 lines
2.0 KiB
TypeScript

import { weapons } from "./lists/weapons";
export const getLocalizedMonthYearString = (
month: number,
year: number,
locale: string
) => {
const dateForLocalization = new Date();
dateForLocalization.setDate(1);
dateForLocalization.setMonth(month - 1);
dateForLocalization.setFullYear(year);
return dateForLocalization.toLocaleString(locale, {
month: "long",
year: "numeric",
});
};
/**
* Return medal emoji for top 3, otherwise returns the number as string.
*/
export const getRankingString = (ranking: number) => {
switch (ranking) {
case 1:
return "🥇";
case 2:
return "🥈";
case 3:
return "🥉";
default:
return `${ranking}`;
}
};
export function makeNameUrlFriendly(name: string) {
return name.trim().replace(/\s\s+/g, " ").toLowerCase().replace(/ /g, "-");
}
// User attributes - should be virtuals in future if support gets added to Prisma
/**
* Takes user object and returns the formatted username morphing it with the Discord discriminator.
* @example
* // returns "Sendou#0043"
* getFullUsername({username: "Sendou", discriminator: "0043"})
*/
export const getFullUsername = ({
username,
discriminator,
}: {
username: string;
discriminator: string;
}) => `${username}#${discriminator}`;
export const getProfilePath = ({
discordId,
customUrlPath,
}: {
discordId: string;
customUrlPath: string | null | undefined;
}) => (customUrlPath ? `/u/${customUrlPath}` : `/u/${discordId}`);
export const getBooleanFromString = (value: unknown) => {
if (typeof value !== "string") return undefined;
if (value === "true") return true;
if (value === "false") return false;
return undefined;
};
export const getWeaponFromString = (value: unknown) => {
if (typeof value !== "string") return undefined;
if (weapons.includes(value as any)) return value;
return undefined;
};
export const capitalizeFirstLetter = (string: string) => {
return string.charAt(0).toUpperCase() + string.slice(1);
};