accords-library.com/src/helpers/formatters.ts

289 lines
8.4 KiB
TypeScript
Raw Normal View History

2022-05-08 10:26:36 +00:00
import { AppStaticProps } from "../graphql/getAppStaticProps";
import { convertPrice } from "./numbers";
2022-07-07 23:42:38 +00:00
import { isDefinedAndNotEmpty } from "./others";
import { DatePickerFragment, PricePickerFragment } from "graphql/generated";
export const prettyDate = (datePicker: DatePickerFragment): string => {
let result = "";
if (datePicker.year) result += datePicker.year.toString();
if (datePicker.month)
result += `/${datePicker.month.toString().padStart(2, "0")}`;
if (datePicker.day)
result += `/${datePicker.day.toString().padStart(2, "0")}`;
return result;
};
export const prettyPrice = (
2022-06-18 02:39:18 +00:00
pricePicker: PricePickerFragment,
currencies: AppStaticProps["currencies"],
targetCurrencyCode?: string
): string => {
if (!targetCurrencyCode) return "";
let result = "";
currencies.map((currency) => {
2022-07-07 23:42:38 +00:00
if (currency.attributes?.code === targetCurrencyCode) {
const amountInTargetCurrency = convertPrice(pricePicker, currency);
result =
currency.attributes.symbol +
amountInTargetCurrency.toLocaleString(undefined, {
minimumFractionDigits: currency.attributes.display_decimals ? 2 : 0,
maximumFractionDigits: currency.attributes.display_decimals ? 2 : 0,
});
}
});
return result;
};
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-02-08 04:52:22 +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
): string => {
2022-02-08 08:44:17 +00:00
let result = "";
if (pretitle) result += `${pretitle}: `;
2022-02-08 08:44:17 +00:00
result += title;
if (subtitle) result += ` - ${subtitle}`;
2022-02-08 08:44:17 +00:00
return result;
};
2022-02-08 08:44:17 +00:00
export const prettyItemType = (
2022-06-18 02:39:18 +00:00
metadata: any,
langui: AppStaticProps["langui"]
2022-07-07 23:42:38 +00:00
): string | null | undefined => {
switch (metadata.__typename) {
case "ComponentMetadataAudio":
return langui.audio;
case "ComponentMetadataBooks":
return langui.textual;
case "ComponentMetadataGame":
return langui.game;
case "ComponentMetadataVideo":
return langui.video;
case "ComponentMetadataGroup":
return langui.group;
case "ComponentMetadataOther":
return langui.other;
default:
return "";
}
};
2022-07-07 23:42:38 +00:00
/* eslint-disable id-denylist */
export const prettyItemSubType = (
2022-06-18 02:39:18 +00:00
metadata:
| {
__typename: "ComponentMetadataAudio";
subtype?: {
data?: {
attributes?: {
slug: string;
2022-07-07 23:42:38 +00:00
titles?:
| ({
title: string;
} | null)[]
| null;
} | null;
} | null;
} | null;
}
| {
__typename: "ComponentMetadataBooks";
subtype?: {
data?: {
attributes?: {
slug: string;
2022-07-07 23:42:38 +00:00
titles?:
| ({
title: string;
} | null)[]
| null;
} | null;
} | null;
} | null;
}
| {
__typename: "ComponentMetadataGame";
platforms?: {
2022-07-07 23:42:38 +00:00
data: {
id?: string | null;
attributes?: {
short: string;
} | null;
2022-07-07 23:42:38 +00:00
}[];
} | null;
}
| {
__typename: "ComponentMetadataGroup";
subtype?: {
data?: {
attributes?: {
slug: string;
2022-07-07 23:42:38 +00:00
titles?:
| ({
title: string;
} | null)[]
| null;
} | null;
} | null;
} | null;
subitems_type?: {
data?: {
attributes?: {
slug: string;
2022-07-07 23:42:38 +00:00
titles?:
| ({
title: string;
} | null)[]
| null;
} | null;
} | null;
} | null;
}
| {
__typename: "ComponentMetadataVideo";
subtype?: {
data?: {
attributes?: {
slug: string;
2022-07-07 23:42:38 +00:00
titles?:
| ({
title: string;
} | null)[]
| null;
} | null;
} | null;
} | null;
}
2022-07-07 23:42:38 +00:00
| { __typename: "ComponentMetadataOther" }
| { __typename: "Error" }
| null
): string => {
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 &&
metadata.subtype.data.attributes.titles[0]
? metadata.subtype.data.attributes.titles[0].title
: 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 &&
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 &&
metadata.subtype.data.attributes.titles[0]
? metadata.subtype.data.attributes.titles[0].title
: prettySlug(metadata.subtype?.data?.attributes?.slug);
const secondPart =
metadata.subitems_type?.data?.attributes?.titles &&
2022-07-07 23:42:38 +00:00
metadata.subitems_type.data.attributes.titles.length > 0 &&
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 "";
}
}
return "";
};
2022-07-07 23:42:38 +00:00
/* eslint-enable id-denylist */
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-04-01 18:47:42 +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-04-01 18:47:42 +00:00
export const prettyLanguage = (
2022-03-12 17:12:02 +00:00
code: string,
languages: AppStaticProps["languages"]
): 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;
};
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-04-04 14:19:17 +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(" ");
words = words.map((word) => capitalizeWord(word));
return words.join(" ");
};
export const slugify = (string: string | undefined): string => {
if (!string) {
return "";
}
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, "")
.trim()
2022-03-28 11:17:30 +00:00
.replace(/ /gu, "-");
};