forked from DonshayS/Data-With-Working-Files
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_cleaning_template.js
More file actions
59 lines (47 loc) · 1.47 KB
/
Copy pathdata_cleaning_template.js
File metadata and controls
59 lines (47 loc) · 1.47 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
const fs = require("fs-extra");
const csv = require("csvtojson");
const { Parser } = require("json2csv");
const dayjs = require("dayjs");
// --- Step 1: Read raw CSV ---
const rawPath = "./raw_sales_data.csv";
async function cleanData() {
console.log("Reading raw CSV...");
let data = await csv().fromFile(rawPath);
console.log("Preview of raw data:");
console.table(data.slice(0, 3));
// --- Step 2: Clean and standardize ---
data = data.map((row) => {
// Normalize keys
return {
CustomerID: row.CustomerID?.trim(),
product: row.product?.trim().toLowerCase(),
price: cleanPrice(row.Price),
date: cleanDate(row.Date),
City: row.City?.trim(),
};
});
// --- Step 3: Export cleaned data ---
await exportCleanData(data);
// --- Step 4: Log changes ---
await fs(
"./cleaning_log.txt",
`Cleaned ${data.length} records on ${new Date()}\n`.toString()
);
}
function cleanPrice(price) {
if (!price || price.toLowerCase() === "n/a") return null;
return Number(price);
}
function cleanDate(date) {
const parsed = dayjs(date);
return parsed.isValid() ? parsed.format("YYYY-MM-DD") : null;
}
async function exportCleanData(data) {
// Export JSON
await fs.writeJson("./clean_data.json", data, { spaces: 2 });
// Export CSV
const parser = new Parser();
const csvData = parser.parse(data);
await fs.writeFile("./clean_data.csv", csvData);
}
cleanData();