Skip to content

Commit

Permalink
fix: chord files not detected properly
Browse files Browse the repository at this point in the history
feat: alert on unknown backups
  • Loading branch information
Theaninova committed Dec 8, 2023
1 parent 1ccb17f commit 8cbdf13
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/lib/backup/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
19 changes: 11 additions & 8 deletions src/lib/backup/compat/legacy-chords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 8cbdf13

Please sign in to comment.