import rss from 'rss-parser' ; const placeholderRe = /(?<raw>\$\{(?<flag>\d*)(?<verb>.+?)\})/g ; export function formatFeeds ( feeds , format , startFlag , endFlag , locale , timezone ) { const fields = [ ... format . matchAll ( placeholderRe ) ] . map ( ( e ) => e . groups ) ; return [ startFlag , ... feeds . map ( ( feed ) => formatFeed ( feed , format , fields , locale , timezone ) ) , endFlag ] ; } function formatFeed ( feed , format , fields , locale , timezone ) { const feedDate = feed . isoDate ? new Date ( feed . isoDate ) : undefined ; const args = { title : feed . title || '(no title)' , url : feed . link , year : feedDate ?. toLocaleString ( locale , { timeZone : timezone , year : 'numeric' } ) , month : feedDate ?. toLocaleString ( locale , { timeZone : timezone , month : 'numeric' } ) , monthshort : feedDate ?. toLocaleString ( locale , { timeZone : timezone , month : 'short' } ) , monthlong : feedDate ?. toLocaleString ( locale , { timeZone : timezone , month : 'long' } ) , day : feedDate ?. toLocaleString ( locale , { timeZone : timezone , day : 'numeric' } ) , date : feedDate ?. toLocaleString ( locale , { timeZone : timezone } ) } ; return fields . reduce ( ( acc , cur ) => { const padstring = cur . flag . startsWith ( '0' ) ? '0' : ' ' ; const padlength = parseInt ( cur . flag ) ; const argraw = args [ cur . verb ] ; const arg = padlength ? argraw ?. padStart ( padlength , padstring ) : argraw ; return acc . replace ( cur . raw , arg || '' ) ; } , format ) ; }