bliss/api/src/lib/utils.ts

84 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-12-15 07:34:09 +00:00
import { randomBytes } from "crypto";
import { lookup } from "mime-types";
2022-12-21 09:56:45 +00:00
import { FieldError } from "./types";
2022-12-15 07:34:09 +00:00
export const toFieldError = (errors: string[]) => {
const fieldErrors: FieldError[] = [];
2022-12-21 09:56:45 +00:00
const special = errors.map((error) => error.includes("_")).includes(true);
2022-12-15 07:34:09 +00:00
errors.map((error) => {
2022-12-21 09:56:45 +00:00
if (special) {
const field = error.split(" ").shift();
const message = error.replace(field, "").trim();
fieldErrors.push({ field, message });
} else {
const [field, message] = error.split(": ");
fieldErrors.push({ field, message });
}
2022-12-15 07:34:09 +00:00
});
return fieldErrors;
};
2022-12-25 07:45:18 +00:00
export const generateRandomString = (len = 32) => {
2022-12-15 07:34:09 +00:00
return randomBytes(20).toString("hex").substring(0, len);
};
export const serialize = (s: string) => {
const url = s.replace(/(^\w+:|^)\/\//, "");
const domain = url.split(".").slice(-2).join(".");
return domain.replace(/\/$/, "");
};
export const formatBytes = (bytes: number, decimals = 2) => {
if (bytes === 0) return "0 Bytes";
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return parseFloat(
(bytes / Math.pow(1024, i)).toFixed(decimals < 0 ? 0 : decimals)
)
.toString()
.concat(` ${sizes[i]}`);
};
export const padTo2Digits = (num: number) => {
return num.toString().padStart(2, "0");
};
export const formatDate = (date: Date) => {
return (
[
padTo2Digits(date.getMonth() + 1),
padTo2Digits(date.getDate()),
date.getFullYear(),
].join("/") +
" " +
[
padTo2Digits(date.getHours()),
padTo2Digits(date.getMinutes()),
padTo2Digits(date.getSeconds()),
].join(":")
);
};
export const matchLocalHost = (ip: string) => {
const ipRegex = /(?:\d{1,3}\.){3}\d{1,3}/;
if (ip === "::1") return true;
return ip.match(ipRegex);
};
export const lookUp = (filename: string) => {
const type = lookup(filename);
if (type === false) {
return "application/octet-stream";
} else {
return type;
}
};
2022-12-16 15:48:04 +00:00
export const generateRandomHexColor = () =>
"#" + Math.floor(Math.random() * 16777215).toString(16);