From 8cbdf1393f26e27387c11f3c79857907b9cc009c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thea=20Sch=C3=B6bl?= Date: Fri, 8 Dec 2023 21:14:37 +0100 Subject: [PATCH] fix: chord files not detected properly feat: alert on unknown backups --- src/lib/backup/backup.ts | 2 ++ src/lib/backup/compat/legacy-chords.ts | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/lib/backup/backup.ts b/src/lib/backup/backup.ts index 153311be..a68aa52c 100644 --- a/src/lib/backup/backup.ts +++ b/src/lib/backup/backup.ts @@ -60,6 +60,8 @@ export async function restoreBackup(event: Event) { restoreFromFile(csvLayoutToJson(text)) } else if (isCsvChords(text)) { restoreFromFile(csvChordsToJson(text)) + } else { + alert("Unknown backup format") } } diff --git a/src/lib/backup/compat/legacy-chords.ts b/src/lib/backup/compat/legacy-chords.ts index c95586ac..e43b850d 100644 --- a/src/lib/backup/compat/legacy-chords.ts +++ b/src/lib/backup/compat/legacy-chords.ts @@ -5,16 +5,19 @@ export function csvChordsToJson(csv: string): CharaChordFile { return { charaVersion: 1, type: "chords", - chords: csv.split("\n").map(line => { - const [input, output] = line.split(",", 2) - return [ - input.split("+").map(it => KEYMAP_IDS.get(it.trim())?.code ?? 0), - output.split("").map(it => KEYMAP_IDS.get(it.trim())?.code ?? 0), - ] - }), + chords: csv + .trim() + .split("\n") + .map(line => { + const [input, output] = line.split(",", 2) + return [ + input.split("+").map(it => KEYMAP_IDS.get(it.trim())?.code ?? 0), + output.split("").map(it => KEYMAP_IDS.get(it.trim())?.code ?? 0), + ] + }), } } export function isCsvChords(csv: string): boolean { - return /^([^+,\s]( *\+ *[^+,\s]+)* *, *[^+,\s]+ *(\n|(?=$)))+$/.test(csv) + return /^([^+, ]+( *\+ *[^+, ]+)* *, *[^+, ]+ *(\n|(?=$)))+$/.test(csv) }