diff --git a/src/lib/files.ts b/src/lib/files.ts index 8d2f8ce..4d629a6 100644 --- a/src/lib/files.ts +++ b/src/lib/files.ts @@ -18,6 +18,10 @@ export function droppedPhotos(transfer: DataTransfer): File[] { // in the chosen folder, must not be overwritten: IMG_0001-date-2.jpg. export function uniqueName(taken: (name: string) => boolean, name: string) { let candidate = name - for (let n = 2; taken(candidate); n++) candidate = name.replace(/(\.[^.]+)$/, `-${n}$1`) + for (let n = 2; taken(candidate); n++) { + // A name without an extension (macOS Photos sometimes drops one) would + // otherwise keep the same candidate forever and hang the save. + candidate = /(\.[^.]+)$/.test(name) ? name.replace(/(\.[^.]+)$/, `-${n}$1`) : `${name}-${n}` + } return candidate } diff --git a/tests/unit/files.test.ts b/tests/unit/files.test.ts index 9452187..0f82536 100644 --- a/tests/unit/files.test.ts +++ b/tests/unit/files.test.ts @@ -40,3 +40,8 @@ describe("stampedFileName", () => { expect(stampedFileName("holiday.photo.png")).toBe("holiday.photo-date.jpg") }) }) + + it("numbers an extensionless name without looping", () => { + const taken = new Set(["export", "export-2"]) + expect(uniqueName((name) => taken.has(name), "export")).toBe("export-3") + })