Skip to content

Commit 807bc5b

Browse files
Initial code for tidying up the CSV
1 parent 4b79737 commit 807bc5b

4 files changed

Lines changed: 241 additions & 0 deletions

File tree

package-lock.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "valley-disco",
3+
"version": "1.0.0",
4+
"description": "",
5+
"license": "ISC",
6+
"author": "",
7+
"type": "commonjs",
8+
"main": "index.js",
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
},
12+
"dependencies": {
13+
"csv-parse": "^5.6.0",
14+
"csv-stringify": "^6.5.2"
15+
}
16+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const { parse } = require('csv-parse');
4+
const { stringify } = require('csv-stringify');
5+
6+
async function loadCsv() {
7+
const filePath = path.join(__dirname, '../data/SPRING_DISCO-_YEARS_1__2_and_3-05-02-2025.csv');
8+
const data = [];
9+
return new Promise((resolve, reject) => {
10+
fs.createReadStream(filePath)
11+
.pipe(parse({ columns: true, trim: true }))
12+
.on('data', (row) => data.push(row))
13+
.on('end', () => resolve(data))
14+
.on('error', (err) => reject(err));
15+
});
16+
}
17+
18+
function manipulateData(data) {
19+
const pupilCounts = {};
20+
data.forEach(row => {
21+
const pupilKey = row.Pupils;
22+
pupilCounts[pupilKey] = (pupilCounts[pupilKey] || 0) + 1;
23+
});
24+
data.forEach(row => {
25+
row.PupilCount = pupilCounts[row.Pupils];
26+
});
27+
return data;
28+
}
29+
30+
function saveCsvToOutFolder(data) {
31+
const filePath = path.join(__dirname, '../data/SPRING_DISCO-_YEARS_1__2_and_3-05-02-2025.csv');
32+
const outFilePath = path.join(__dirname, '../out', path.basename(filePath));
33+
stringify(data, { header: true }, (err, outputCsv) => {
34+
35+
console.log("Hello World");
36+
37+
if (err) throw err;
38+
fs.writeFileSync(outFilePath, outputCsv);
39+
});
40+
}
41+
42+
function expandPupils(data, years) {
43+
const expanded = [];
44+
const visited = new Set();
45+
data.forEach((row) => {
46+
if (row.Pupils && row.Pupils.includes('&')) {
47+
row.Pupils.split('&').map((p) => p.trim()).forEach((val) => {
48+
if (years.some((year) => val.includes(year))) {
49+
const newRow = { ...row, Pupil: val };
50+
const key = JSON.stringify(newRow);
51+
if (!visited.has(key)) {
52+
visited.add(key);
53+
expanded.push(newRow);
54+
}
55+
}
56+
});
57+
} else {
58+
const newRow = { ...row, Pupil: row.Pupils };
59+
const key = JSON.stringify(newRow);
60+
if (!visited.has(key)) {
61+
visited.add(key);
62+
expanded.push(newRow);
63+
}
64+
}
65+
});
66+
return expanded;
67+
}
68+
69+
async function main() {
70+
71+
const years = ['Year 1', 'Year 2', 'Year 3'];
72+
73+
const data = await loadCsv();
74+
let updated = manipulateData(data);
75+
updated = expandPupils(updated, years);
76+
saveCsvToOutFolder(updated);
77+
}
78+
79+
main();

src/index.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const { parse } = require("csv-parse");
4+
const { stringify } = require("csv-stringify");
5+
6+
async function loadCsv(filename) {
7+
const filePath = path.join(__dirname, "../data/", filename);
8+
const data = [];
9+
return new Promise((resolve, reject) => {
10+
fs.createReadStream(filePath)
11+
.pipe(parse({ columns: true, trim: true }))
12+
.on("data", (row) => data.push(row))
13+
.on("end", () => resolve(data))
14+
.on("error", (err) => reject(err));
15+
});
16+
}
17+
18+
function manipulateData(data) {
19+
const pupilCounts = {};
20+
data.forEach((row) => {
21+
const pupilKey = row.Pupils;
22+
pupilCounts[pupilKey] = (pupilCounts[pupilKey] || 0) + 1;
23+
});
24+
data.forEach((row) => {
25+
row.TicketCount = pupilCounts[row.Pupils];
26+
});
27+
return data;
28+
}
29+
30+
function saveCsvToOutFolder(data, filename) {
31+
const filePath = path.join(__dirname, "../data/", filename);
32+
const outFilePath = path.join(__dirname, "../out", path.basename(filePath));
33+
stringify(data, { header: true }, (err, outputCsv) => {
34+
console.log("\nSaving updated CSV to", outFilePath);
35+
36+
if (err) throw err;
37+
fs.writeFileSync(outFilePath, outputCsv);
38+
});
39+
}
40+
41+
function expandPupils(data, years) {
42+
const expanded = [];
43+
return data.map((row) => {
44+
let retPupil = "Unknown, check other column";
45+
46+
const pupils = row.Pupils.split("&").filter((p) =>
47+
years.some((year) => p.includes(year))
48+
);
49+
50+
if (
51+
// row['Guest first name'] === 'Kelly'
52+
row['Guest first name'] === 'Micaela'
53+
) {
54+
console.log('-----------------');
55+
console.log(`Pupils: ${row.Pupils}, Found: ${pupils}, Tickets: ${row.TicketCount}`);
56+
console.log('-----------------');
57+
}
58+
59+
if (pupils.length > 1) {
60+
console.log(`Multiple pupils found for row ${row.Pupils}`);
61+
}
62+
63+
for (const pupil of pupils) {
64+
if (expanded.some((e) => e.Pupil === pupil && row['Guest first name'] === e['Guest first name'])) {
65+
console.log(`Pupil ${pupil} already exists`);
66+
} else {
67+
console.log(`Adding Pupil ${pupil}`);
68+
retPupil = pupil;
69+
break;
70+
}
71+
}
72+
73+
// save record for later checking
74+
expanded.push({ ...row, Pupil: retPupil });
75+
76+
return { ...row, Pupil: retPupil };
77+
});
78+
}
79+
80+
async function main() {
81+
config = {
82+
reception: {
83+
filename: "CARNIVAL_DISCO-_RECEPTION.csv",
84+
years: ["Reception"],
85+
},
86+
years1_2_3: {
87+
filename: "SPRING_DISCO-_YEARS_1_2_3.csv",
88+
years: ["Year 1", "Year 2", "Year 3"],
89+
},
90+
years4_5_6: {
91+
filename: "SPRING_DISCO-_YEARS_4_5_6.csv",
92+
years: ["Year 4", "Year 5", "Year 6"],
93+
},
94+
};
95+
96+
// filename: "SPRING_DISCO-_YEARS_1__2_and_3-05-02-2025.csv"
97+
// const years = ["Year 1", "Year 2", "Year 3"];
98+
99+
async function disco(filename, years) {
100+
const data = await loadCsv(filename);
101+
let updated = manipulateData(data);
102+
updated = expandPupils(updated, years);
103+
saveCsvToOutFolder(updated, filename);
104+
console.log("Input Data, Rows: ", data.length);
105+
console.log("Output Data, Rows: ", updated.length);
106+
}
107+
108+
disco(config.reception.filename, config.reception.years);
109+
disco(config.years1_2_3.filename, config.years1_2_3.years);
110+
disco(config.years4_5_6.filename, config.years4_5_6.years);
111+
}
112+
113+
main();
114+
115+
// reception = 48 tickets, outpout 44
116+
// years 1,2,3 = 85 tickets, output 85
117+
// years 4,5,6 = 114 tickets, output 113

0 commit comments

Comments
 (0)