1 | import { htmlToText } from "html-to-text";
|
2 | import { Item } from "./item";
|
3 | import { ItemType } from "./item-type";
|
4 | import { RawItem } from "./raw-item";
|
5 | import { unixToIsoString } from "./unix-to-iso-string";
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | export function normalizeRawItem({ rawItem }: { rawItem: RawItem }): Item {
|
11 |
|
12 | const type: ItemType = rawItem.type ?? "unknown";
|
13 |
|
14 |
|
15 | const timestamp = rawItem.time
|
16 | ? unixToIsoString({ time: rawItem.time })
|
17 | : undefined;
|
18 |
|
19 |
|
20 | const deleted = rawItem.deleted ?? false;
|
21 | const dead = rawItem.dead ?? false;
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | const title = rawItem.title
|
27 | ? htmlToText(rawItem.title, { wordwrap: false })
|
28 | : undefined;
|
29 | const text = rawItem.text
|
30 | ? htmlToText(rawItem.text, { wordwrap: false })
|
31 | : undefined;
|
32 |
|
33 | return {
|
34 | id: rawItem.id,
|
35 | type,
|
36 | author: rawItem.by,
|
37 | title,
|
38 | url: rawItem.url,
|
39 | text,
|
40 | timestamp,
|
41 | score: rawItem.score,
|
42 | numChildren: rawItem.descendants,
|
43 | deleted,
|
44 | dead,
|
45 | parentId: rawItem.parent,
|
46 | childrenIds: rawItem.kids,
|
47 | pollId: rawItem.poll,
|
48 | pollOptionsIds: rawItem.parts,
|
49 | };
|
50 | }
|