UNPKG

848 BPlain TextView Raw
1import { htmlToText } from "html-to-text";
2import { RawUser } from "./raw-user";
3import { unixToIsoString } from "./unix-to-iso-string";
4import { User } from "./user";
5
6/**
7 * `normalizeRawUser` transforms a `RawUser` into a `User`.
8 */
9export function normalizeRawUser({ rawUser }: { rawUser: RawUser }): User {
10 // Convert Unix time in second to UTC timestamp
11 const createdAt = unixToIsoString({ time: rawUser.created });
12
13 // Convert `about` from HTML to plain text;
14 // for example, see from API https://hacker-news.firebaseio.com/v0/user/velut.json
15 // and rendered https://news.ycombinator.com/user?id=velut
16 const about = rawUser.about
17 ? htmlToText(rawUser.about, { wordwrap: false })
18 : undefined;
19
20 return {
21 id: rawUser.id,
22 createdAt,
23 karma: rawUser.karma,
24 about,
25 submittedIds: rawUser.submitted,
26 };
27}