98 lines
2.9 KiB
TypeScript
98 lines
2.9 KiB
TypeScript
export const createPlaceholders = ( arr: any[] ) => {
|
|
return arr.map(() => '?').join(', ');
|
|
}
|
|
|
|
export type AddDollarPrefix<T> = {
|
|
[K in keyof T as `$${string & K}`]: T[K];
|
|
};
|
|
|
|
export function prefixKeysWithDollar<T extends Record<string, any>>(obj: T): AddDollarPrefix<T> {
|
|
const result = {} as AddDollarPrefix<T>;
|
|
|
|
for (const key in obj) {
|
|
const newKey = `$${key}` as keyof AddDollarPrefix<T>;
|
|
result[newKey] = obj[key] as any;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
export function transformArray<T extends Record<string, any>>(arr: T[]): AddDollarPrefix<T>[] {
|
|
return arr.map(prefixKeysWithDollar);
|
|
}
|
|
|
|
export function pad_l2 ( _thing: string | number ): string {
|
|
if ( typeof _thing == "number" ) {
|
|
_thing = JSON.stringify(_thing);
|
|
};
|
|
return _thing.padStart(2, "0");
|
|
}
|
|
|
|
export function getTsNow() {
|
|
const now = new Date();
|
|
const rtn = {
|
|
year: now.getFullYear(),
|
|
month: now.getMonth() + 1,
|
|
day: now.getDate(),
|
|
minute: now.getMinutes(),
|
|
seconds: now.getSeconds()
|
|
}
|
|
return rtn;
|
|
}
|
|
|
|
export function unixToDate( unix_timestamp: number ) { return new Date(unix_timestamp * 1000) }
|
|
export function dateToUnix( date: Date ) { return Math.round( date.getTime()/1000 ) }
|
|
|
|
export function formatTimeDiff(dateA: Date, dateB: Date) {
|
|
// Difference in milliseconds
|
|
const diffMs = dateB.getTime() - dateA.getTime();
|
|
|
|
// Get sign (+ or -)
|
|
const sign = diffMs < 0 ? "-" : "";
|
|
|
|
// Convert to absolute minutes
|
|
const diffMinutes = Math.floor(Math.abs(diffMs) / 60000);
|
|
|
|
// Split into hours and minutes
|
|
const hours = Math.floor(diffMinutes / 60);
|
|
const minutes = diffMinutes % 60;
|
|
|
|
// Return formatted string
|
|
return `${sign}${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}`;
|
|
}
|
|
|
|
export function subtractHours(date: Date, hours: number) {
|
|
// Create a new Date so we don't mutate the original
|
|
return new Date(date.getTime() - hours * 60 * 60 * 1000);
|
|
}
|
|
|
|
// Helper: get last Sunday of a given month
|
|
function lastSundayOfMonth(year: number, month: number ) {
|
|
const lastDay = new Date(Date.UTC(year, month + 1, 0)); // last day of month
|
|
const day = lastDay.getUTCDay(); // 0 = Sunday
|
|
const diff = day === 0 ? 0 : day; // how far back to go to reach Sunday
|
|
lastDay.setUTCDate(lastDay.getUTCDate() - diff);
|
|
return lastDay;
|
|
}
|
|
|
|
export function isEuropeanDST( date: Date ) {
|
|
const year = date.getFullYear();
|
|
|
|
// DST starts: last Sunday in March, 01:00 UTC
|
|
const start = lastSundayOfMonth(year, 2); // March (month = 2)
|
|
start.setUTCHours(1, 0, 0, 0);
|
|
|
|
// DST ends: last Sunday in October, 01:00 UTC
|
|
const end = lastSundayOfMonth(year, 9); // October (month = 9)
|
|
end.setUTCHours(1, 0, 0, 0);
|
|
|
|
// Return true if within DST period
|
|
return date >= start && date < end;
|
|
}
|
|
|
|
export function createQS (params: Record<string, string | number | boolean>): string {
|
|
const queryString = Object.entries(params)
|
|
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
|
.join("&");
|
|
return queryString;
|
|
} |