-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform-data.js
57 lines (53 loc) · 1.61 KB
/
transform-data.js
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
import { writeFileSync } from 'fs';
export const exportToJson = (data, filename) => {
const content = JSON.stringify(data, null, 2);
writeFileSync(filename, content, 'utf8', (err) => {
if (err) throw err;
console.log(`${filename} has been saved.`);
});
};
export const exportToCsv = (data, filename) => {
const csvRows = [
'Name,Type,Address,Entity,Canton,Municipality,Latitude,Longitude,Phone,Email,Url,Owner'
]; // Header row
data.forEach(item => {
const row = [
item.Name,
item.Type,
item.Address,
item.Entity,
item.Canton,
item.Municipality,
item.Latitude,
item.Longitude,
item.Phone,
item.Email,
item.Url || item.WebAddress || '', // Adjust for Url or WebAddress
item.Owner
].map(field => `"${field}"`); // Enclose each field in quotes
csvRows.push(row.join(','));
});
const content = csvRows.join('\n');
writeFileSync(filename, content, 'utf8', (err) => {
if (err) throw err;
console.log(`${filename} has been saved.`);
});
};
export const replaceChars = (schools) => schools.map(school => {
const replaceChars = (str) => {
const replacements = {
'ć': 'c', 'č': 'c', 'ž': 'z', 'đ': 'dj', 'š': 's',
'Ć': 'C', 'Č': 'C', 'Ž': 'Z', 'Đ': 'Dj', 'Š': 'S'
};
return str.replace(/[ćčžđšĆČŽĐŠ]/g, match => replacements[match]);
};
let modifiedString = {};
for (let key in school) {
if (typeof school[key] === 'string') {
modifiedString[key] = replaceChars(school[key]);
} else {
modifiedString[key] = school[key];
}
}
return modifiedString;
});