-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8617dce
commit 553d789
Showing
3 changed files
with
28 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("-"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |