import { PaginatedResponse } from "./elements/table/Table"; import { AbstractIPNum, IPv4, IPv4CidrRange, IPv6, IPv6CidrRange } from "ip-num"; export interface KV { [key: string]: string; } export function PaginationWrapper(items: T[]): PaginatedResponse { return { pagination: { count: items.length, current: 1, totalPages: 1, startIndex: 1, endIndex: items.length, }, results: items, }; } export function firstElement(items: T[] | null | undefined): T | undefined { if (items !== undefined && items !== null && items.length > 0) { return items[0]; } return undefined; } export function first(...args: Array): T { for (let index = 0; index < args.length; index++) { const element = args[index]; if (element !== undefined && element !== null) { return element; } } throw new Error(`No compatible arg given: ${args}`); } export function ip(raw: string): AbstractIPNum & { getValue(): bigint } { try { return IPv6.fromString(raw); } catch { try { return IPv4.fromString(raw); } catch { return new IPv4(0); } } } export function sortByIP(getter: (item: T) => string): (a: T, b: T) => number { return (a: T, b: T) => { const aIP = ip(getter(a)); const bIP = ip(getter(b)); if (aIP.getValue() > bIP.getValue()) return 1; if (aIP.getValue() < bIP.getValue()) return -1; return 0; }; } /** * @file Temporal utilitie for working with dates and times. */ /** * Duration in milliseconds for time units used by the `Intl.RelativeTimeFormat` API. */ export const Duration = { /** * The number of milliseconds in a year. */ year: 1000 * 60 * 60 * 24 * 365, /** * The number of milliseconds in a month. */ month: (24 * 60 * 60 * 1000 * 365) / 12, /** * The number of milliseconds in a day. */ day: 1000 * 60 * 60 * 24, /** * The number of milliseconds in an hour. */ hour: 1000 * 60 * 60, /** * The number of milliseconds in a minute. */ minute: 1000 * 60, /** * The number of milliseconds in a second. */ second: 1000, } as const satisfies Partial>; export type DurationUnit = keyof typeof Duration; /** * The order of time units used by the `Intl.RelativeTimeFormat` API. */ const DurationGranularity = [ "year", "month", "day", "hour", "minute", "second", ] as const satisfies DurationUnit[]; /** * Given two dates, return a human-readable string describing the time elapsed between them. */ export function formatElapsedTime(d1: Date, d2: Date = new Date()): string { const elapsed = d1.getTime() - d2.getTime(); const rtf = new Intl.RelativeTimeFormat("default", { numeric: "auto" }); for (const unit of DurationGranularity) { const duration = Duration[unit]; if (Math.abs(elapsed) > duration || unit === "second") { let rounded = Math.round(elapsed / duration); if (!isFinite(rounded)) { rounded = 0; } return rtf.format(rounded, unit); } } return rtf.format(Math.round(elapsed / 1000), "second"); } export function subnetToDnsZone(subnet: IPv4CidrRange | IPv6CidrRange) { const prefix = Number(subnet.cidrPrefix.value); if (subnet instanceof IPv4CidrRange) { // IPv4 reverse zone const octets = subnet.getFirst().toString().split(".").map(Number); if (prefix % 8 !== 0) { throw new Error("IPv4 prefix must be a multiple of 8 for a zone name."); } const octetCount = prefix / 8; const reversed = octets.slice(0, octetCount).reverse().join("."); return `${reversed}.in-addr.arpa`; } else if (subnet instanceof IPv6CidrRange) { // IPv6 reverse zone const expanded = subnet.getPrefix().toString().replace(/:/g, ""); const nibbleCount = prefix / 4; if (prefix % 4 !== 0) { throw new Error("IPv6 prefix must be a multiple of 4 for a zone name."); } const nibbles = expanded.slice(0, nibbleCount).split("").reverse().join("."); return `${nibbles}.ip6.arpa`; } throw new Error("Invalid IP address format."); }