178 lines
4.1 KiB
TypeScript
178 lines
4.1 KiB
TypeScript
import Cookies from "js-cookie";
|
|
import CryptoJS from "crypto-js";
|
|
|
|
export function convertDateFormat(dateString: string) {
|
|
var date = new Date(dateString);
|
|
|
|
var day = date.getDate();
|
|
var month = date.getMonth() + 1;
|
|
var year = date.getFullYear();
|
|
var hours = date.getHours();
|
|
var minutes = date.getMinutes();
|
|
|
|
var formattedTime =
|
|
(hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes;
|
|
var formattedDate =
|
|
(day < 10 ? "0" : "") +
|
|
day +
|
|
"-" +
|
|
(month < 10 ? "0" : "") +
|
|
month +
|
|
"-" +
|
|
year +
|
|
", " +
|
|
formattedTime;
|
|
|
|
return formattedDate;
|
|
}
|
|
export function convertDateFormatNoTime(dateString: any) {
|
|
var date = new Date(dateString);
|
|
|
|
var day = date.getDate();
|
|
var month = date.getMonth() + 1;
|
|
var year = date.getFullYear();
|
|
|
|
var formattedDate =
|
|
(day < 10 ? "0" : "") +
|
|
day +
|
|
"-" +
|
|
(month < 10 ? "0" : "") +
|
|
month +
|
|
"-" +
|
|
year;
|
|
|
|
return formattedDate;
|
|
}
|
|
|
|
export function convertDateFormatNoTimeV2(dateString: string | Date) {
|
|
var date = new Date(dateString);
|
|
|
|
var day = date.getDate();
|
|
var month = date.getMonth() + 1;
|
|
var year = date.getFullYear();
|
|
|
|
var formattedDate =
|
|
year +
|
|
"-" +
|
|
(month < 10 ? "0" : "") +
|
|
month +
|
|
"-" +
|
|
(day < 10 ? "0" : "") +
|
|
day;
|
|
|
|
return formattedDate;
|
|
}
|
|
|
|
export function formatTextToHtmlTag(text: string) {
|
|
if (text) {
|
|
const htmlText = text.replaceAll("\\n", "<br>").replaceAll(/"/g, "");
|
|
return { __html: htmlText };
|
|
}
|
|
}
|
|
|
|
export function delay(ms: number) {
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
}
|
|
|
|
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 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 formatMonthString(dateString: string) {
|
|
const months = [
|
|
"Januari",
|
|
"Februari",
|
|
"Maret",
|
|
"April",
|
|
"Mei",
|
|
"Juni",
|
|
"Juli",
|
|
"Agustus",
|
|
"September",
|
|
"Oktober",
|
|
"November",
|
|
"Desember",
|
|
];
|
|
|
|
const date = new Date(dateString); // Konversi string ke objek Date
|
|
const day = date.getDate(); // Ambil tanggal
|
|
const month = months[date.getMonth()]; // Ambil nama bulan
|
|
const year = date.getFullYear(); // Ambil tahun
|
|
|
|
return `${day} ${month} ${year}`;
|
|
}
|
|
|
|
export function getUnixTimestamp() {
|
|
const unixTimestampInSeconds: number = Math.floor(Date.now() / 1000);
|
|
return unixTimestampInSeconds;
|
|
}
|
|
|
|
export function setCookiesEncrypt(
|
|
param: string,
|
|
data: any,
|
|
options?: Cookies.CookieAttributes
|
|
) {
|
|
// Enkripsi data
|
|
const cookiesEncrypt = CryptoJS.AES.encrypt(
|
|
JSON.stringify(data),
|
|
`${param}_EncryptKey@humas`
|
|
).toString(); // Tambahkan .toString() di sini
|
|
|
|
// Simpan data terenkripsi di cookie
|
|
Cookies.set(param, cookiesEncrypt, options);
|
|
}
|
|
|
|
export function getCookiesDecrypt(param: any) {
|
|
const cookiesEncrypt = Cookies.get(param);
|
|
try {
|
|
if (cookiesEncrypt != undefined) {
|
|
const output = CryptoJS.AES.decrypt(
|
|
cookiesEncrypt.toString(),
|
|
`${param}_EncryptKey@humas`
|
|
).toString(CryptoJS.enc.Utf8);
|
|
if (output.startsWith('"')) {
|
|
return output.slice(1, -1);
|
|
}
|
|
return output;
|
|
}
|
|
} catch (e) {
|
|
//console.log("Error", cookiesEncrypt);
|
|
}
|
|
}
|