2024-12-09 16:09:42 +00:00
|
|
|
import { format } from "date-fns";
|
|
|
|
|
import { id, tr } from "date-fns/locale";
|
|
|
|
|
|
2024-12-04 14:12:10 +00:00
|
|
|
export const generateLocalizedPath = (href: string, locale: string): string => {
|
|
|
|
|
if (href.startsWith(`/${locale}`)) {
|
|
|
|
|
return href;
|
|
|
|
|
}
|
|
|
|
|
return `/${locale}${href}`;
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-03 21:18:22 +00:00
|
|
|
export function textEllipsis(
|
|
|
|
|
str: string,
|
|
|
|
|
maxLength: number,
|
|
|
|
|
{ side = "end", ellipsis = "..." } = {}
|
|
|
|
|
) {
|
2024-12-04 14:12:10 +00:00
|
|
|
if (str !== undefined && str?.length > maxLength) {
|
|
|
|
|
switch (side) {
|
|
|
|
|
case "start":
|
|
|
|
|
return ellipsis + str.slice(-(maxLength - ellipsis.length));
|
|
|
|
|
case "end":
|
|
|
|
|
default:
|
|
|
|
|
return str.slice(0, maxLength - ellipsis.length) + ellipsis;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return str;
|
2024-12-24 18:17:21 +00:00
|
|
|
}
|
2024-12-09 16:09:42 +00:00
|
|
|
|
|
|
|
|
export function formatDateToIndonesian(d: Date) {
|
|
|
|
|
try {
|
|
|
|
|
const dateString = format(d, "d MMMM yyyy HH:mm", { locale: id });
|
|
|
|
|
return dateString;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
2024-12-04 14:12:10 +00:00
|
|
|
}
|
2024-12-24 18:17:21 +00:00
|
|
|
|
|
|
|
|
export function htmlToString(str: string) {
|
|
|
|
|
if (str == undefined || str == null) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
str
|
|
|
|
|
.replaceAll(/<style[^>]*>.*<\/style>/gm, "")
|
|
|
|
|
// Remove script tags and content
|
|
|
|
|
.replaceAll(/<script[^>]*>.*<\/script>/gm, "")
|
|
|
|
|
// Replace  ,&ndash
|
|
|
|
|
.replaceAll(" ", "")
|
|
|
|
|
.replaceAll("–", "-")
|
|
|
|
|
// Replace quotation mark
|
|
|
|
|
.replaceAll("“", '"')
|
|
|
|
|
.replaceAll("”", '"')
|
|
|
|
|
// Remove all opening, closing and orphan HTML tags
|
|
|
|
|
.replaceAll(/<[^>]+>/gm, "")
|
|
|
|
|
// Remove leading spaces and repeated CR/LF
|
|
|
|
|
.replaceAll(/([\n\r]+ +)+/gm, "")
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getOnlyDate(date: Date) {
|
|
|
|
|
const year = date.getFullYear();
|
|
|
|
|
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
|
|
|
const day = String(date.getDate()).padStart(2, "0");
|
|
|
|
|
|
|
|
|
|
return `${year}-${month}-${day}`;
|
|
|
|
|
}
|
2024-12-26 15:03:45 +00:00
|
|
|
|
|
|
|
|
export function getOnlyMonthAndYear(d: Date) {
|
|
|
|
|
const pad = (n: any, s = 2) => `${new Array(s).fill(0)}${n}`.slice(-s);
|
|
|
|
|
return `${pad(d.getMonth() + 1)}/${pad(d.getFullYear(), 4)}`;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-31 08:25:40 +00:00
|
|
|
export function getPublicLocaleTimestamp(d: any) {
|
|
|
|
|
const pad = (n: any, s = 2) => `${new Array(s).fill(0)}${n}`.slice(-s);
|
2025-01-03 21:18:22 +00:00
|
|
|
return `${pad(d.getDate())}/${pad(d.getMonth() + 1)}/${pad(
|
|
|
|
|
d.getFullYear(),
|
|
|
|
|
4
|
|
|
|
|
)} ${pad(d.getHours())}:${pad(d.getMinutes())}`;
|
2024-12-31 08:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function capitalize(s: any) {
|
|
|
|
|
// return s[0].toUpperCase() + s.slice(1);
|
|
|
|
|
const splitStr = s.toLowerCase().split(" ");
|
|
|
|
|
for (let i = 0; i < splitStr.length; i++) {
|
|
|
|
|
splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].slice(1);
|
|
|
|
|
}
|
|
|
|
|
return splitStr.join(" ");
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-03 21:18:22 +00:00
|
|
|
export function getLocaleTimestamp(d: Date): string {
|
|
|
|
|
const pad = (n: number, s: number = 2): string =>
|
|
|
|
|
`${new Array(s).fill(0)}${n}`.slice(-s);
|
|
|
|
|
|
|
|
|
|
return `${pad(d.getDate())}-${pad(d.getMonth() + 1)}-${pad(
|
|
|
|
|
d.getFullYear(),
|
|
|
|
|
4
|
|
|
|
|
)} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getLocaleTime(d: Date) {
|
|
|
|
|
const pad = (n: number, s = 2) => `${new Array(s).fill(0)}${n}`.slice(-s);
|
|
|
|
|
return `${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
|
|
|
|
|
}
|