197 lines
5.8 KiB
TypeScript
197 lines
5.8 KiB
TypeScript
"use client";
|
|
|
|
import { format } from "date-fns";
|
|
import { id, tr } from "date-fns/locale";
|
|
import { useEffect } from "react";
|
|
|
|
export const generateLocalizedPath = (href: string, locale: string): string => {
|
|
if (href.startsWith(`/${locale}`)) {
|
|
return href;
|
|
}
|
|
return `/${locale}${href}`;
|
|
};
|
|
|
|
export function textEllipsis(
|
|
str: string,
|
|
maxLength: number,
|
|
{ side = "end", ellipsis = "..." } = {}
|
|
) {
|
|
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;
|
|
}
|
|
|
|
export function formatDateToIndonesian(d: Date) {
|
|
try {
|
|
const dateString = format(d, "d/MM/yy HH:mm", { locale: id });
|
|
return dateString;
|
|
} catch (error) {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
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}`;
|
|
}
|
|
|
|
export function getOnlyDateSlash(date: Date) {
|
|
const year = date.getFullYear();
|
|
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
const day = String(date.getDate()).padStart(2, "0");
|
|
|
|
return `${day}/${month}/${year}`;
|
|
}
|
|
|
|
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)}`;
|
|
}
|
|
|
|
export function getPublicLocaleTimestamp(d: any) {
|
|
const pad = (n: any, s = 2) => `${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())}`;
|
|
}
|
|
|
|
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(" ");
|
|
}
|
|
|
|
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())}`;
|
|
}
|
|
export function getTimestamp(d: Date) {
|
|
const pad = (n: any, s = 2) => `${new Array(s).fill(0)}${n}`.slice(-s);
|
|
return `${pad(d.getFullYear(), 4)}-${pad(d.getMonth() + 1)}-${pad(
|
|
d.getDate()
|
|
)} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
|
|
}
|
|
|
|
export function secondToTimes(sec: number) {
|
|
if (sec) {
|
|
const date = new Date(0);
|
|
date.setSeconds(sec); // specify value for SECONDS here
|
|
return date?.toISOString().slice(11, 19);
|
|
}
|
|
return "00:00:00";
|
|
}
|
|
|
|
export function checkMaliciousText(str: any) {
|
|
try {
|
|
const urlPattern = new RegExp(
|
|
"(https?:\\/\\/(?:www\\.|(?!www))[^\\s\\.]+\\.[^\\s]{2,}|www\\.[^\\s]+\\.[^\\s]{2,}|https?:\\/\\/[^\\s]+|\\b(?:https?|ftp):\\/\\/[^\\s/$.?#].[^\\s]*)",
|
|
"gi"
|
|
);
|
|
const isContainUrl = urlPattern.test(str);
|
|
if (isContainUrl) {
|
|
return "Message mengandung URL yang tidak diizinkan";
|
|
}
|
|
|
|
const emailPattern = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/i;
|
|
const isContainEmail = emailPattern.test(str);
|
|
if (isContainEmail) {
|
|
return "Message mengandung Email yang tidak diizinkan";
|
|
}
|
|
|
|
const phonePattern = /\b(?!\+?62|62|082[0-9])[2-9][0-9]{7,11}\b/g;
|
|
const isContainPhone = phonePattern.test(str);
|
|
if (isContainPhone) {
|
|
return "Message mengandung Nomor HP yang tidak diizinkan";
|
|
}
|
|
|
|
return "";
|
|
} catch (error) {
|
|
console.log(error);
|
|
return "";
|
|
}
|
|
}
|
|
|
|
export const shimmer = (w: number, h: number) => `
|
|
<svg width="${w}" height="${h}" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
<defs>
|
|
<linearGradient id="g">
|
|
<stop stop-color="#333" offset="20%" />
|
|
<stop stop-color="#222" offset="50%" />
|
|
<stop stop-color="#333" offset="70%" />
|
|
</linearGradient>
|
|
</defs>
|
|
<rect width="${w}" height="${h}" fill="#333" />
|
|
<rect id="r" width="${w}" height="${h}" fill="url(#g)" />
|
|
<animate xlink:href="#r" attributeName="x" from="-${w}" to="${w}" dur="1s" repeatCount="indefinite" />
|
|
</svg>`;
|
|
|
|
export const toBase64 = (str: string) =>
|
|
typeof window === "undefined"
|
|
? Buffer.from(str).toString("base64")
|
|
: window.btoa(str);
|
|
|
|
const LoadScript = () => {
|
|
useEffect(() => {
|
|
const script = document.createElement("script");
|
|
script.src = "https://cdn.userway.org/widget.js";
|
|
script.setAttribute("data-account", "X36s1DpjqB");
|
|
script.setAttribute("data-position", "5");
|
|
script.async = true;
|
|
document.head.appendChild(script);
|
|
|
|
return () => {
|
|
document.head.removeChild(script);
|
|
};
|
|
}, []);
|
|
|
|
return null;
|
|
};
|
|
|
|
export default LoadScript;
|