-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
format.ts
54 lines (45 loc) · 1.96 KB
/
format.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import rss from 'rss-parser'
interface MatchedPlaceholder {
raw: string
flag: string
verb: string
}
const placeholderRe = /(?<raw>\$\{(?<flag>\d*)(?<verb>.+?)\})/g
export function formatFeeds(
feeds: Array<rss.Item>, format: string, startFlag: string, endFlag: string,
locale: string, timezone: string
): Array<string> {
const fields = [...format.matchAll(placeholderRe)].map((e) => e.groups) as unknown as MatchedPlaceholder[]
return [
startFlag,
...feeds.map((feed) => formatFeed(feed, format, fields, locale, timezone)),
endFlag
]
}
function formatFeed(
feed: rss.Item, format: string, fields: MatchedPlaceholder[],
locale: string, timezone: string): string {
const feedDate = feed.isoDate ? new Date(feed.isoDate) : undefined
// TODO: more memory-friendly arg initialize
const args: { [k: string]: string | undefined } = {
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 }),
guid: feed.guid
}
return fields.reduce((acc: string, cur: MatchedPlaceholder) => {
// flag: 2 -> pad to length 2 with space (' ')
// flag:02 -> pad to length 2 with zero('0')
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)
}