| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- export const readableDate = (s: string) => {
- const ts = new Date(s);
- const date = ts.toLocaleDateString();
- const time = ts.toLocaleTimeString([], {
- hour: "numeric",
- minute: "2-digit",
- });
- return `${time} on ${date}`;
- };
- export const relativeDate = (date: string | number) => {
- if (!date) {
- return "N/A";
- }
- // @ts-ignore
- const rtf = new Intl.RelativeTimeFormat("en", {
- localeMatcher: "best fit", // other values: "lookup"
- numeric: "auto", // other values: "auto"
- style: "long", // other values: "short" or "narrow"
- });
- const time = timeFrom(date);
- if (!time) {
- return "N/A";
- }
- return rtf.format(-time.time, time.unitOfTime);
- };
- export const timeFrom = (
- time: string | number,
- secondTime?: string | number
- ) => {
- // Get timestamps
- let unixTime = new Date(time).getTime();
- if (!unixTime) return;
- let now = new Date().getTime();
- if (secondTime) {
- now = new Date(secondTime).getTime();
- }
- // Calculate difference
- let difference = unixTime / 1000 - now / 1000;
- // Setup return object
- let tfn: any = {};
- // Check if time is in the past, present, or future
- tfn.when = "now";
- if (difference > 0) {
- tfn.when = "future";
- } else if (difference < -1) {
- tfn.when = "past";
- }
- // Convert difference to absolute
- difference = Math.abs(difference);
- // Calculate time unit
- if (difference / (60 * 60 * 24 * 365) > 1) {
- // Years
- tfn.unitOfTime = "years";
- tfn.time = Math.floor(difference / (60 * 60 * 24 * 365));
- } else if (difference / (60 * 60 * 24 * 45) > 1) {
- // Months
- tfn.unitOfTime = "months";
- tfn.time = Math.floor(difference / (60 * 60 * 24 * 45));
- } else if (difference / (60 * 60 * 24) > 1) {
- // Days
- tfn.unitOfTime = "days";
- tfn.time = Math.floor(difference / (60 * 60 * 24));
- } else if (difference / (60 * 60) > 1) {
- // Hours
- tfn.unitOfTime = "hours";
- tfn.time = Math.floor(difference / (60 * 60));
- } else if (difference / 60 > 1) {
- // Minutes
- tfn.unitOfTime = "minutes";
- tfn.time = Math.floor(difference / 60);
- } else {
- // Seconds
- tfn.unitOfTime = "seconds";
- tfn.time = Math.floor(difference);
- }
- // Return time from now data
- return tfn;
- };
- export const capitalize = (s: string) => {
- if (!s) {
- return "";
- } else if (s.length == 0) {
- return s;
- } else if (s.length == 1) {
- return s.charAt(0).toUpperCase();
- }
- return s.charAt(0).toUpperCase() + s.substring(1).toLowerCase();
- };
- const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/gm;
- export const dotenv_parse = (src: string): Record<string, string> => {
- // Parser src into an Object
- const obj = {} as Record<string, string>;
- // Convert buffer to string
- let lines = src.toString();
- // Convert line breaks to same format
- lines = lines.replace(/\r\n?/gm, "\n");
- let match;
- while ((match = LINE.exec(lines)) != null) {
- const key = match[1];
- // Default undefined or null to empty string
- let value = match[2] || "";
- // Remove whitespace
- value = value.trim();
- // Check if double quoted
- const maybeQuote = value[0];
- // Remove surrounding quotes
- value = value.replace(/^(['"`])([\s\S]*)\1$/gm, "$2");
- // Expand newlines if double quoted
- if (maybeQuote === '"') {
- value = value.replace(/\\n/g, "\n");
- value = value.replace(/\\r/g, "\r");
- }
- // Add to object
- obj[key] = value;
- }
- return obj;
- };
|