-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-flat-country-data.ts
42 lines (34 loc) · 1.17 KB
/
extract-flat-country-data.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
import {
DOMParser,
Element,
} from "https://deno.land/x/[email protected]/deno-dom-wasm.ts";
export interface TableData {
status?: string;
country?: string;
changes: string | null;
}
export const extractFlatCountyData = (html: string): TableData[] | null => {
const doc = new DOMParser().parseFromString(html, "text/html");
if (!doc) return null;
const tables = doc.querySelectorAll("table") as unknown as Element[];
const data = [...tables]
.map((table) => {
if (!table) return null;
const status = table
.querySelector("thead th:first-child")
?.textContent.split(" ")[0]
.toLowerCase();
const tableBodyChildren = table.querySelector("tbody")?.children;
let countryChanges;
if (tableBodyChildren) {
countryChanges = [...tableBodyChildren].map((el) => {
const country = el.querySelector("th")?.textContent.trim();
const changes = el.querySelector("td")?.textContent.trim() || null;
return { status, country, changes };
});
}
return countryChanges ?? null;
})
.filter((item) => item !== null);
return (data as unknown as TableData[]).flat();
};