2022-05-08 10:26:36 +00:00
|
|
|
import { AppStaticProps } from "../graphql/getAppStaticProps";
|
2022-05-08 10:21:29 +00:00
|
|
|
import { convertPrice } from "./numbers";
|
2022-07-23 08:24:13 +00:00
|
|
|
import { isDefinedAndNotEmpty, isUndefined } from "./others";
|
2022-07-07 23:42:38 +00:00
|
|
|
import { DatePickerFragment, PricePickerFragment } from "graphql/generated";
|
2022-01-27 16:52:44 +00:00
|
|
|
|
2022-07-23 08:24:13 +00:00
|
|
|
export const prettyDate = (
|
|
|
|
datePicker: DatePickerFragment,
|
|
|
|
locale = "en",
|
|
|
|
dateStyle: Intl.DateTimeFormatOptions["dateStyle"] = "medium"
|
|
|
|
): string =>
|
|
|
|
new Date(
|
|
|
|
datePicker.year ?? 0,
|
|
|
|
datePicker.month ?? 0,
|
|
|
|
datePicker.day ?? 1
|
|
|
|
).toLocaleString(locale, { dateStyle });
|
2022-01-27 16:52:44 +00:00
|
|
|
|
2022-07-03 12:34:00 +00:00
|
|
|
export const prettyPrice = (
|
2022-06-18 02:39:18 +00:00
|
|
|
pricePicker: PricePickerFragment,
|
2022-03-31 12:59:31 +00:00
|
|
|
currencies: AppStaticProps["currencies"],
|
2022-03-06 17:23:10 +00:00
|
|
|
targetCurrencyCode?: string
|
2022-07-03 12:34:00 +00:00
|
|
|
): string => {
|
2022-03-06 17:23:10 +00:00
|
|
|
if (!targetCurrencyCode) return "";
|
2022-07-23 08:24:13 +00:00
|
|
|
if (isUndefined(pricePicker.amount)) return "";
|
|
|
|
|
|
|
|
const targetCurrency = currencies.find(
|
|
|
|
(currency) => currency.attributes?.code === targetCurrencyCode
|
|
|
|
);
|
|
|
|
|
|
|
|
if (targetCurrency?.attributes) {
|
|
|
|
const amountInTargetCurrency = convertPrice(pricePicker, targetCurrency);
|
|
|
|
return amountInTargetCurrency.toLocaleString("en", {
|
|
|
|
style: "currency",
|
|
|
|
currency: targetCurrency.attributes.code,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return pricePicker.amount.toLocaleString("en", {
|
|
|
|
style: "currency",
|
|
|
|
currency: pricePicker.currency?.data?.attributes?.code,
|
2022-03-06 17:23:10 +00:00
|
|
|
});
|
2022-07-03 12:34:00 +00:00
|
|
|
};
|
2022-01-27 16:52:44 +00:00
|
|
|
|
2022-07-03 12:34:00 +00:00
|
|
|
export const prettySlug = (slug?: string, parentSlug?: string): string => {
|
2022-07-07 23:42:38 +00:00
|
|
|
let newSlug = slug;
|
|
|
|
if (newSlug) {
|
|
|
|
if (isDefinedAndNotEmpty(parentSlug) && newSlug.startsWith(parentSlug))
|
|
|
|
newSlug = newSlug.substring(parentSlug.length + 1);
|
|
|
|
newSlug = newSlug.replaceAll("-", " ");
|
|
|
|
return capitalizeString(newSlug);
|
2022-02-24 12:25:43 +00:00
|
|
|
}
|
|
|
|
return "";
|
2022-07-03 12:34:00 +00:00
|
|
|
};
|
2022-02-08 04:52:22 +00:00
|
|
|
|
2022-07-19 18:06:31 +00:00
|
|
|
export const prettyInlineTitle = (
|
2022-07-07 23:42:38 +00:00
|
|
|
pretitle: string | null | undefined,
|
|
|
|
title: string | null | undefined,
|
|
|
|
subtitle: string | null | undefined
|
2022-07-03 12:34:00 +00:00
|
|
|
): string => {
|
2022-02-08 08:44:17 +00:00
|
|
|
let result = "";
|
2022-03-27 15:01:14 +00:00
|
|
|
if (pretitle) result += `${pretitle}: `;
|
2022-02-08 08:44:17 +00:00
|
|
|
result += title;
|
2022-03-27 15:01:14 +00:00
|
|
|
if (subtitle) result += ` - ${subtitle}`;
|
2022-02-08 08:44:17 +00:00
|
|
|
return result;
|
2022-07-03 12:34:00 +00:00
|
|
|
};
|
2022-02-08 08:44:17 +00:00
|
|
|
|
2022-07-03 12:34:00 +00:00
|
|
|
export const prettyItemType = (
|
2022-06-18 02:39:18 +00:00
|
|
|
metadata: any,
|
2022-03-31 12:59:31 +00:00
|
|
|
langui: AppStaticProps["langui"]
|
2022-07-10 00:47:32 +00:00
|
|
|
): string => {
|
2022-02-17 01:49:32 +00:00
|
|
|
switch (metadata.__typename) {
|
|
|
|
case "ComponentMetadataAudio":
|
2022-07-10 00:47:32 +00:00
|
|
|
return langui.audio ?? "Audio";
|
2022-02-17 01:49:32 +00:00
|
|
|
case "ComponentMetadataBooks":
|
2022-07-10 00:47:32 +00:00
|
|
|
return langui.textual ?? "Textual";
|
2022-02-17 01:49:32 +00:00
|
|
|
case "ComponentMetadataGame":
|
2022-07-10 00:47:32 +00:00
|
|
|
return langui.game ?? "Game";
|
2022-02-17 01:49:32 +00:00
|
|
|
case "ComponentMetadataVideo":
|
2022-07-10 00:47:32 +00:00
|
|
|
return langui.video ?? "Video";
|
2022-03-05 23:40:55 +00:00
|
|
|
case "ComponentMetadataGroup":
|
2022-07-10 00:47:32 +00:00
|
|
|
return langui.group ?? "Group";
|
2022-02-17 01:49:32 +00:00
|
|
|
case "ComponentMetadataOther":
|
2022-07-10 00:47:32 +00:00
|
|
|
return langui.other ?? "Other";
|
2022-02-17 01:49:32 +00:00
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
2022-07-03 12:34:00 +00:00
|
|
|
};
|
2022-02-17 01:49:32 +00:00
|
|
|
|
2022-07-07 23:42:38 +00:00
|
|
|
/* eslint-disable id-denylist */
|
2022-07-03 12:34:00 +00:00
|
|
|
export const prettyItemSubType = (
|
2022-06-18 02:39:18 +00:00
|
|
|
metadata:
|
2022-03-31 12:59:31 +00:00
|
|
|
| {
|
|
|
|
__typename: "ComponentMetadataAudio";
|
|
|
|
subtype?: {
|
|
|
|
data?: {
|
|
|
|
attributes?: {
|
|
|
|
slug: string;
|
2022-07-07 23:42:38 +00:00
|
|
|
titles?:
|
|
|
|
| ({
|
|
|
|
title: string;
|
|
|
|
} | null)[]
|
|
|
|
| null;
|
2022-03-31 12:59:31 +00:00
|
|
|
} | null;
|
|
|
|
} | null;
|
|
|
|
} | null;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
__typename: "ComponentMetadataBooks";
|
|
|
|
subtype?: {
|
|
|
|
data?: {
|
|
|
|
attributes?: {
|
|
|
|
slug: string;
|
2022-07-07 23:42:38 +00:00
|
|
|
titles?:
|
|
|
|
| ({
|
|
|
|
title: string;
|
|
|
|
} | null)[]
|
|
|
|
| null;
|
2022-03-31 12:59:31 +00:00
|
|
|
} | null;
|
|
|
|
} | null;
|
|
|
|
} | null;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
__typename: "ComponentMetadataGame";
|
|
|
|
platforms?: {
|
2022-07-07 23:42:38 +00:00
|
|
|
data: {
|
2022-03-31 12:59:31 +00:00
|
|
|
id?: string | null;
|
|
|
|
attributes?: {
|
|
|
|
short: string;
|
|
|
|
} | null;
|
2022-07-07 23:42:38 +00:00
|
|
|
}[];
|
2022-03-31 12:59:31 +00:00
|
|
|
} | null;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
__typename: "ComponentMetadataGroup";
|
|
|
|
subtype?: {
|
|
|
|
data?: {
|
|
|
|
attributes?: {
|
|
|
|
slug: string;
|
2022-07-07 23:42:38 +00:00
|
|
|
titles?:
|
|
|
|
| ({
|
|
|
|
title: string;
|
|
|
|
} | null)[]
|
|
|
|
| null;
|
2022-03-31 12:59:31 +00:00
|
|
|
} | null;
|
|
|
|
} | null;
|
|
|
|
} | null;
|
|
|
|
subitems_type?: {
|
|
|
|
data?: {
|
|
|
|
attributes?: {
|
|
|
|
slug: string;
|
2022-07-07 23:42:38 +00:00
|
|
|
titles?:
|
|
|
|
| ({
|
|
|
|
title: string;
|
|
|
|
} | null)[]
|
|
|
|
| null;
|
2022-03-31 12:59:31 +00:00
|
|
|
} | null;
|
|
|
|
} | null;
|
|
|
|
} | null;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
__typename: "ComponentMetadataVideo";
|
|
|
|
subtype?: {
|
|
|
|
data?: {
|
|
|
|
attributes?: {
|
|
|
|
slug: string;
|
2022-07-07 23:42:38 +00:00
|
|
|
titles?:
|
|
|
|
| ({
|
|
|
|
title: string;
|
|
|
|
} | null)[]
|
|
|
|
| null;
|
2022-03-31 12:59:31 +00:00
|
|
|
} | null;
|
|
|
|
} | null;
|
|
|
|
} | null;
|
|
|
|
}
|
2022-07-07 23:42:38 +00:00
|
|
|
| { __typename: "ComponentMetadataOther" }
|
2022-03-31 12:59:31 +00:00
|
|
|
| { __typename: "Error" }
|
|
|
|
| null
|
2022-07-03 12:34:00 +00:00
|
|
|
): string => {
|
2022-03-31 12:59:31 +00:00
|
|
|
if (metadata) {
|
|
|
|
switch (metadata.__typename) {
|
|
|
|
case "ComponentMetadataAudio":
|
|
|
|
case "ComponentMetadataBooks":
|
|
|
|
case "ComponentMetadataVideo":
|
|
|
|
return metadata.subtype?.data?.attributes?.titles &&
|
2022-07-07 23:42:38 +00:00
|
|
|
metadata.subtype.data.attributes.titles.length > 0 &&
|
2022-03-31 12:59:31 +00:00
|
|
|
metadata.subtype.data.attributes.titles[0]
|
2022-03-05 23:40:55 +00:00
|
|
|
? metadata.subtype.data.attributes.titles[0].title
|
2022-03-31 12:59:31 +00:00
|
|
|
: prettySlug(metadata.subtype?.data?.attributes?.slug);
|
|
|
|
case "ComponentMetadataGame":
|
|
|
|
return metadata.platforms?.data &&
|
2022-07-07 23:42:38 +00:00
|
|
|
metadata.platforms.data.length > 0 &&
|
2022-03-31 12:59:31 +00:00
|
|
|
metadata.platforms.data[0].attributes
|
|
|
|
? metadata.platforms.data[0].attributes.short
|
|
|
|
: "";
|
|
|
|
case "ComponentMetadataGroup": {
|
|
|
|
const firstPart =
|
|
|
|
metadata.subtype?.data?.attributes?.titles &&
|
2022-07-07 23:42:38 +00:00
|
|
|
metadata.subtype.data.attributes.titles.length > 0 &&
|
2022-03-31 12:59:31 +00:00
|
|
|
metadata.subtype.data.attributes.titles[0]
|
|
|
|
? metadata.subtype.data.attributes.titles[0].title
|
|
|
|
: prettySlug(metadata.subtype?.data?.attributes?.slug);
|
2022-03-05 23:40:55 +00:00
|
|
|
|
2022-03-31 12:59:31 +00:00
|
|
|
const secondPart =
|
|
|
|
metadata.subitems_type?.data?.attributes?.titles &&
|
2022-07-07 23:42:38 +00:00
|
|
|
metadata.subitems_type.data.attributes.titles.length > 0 &&
|
2022-03-31 12:59:31 +00:00
|
|
|
metadata.subitems_type.data.attributes.titles[0]
|
|
|
|
? metadata.subitems_type.data.attributes.titles[0].title
|
|
|
|
: prettySlug(metadata.subitems_type?.data?.attributes?.slug);
|
|
|
|
return `${secondPart} ${firstPart}`;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return "";
|
2022-03-05 23:40:55 +00:00
|
|
|
}
|
2022-02-17 01:49:32 +00:00
|
|
|
}
|
2022-03-31 12:59:31 +00:00
|
|
|
return "";
|
2022-07-03 12:34:00 +00:00
|
|
|
};
|
2022-07-07 23:42:38 +00:00
|
|
|
/* eslint-enable id-denylist */
|
2022-02-17 01:49:32 +00:00
|
|
|
|
2022-07-03 12:34:00 +00:00
|
|
|
export const prettyShortenNumber = (number: number): string => {
|
2022-04-01 18:47:42 +00:00
|
|
|
if (number > 1000000) {
|
|
|
|
return number.toLocaleString(undefined, {
|
|
|
|
maximumSignificantDigits: 3,
|
|
|
|
});
|
|
|
|
} else if (number > 1000) {
|
2022-07-07 23:42:38 +00:00
|
|
|
return `${(number / 1000).toLocaleString(undefined, {
|
|
|
|
maximumSignificantDigits: 2,
|
|
|
|
})}K`;
|
2022-04-01 18:47:42 +00:00
|
|
|
}
|
|
|
|
return number.toLocaleString();
|
2022-07-03 12:34:00 +00:00
|
|
|
};
|
2022-04-01 18:47:42 +00:00
|
|
|
|
2022-07-03 12:34:00 +00:00
|
|
|
export const prettyDuration = (seconds: number): string => {
|
2022-04-01 18:47:42 +00:00
|
|
|
let hours = 0;
|
|
|
|
let minutes = 0;
|
2022-07-07 23:42:38 +00:00
|
|
|
let remainingSeconds = seconds;
|
|
|
|
while (remainingSeconds > 60) {
|
|
|
|
minutes++;
|
|
|
|
remainingSeconds -= 60;
|
2022-04-01 18:47:42 +00:00
|
|
|
}
|
|
|
|
while (minutes > 60) {
|
2022-07-07 23:42:38 +00:00
|
|
|
hours++;
|
2022-04-01 18:47:42 +00:00
|
|
|
minutes -= 60;
|
|
|
|
}
|
|
|
|
let result = "";
|
2022-07-07 23:42:38 +00:00
|
|
|
if (hours) result += `${hours.toString().padStart(2, "0")}:`;
|
|
|
|
result += `${minutes.toString().padStart(2, "0")}:`;
|
|
|
|
result += remainingSeconds.toString().padStart(2, "0");
|
2022-04-01 18:47:42 +00:00
|
|
|
return result;
|
2022-07-03 12:34:00 +00:00
|
|
|
};
|
2022-04-01 18:47:42 +00:00
|
|
|
|
2022-07-03 12:34:00 +00:00
|
|
|
export const prettyLanguage = (
|
2022-03-12 17:12:02 +00:00
|
|
|
code: string,
|
2022-03-31 12:59:31 +00:00
|
|
|
languages: AppStaticProps["languages"]
|
2022-07-03 12:34:00 +00:00
|
|
|
): string => {
|
2022-03-12 17:12:02 +00:00
|
|
|
let result = code;
|
|
|
|
languages.forEach((language) => {
|
2022-07-07 23:42:38 +00:00
|
|
|
if (language.attributes?.code === code)
|
2022-03-12 17:12:02 +00:00
|
|
|
result = language.attributes.localized_name;
|
|
|
|
});
|
|
|
|
return result;
|
2022-07-03 12:34:00 +00:00
|
|
|
};
|
2022-02-19 01:14:16 +00:00
|
|
|
|
2022-07-03 12:34:00 +00:00
|
|
|
export const prettyURL = (url: string): string => {
|
2022-07-07 23:42:38 +00:00
|
|
|
const domain = new URL(url);
|
2022-04-04 14:19:17 +00:00
|
|
|
return domain.hostname.replace("www.", "");
|
2022-07-03 12:34:00 +00:00
|
|
|
};
|
2022-04-04 14:19:17 +00:00
|
|
|
|
2022-07-03 12:34:00 +00:00
|
|
|
const capitalizeString = (string: string): string => {
|
2022-07-07 23:42:38 +00:00
|
|
|
const capitalizeWord = (word: string): string =>
|
|
|
|
word.charAt(0).toUpperCase() + word.substring(1);
|
2022-02-08 04:52:22 +00:00
|
|
|
|
|
|
|
let words = string.split(" ");
|
2022-03-27 15:01:14 +00:00
|
|
|
words = words.map((word) => capitalizeWord(word));
|
2022-02-07 04:03:51 +00:00
|
|
|
return words.join(" ");
|
2022-07-03 12:34:00 +00:00
|
|
|
};
|
2022-02-07 04:03:51 +00:00
|
|
|
|
2022-07-03 12:34:00 +00:00
|
|
|
export const slugify = (string: string | undefined): string => {
|
2022-03-27 15:01:14 +00:00
|
|
|
if (!string) {
|
|
|
|
return "";
|
|
|
|
}
|
2022-03-19 16:35:17 +00:00
|
|
|
return string
|
2022-07-07 23:42:38 +00:00
|
|
|
.replace(/[ÀÁÂÃÄÅàáâãäåæÆ]/gu, "a")
|
2022-03-28 11:17:30 +00:00
|
|
|
.replace(/[çÇ]/gu, "c")
|
|
|
|
.replace(/[ðÐ]/gu, "d")
|
|
|
|
.replace(/[ÈÉÊËéèêë]/gu, "e")
|
|
|
|
.replace(/[ÏïÎîÍíÌì]/gu, "i")
|
|
|
|
.replace(/[Ññ]/gu, "n")
|
|
|
|
.replace(/[øØœŒÕõÔôÓóÒò]/gu, "o")
|
|
|
|
.replace(/[ÜüÛûÚúÙù]/gu, "u")
|
|
|
|
.replace(/[ŸÿÝý]/gu, "y")
|
|
|
|
.toLowerCase()
|
|
|
|
.replace(/[^a-z0-9- ]/gu, "")
|
2022-03-12 12:27:21 +00:00
|
|
|
.trim()
|
2022-03-28 11:17:30 +00:00
|
|
|
.replace(/ /gu, "-");
|
2022-07-03 12:34:00 +00:00
|
|
|
};
|