-
-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathblog-format.ts
More file actions
70 lines (57 loc) · 1.64 KB
/
Copy pathblog-format.ts
File metadata and controls
70 lines (57 loc) · 1.64 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { findLibrary, type LibrarySlim } from '~/libraries'
const listJoiner = new Intl.ListFormat('en-US', {
style: 'long',
type: 'conjunction',
})
export function formatAuthors(authors: Array<string>) {
if (!authors.length) {
return 'TanStack'
}
return listJoiner.format(authors)
}
function getUtcDateString(date = new Date()) {
return date.toISOString().slice(0, 10)
}
function parsePublishedDate(published: string) {
const [year, month, day] = published.split('-').map(Number)
return new Date(Date.UTC(year, month - 1, day, 12))
}
export function formatPublishedDate(published: string) {
return parsePublishedDate(published).toLocaleDateString('en-US', {
timeZone: 'UTC',
year: 'numeric',
month: 'short',
day: 'numeric',
})
}
export function isPublishedDateReleased(published: string, now = new Date()) {
return published <= getUtcDateString(now)
}
export function publishedDateToUTCString(published: string) {
return parsePublishedDate(published).toUTCString()
}
function isLibrarySlim(
library: LibrarySlim | undefined,
): library is LibrarySlim {
return library !== undefined
}
export function getBlogLibraries(library: string | undefined): LibrarySlim[] {
if (!library) {
return []
}
return library
.split(',')
.map((libraryId) => findLibrary(libraryId.trim()))
.filter(isLibrarySlim)
}
export function getDistinctAuthors(
posts: ReadonlyArray<{ authors: string[] }>,
): string[] {
const authors = new Set<string>()
for (const post of posts) {
for (const author of post.authors) {
authors.add(author)
}
}
return [...authors].sort((a, b) => a.localeCompare(b))
}