import { DateTime, Duration } from 'luxon'; export class Format { public static spaceCamelCase(str: string): string { return str.replace(RegExp('/([A-Z])/g'), ' $1').trim(); }; public static sortAlphabetic(a: string, b: string) { if (a > b) { return 1; } if (a < b) { return -1; } return 0; }; public static secondsToHMS(seconds: number) : string { // calculate Hours and Minutes let minutes = Math.floor(seconds / 60); let hours = Math.floor(minutes / 60); minutes = minutes % 60; seconds = seconds % 60; // Format for display let hourText = hours + ' hour' + (hours == 1 ? '' : 's') ; return `${hours > 0 ? hourText : ''} ${minutes} minute${minutes == 1 ? '' : 's'} ${seconds} second${seconds == 1 ? '' : 's'}`; } public static secondsToErlangs(seconds: number) : string { let erlangs = seconds / 3600; return erlangs.toFixed(2); } public static displayBandwidth(value: number): string { let units = ['kbps', 'mbps', 'gbps']; let index = 0; while(value > 1000 && index < units.length) { value /= 1000; index ++; } let displayValue = value.toString(); if(value - Math.floor(value) > 0) { displayValue = value.toFixed(1); } return `${displayValue} ${units[index]}`; } public static displayDateRanges(dates: string[], interval = 1, intervalUnits = 'hours'): string { const recentDate = DateTime.fromISO(dates.pop()) const duration = Duration.fromObject({ [intervalUnits]: interval }); return `${recentDate.toFormat('MMM D h:mma')} - ${recentDate.plus(duration).toFormat('h:mma')}`; } public static displayDate(dateString: string): string { let date = DateTime.fromISO(dateString); return date.toFormat("MMM D h:mma"); } }