WIP: A simple cli for daily tangled use cases and AI integration. This is for my personal use right now, but happy if others get mileage from it! :)
at pull-request-format-testing 71 lines 2.1 kB view raw
1/** 2 * Format a date for display with human-readable relative time 3 * @param dateString - ISO date string 4 * @returns Human-readable date string 5 */ 6export function formatDate(dateString: string): string { 7 const date = new Date(dateString); 8 const now = new Date(); 9 const diff = now.getTime() - date.getTime(); 10 const days = Math.floor(diff / (1000 * 60 * 60 * 24)); 11 12 if (days === 0) return 'today'; 13 if (days === 1) return 'yesterday'; 14 if (days < 7) return `${days} days ago`; 15 if (days < 30) return `${Math.floor(days / 7)} weeks ago`; 16 if (days < 365) return `${Math.floor(days / 30)} months ago`; 17 return date.toLocaleDateString(); 18} 19 20/** 21 * Format issue state for display 22 * @param state - Issue state ('open' or 'closed') 23 * @returns Formatted state badge string 24 */ 25export function formatIssueState(state: 'open' | 'closed'): string { 26 return state === 'open' ? '[OPEN]' : '[CLOSED]'; 27} 28 29/** 30 * Pick specific fields from an object, omitting fields not present in the object 31 */ 32function pickFields(obj: Record<string, unknown>, fields: string[]): Record<string, unknown> { 33 const result: Record<string, unknown> = {}; 34 for (const field of fields) { 35 if (field in obj) { 36 result[field] = obj[field]; 37 } 38 } 39 return result; 40} 41 42/** 43 * Output data as JSON to stdout, following GitHub CLI conventions. 44 * 45 * @param data - The data to output (object or array of objects) 46 * @param fields - Comma-separated field names to include; omit for all fields 47 */ 48export function outputJson( 49 data: Record<string, unknown> | Record<string, unknown>[], 50 fields?: string 51): void { 52 if (fields) { 53 const fieldList = fields 54 .split(',') 55 .map((f) => f.trim()) 56 .filter(Boolean); 57 if (Array.isArray(data)) { 58 console.log( 59 JSON.stringify( 60 data.map((item) => pickFields(item, fieldList)), 61 null, 62 2 63 ) 64 ); 65 } else { 66 console.log(JSON.stringify(pickFields(data, fieldList), null, 2)); 67 } 68 } else { 69 console.log(JSON.stringify(data, null, 2)); 70 } 71}