Fix CSV restore: strip BOM and extract embedded ratings from drink rows
- Strip UTF-8 BOM that Excel/editors add to CSV files - When drink rows contain score/notes/wouldReorder fields, automatically create rating entries (supports manually edited CSVs) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -263,6 +263,23 @@ export function parseBackupRows(
|
|||||||
createdAt: parseOptionalDate(row.createdAt ?? ""),
|
createdAt: parseOptionalDate(row.createdAt ?? ""),
|
||||||
updatedAt: parseOptionalDate(row.updatedAt ?? ""),
|
updatedAt: parseOptionalDate(row.updatedAt ?? ""),
|
||||||
})
|
})
|
||||||
|
// Extract embedded rating data from drink rows (e.g. manually edited CSVs)
|
||||||
|
if (row.score && row.score.trim() !== "") {
|
||||||
|
const score = parseInt(row.score, 10)
|
||||||
|
if (score >= 1 && score <= 5) {
|
||||||
|
data.ratings.push({
|
||||||
|
_originalId: `${row._originalId ?? ""}_rating`,
|
||||||
|
_parentId: row._originalId ?? "",
|
||||||
|
_drinkName: row.name ?? "",
|
||||||
|
score,
|
||||||
|
notes: row.notes || undefined,
|
||||||
|
wouldReorder: parseBoolean(row.wouldReorder ?? "false"),
|
||||||
|
location: row.location || undefined,
|
||||||
|
createdAt: parseOptionalDate(row.createdAt ?? ""),
|
||||||
|
updatedAt: parseOptionalDate(row.updatedAt ?? ""),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
break
|
break
|
||||||
|
|
||||||
case "rating":
|
case "rating":
|
||||||
|
|||||||
@@ -39,7 +39,9 @@ export function objectsToCsv(
|
|||||||
* Uses a state-machine parser to correctly handle quoted fields.
|
* Uses a state-machine parser to correctly handle quoted fields.
|
||||||
*/
|
*/
|
||||||
export function csvToObjects(csvText: string): Record<string, string>[] {
|
export function csvToObjects(csvText: string): Record<string, string>[] {
|
||||||
const rows = parseCsvRows(csvText)
|
// Strip UTF-8 BOM if present
|
||||||
|
const cleanText = csvText.charCodeAt(0) === 0xfeff ? csvText.slice(1) : csvText
|
||||||
|
const rows = parseCsvRows(cleanText)
|
||||||
if (rows.length < 1) return []
|
if (rows.length < 1) return []
|
||||||
|
|
||||||
const headers = rows[0]
|
const headers = rows[0]
|
||||||
|
|||||||
Reference in New Issue
Block a user