-
Notifications
You must be signed in to change notification settings - Fork 1
/
transform.ts
77 lines (64 loc) · 2.34 KB
/
transform.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { RawReview, Extract, Review, ReviewTimeline } from '../types';
export function indexBy<T, K extends string | number>(
items: Array<T>,
getIndex: (item: T) => K
): Record<K, T> {
return items.reduce((acc, item) => ({ ...acc, [getIndex(item)]: item }), {} as Record<K, T>);
}
export const unique = <T, K>(items: T[], getIndex: (item: T) => K) => [
...new Set(items.map(getIndex)),
];
export const mostRecentlyStarted = (
a: { timeline: { started: string | null; shelved: string } },
b: { timeline: { started: string | null; shelved: string } }
) => {
// If neither of the books have been started, compare their shelved dates
if (!a.timeline.started && !b.timeline.started) {
return b.timeline.shelved.localeCompare(a.timeline.shelved);
}
// Otherwise, started books always appear before "un"started books
if (!a.timeline.started && b.timeline.started) {
return 1;
}
if (a.timeline.started && !b.timeline.started) {
return -1;
}
// Started books are ordered by most-recently-started first
return b.timeline.started!.localeCompare(a.timeline.started!);
};
export const mostRecentlyFinished = (
a: { timeline: { finished: string | null; shelved: string } },
b: { timeline: { finished: string | null; shelved: string } }
) => {
// If neither of the books have been finished, compare their shelved dates
if (!a.timeline.finished && !b.timeline.finished) {
return b.timeline.shelved.localeCompare(a.timeline.shelved);
}
// Otherwise, finished books always appear before unfinished books
if (!a.timeline.finished && b.timeline.finished) {
return 1;
}
if (a.timeline.finished && !b.timeline.finished) {
return -1;
}
// finished books are ordered by most-recently-finished first
return b.timeline.finished!.localeCompare(a.timeline.finished!);
};
export const reviewsFromExtract = (extract: Extract): Review[] => {
return extract.reviews.map((review) => {
const book = { ...extract.booksByUrl[review.bookUrl] } as any;
book.author = extract.authorsByUrl[book.authorUrl];
delete book.authorUrl;
if (book.series) {
book.series = extract.seriesByUrl[book.series.url];
}
const fullReview = { ...review, book } as {
book: any;
reviewId: number;
bookUrl?: string;
timeline: ReviewTimeline;
};
delete fullReview.bookUrl;
return fullReview;
});
};