Skip to content

Commit

Permalink
Fix processing of CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchernchong committed Dec 16, 2024
1 parent 8617dce commit 553d789
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/lib/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const updater = async <T extends PgTable>({
);

// Check against the existing records for new non-duplicated entries
const newRecords = processedData.filter((record: Record<string, any>) => {
const newRecords = processedData.filter((record) => {
const identifier = createUniqueKey(record, keyFields);
return !existingKeys.has(identifier);
});
Expand Down
4 changes: 2 additions & 2 deletions src/utils/createUniqueKey.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const createUniqueKey = (
item: Record<string, any>,
record: Record<string, any>,
keyFields: string[],
) => keyFields.map((field) => item[field]).join("-");
) => keyFields.map((field) => record[field]).join("-");
53 changes: 25 additions & 28 deletions src/utils/processCSV.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
import fs from "node:fs";
import Papa from "papaparse";

export const processCSV = async (
filePath: string,
): Promise<Record<string, any>[]> => {
export const processCSV = async (filePath: string) => {
const fileContent = fs.readFileSync(filePath, "utf-8");

return new Promise<Record<string, any>[]>((resolve, reject) => {
Papa.parse(fileContent, {
header: true,
dynamicTyping: true,
skipEmptyLines: true,
transform: (value) => {
if (/\d+,\d+/.test(value)) {
if (value === "") {
return 0;
}
const cleanValue = value.replace(/,/g, "");
return cleanValue.includes(".")
? Number.parseFloat(cleanValue)
: Number.parseInt(cleanValue, 10);
}
return value;
},
complete: ({ data }: { data: Record<string, any>[] }) => {
resolve(data);
},
error: (error: Error) => {
console.error(error);
reject(error);
},
});
const { data } = Papa.parse(fileContent, {
header: true,
dynamicTyping: true,
skipEmptyLines: true,
transform: (value, field) => {
// Handle empty number field
if (field === "number" && value === "") {
return 0;
}

// Clean up make field
if (field === "make") {
return value.trim().replace(/\./g, "");
}

// Handle numeric values with commas
if (/\d+,\d+/.test(value)) {
return Number.parseInt(value.replace(/,/g, ""), 10);
}

return value;
},
});

return data;
};

0 comments on commit 553d789

Please sign in to comment.