@@ -202,19 +202,17 @@ export function formatAnalystEstimates(data: unknown): string {
202202export function formatEarnings ( data : unknown ) : string {
203203 const d = ( data && typeof data === 'object' ) ? data as Rec : { } ;
204204 if ( Object . keys ( d ) . length === 0 ) return 'No earnings data available.' ;
205- const filings = Array . isArray ( d . filings ) ? d . filings as Rec [ ] : [ ] ;
206- if ( filings . length === 0 ) return 'No earnings data available.' ;
207- // Prefer the 8-K announcement (has surprise/estimate data); fall back to first filing.
208- const primary = filings . find ( ( f ) => f ?. source_type === '8-K' ) ?? filings [ 0 ] ;
209- const figures = ( ( primary . quarterly ?? primary . annual ) && typeof ( primary . quarterly ?? primary . annual ) === 'object' )
210- ? ( primary . quarterly ?? primary . annual ) as Rec
205+ // Flat shape: each entry IS one filing. data.earnings[0] (already unwrapped upstream)
206+ // lands on the most recent period's 8-K when present (sorted report_period DESC, filing_date ASC).
207+ const figures = ( ( d . quarterly ?? d . annual ) && typeof ( d . quarterly ?? d . annual ) === 'object' )
208+ ? ( d . quarterly ?? d . annual ) as Rec
211209 : { } ;
212210 const ticker = ( d . ticker as string ) ?. toUpperCase ( ) ?? '' ;
213211 const lines : string [ ] = [ ] ;
214212 const header = `${ ticker } Earnings — ${ fmtDate ( d . report_period ) } ${ d . fiscal_period ? ` (${ d . fiscal_period } )` : '' } ${ d . currency ? ` [${ d . currency } ]` : '' } ` ;
215213 lines . push ( header . trim ( ) ) ;
216214 lines . push ( '' ) ;
217- lines . push ( `Source: ${ primary . source_type ?? '—' } | Filed: ${ String ( primary . filing_date ?? '—' ) . slice ( 0 , 10 ) } | Accession: ${ primary . accession_number ?? '—' } ` ) ;
215+ lines . push ( `Source: ${ d . source_type ?? '—' } | Filed: ${ String ( d . filing_date ?? '—' ) . slice ( 0 , 10 ) } | Accession: ${ d . accession_number ?? '—' } ` ) ;
218216 if ( figures . revenue !== undefined ) lines . push ( `Revenue: ${ fmtNum ( figures . revenue ) } ` ) ;
219217 if ( figures . net_income !== undefined ) lines . push ( `Net Income: ${ fmtNum ( figures . net_income ) } ` ) ;
220218 const eps = figures . earnings_per_share ?? figures . eps ;
0 commit comments