Repository navigation
Expand file tree
/
Copy pathconvert-csv.js
More file actions
111 lines (91 loc) · 2.58 KB
/
Copy pathconvert-csv.js
File metadata and controls
111 lines (91 loc) · 2.58 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import * as fs from "node:fs";
import { parse } from "csv-parse/sync";
try {
const data = fs.readFileSync(process.argv[2], "utf8");
const records = parse(data, { delimiter: ";", columns: true });
const contacts = getContacts(records);
const contactsJson = JSON.stringify(contacts, null, 2);
fs.writeFileSync(
"./src/data/generated/contacts.ts",
`export const contacts = ${contactsJson}`,
);
const branches = getBranches(contacts);
const branchesJson = JSON.stringify(branches, null, 2);
fs.writeFileSync(
"./src/data/generated/branches.ts",
`export const branches = ${branchesJson}`,
);
const ranges = getRanges(contacts);
const rangesJson = JSON.stringify(ranges, null, 2);
fs.writeFileSync(
"./src/data/generated/ranges.ts",
`export const ranges = ${rangesJson}`,
);
} catch (err) {
console.error(err);
}
function getContacts(records) {
const contacts = records
.map(toContact)
.filter(({ organisation }) => organisation !== "")
.filter(isDuplicate);
checkAndWarnForEmptyFields(contacts);
return contacts;
}
function toContact(record) {
return {
organisation: record["Organisation"],
branch: record["Branche"],
range: toShortName(record["Wirkungsgrad"]),
website: record["Website"],
tags: toTags(record["Tags"]),
description: record["Beschreibung (short description)"],
};
}
function isDuplicate({ organisation }, index, contacts) {
const indexOfFirstOccurrence = contacts.findIndex(
(contact) => contact.organisation === organisation,
);
return index === indexOfFirstOccurrence;
}
function checkAndWarnForEmptyFields(contacts) {
const relevantFields = ["branch", "range", "website", "description"];
contacts.forEach((contact) => {
const emptyFields = relevantFields.filter((field) => contact[field] === "");
if (emptyFields.length === 0) {
return;
}
console.warn(
`Warning: organisation "${contact.organisation}" has empty field(s): ${emptyFields.join(", ")}`,
);
});
}
function toShortName(recordElement) {
if (recordElement === "Deutschland") {
return "DE";
}
return recordElement;
}
function toTags(tags) {
return tags
.split(",")
.map((tag) => tag.trim())
.filter((tag) => tag !== "");
}
function getBranches(contacts) {
const sanitizedUniqueBranches = [
...new Set(contacts.map(({ branch }) => branch.trim())),
];
return sanitizedUniqueBranches.map((branch) => ({
name: branch,
description: "",
}));
}
function getRanges(contacts) {
const sanitizedUniqueRanges = [
...new Set(contacts.map(({ range }) => range.trim())),
];
return sanitizedUniqueRanges.map(toShortName).map((range) => ({
name: range,
}));
}