Skip to content

Commit 8127133

Browse files
committed
fix #2973 -- duplicate of 2018-07-12-091218.ipynb is 2018-07-12-91219.ipynb, but should be 2018-07-12-091219.ipynb
1 parent 543b90c commit 8127133

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/packages/util/misc.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,25 @@ test 123`;
321321
expect(t).toEqual("foo");
322322
});
323323
});
324+
325+
describe("suggest_duplicate_filename", () => {
326+
const dup = misc.suggest_duplicate_filename;
327+
it("works with numbers", () => {
328+
expect(dup("filename-1.test")).toBe("filename-2.test");
329+
expect(dup("filename-99.test")).toBe("filename-100.test");
330+
expect(dup("filename_99.test")).toBe("filename_100.test");
331+
});
332+
it("handles leading zeros", () => {
333+
// handles leading 0's properly: https://github.com/sagemathinc/cocalc/issues/2973
334+
expect(dup("filename_001.test")).toBe("filename_002.test");
335+
});
336+
it("works also without", () => {
337+
expect(dup("filename-test")).toBe("filename-test-1");
338+
expect(dup("filename-xxx.test")).toBe("filename-xxx-1.test");
339+
expect(dup("bla")).toBe("bla-1");
340+
expect(dup("foo.bar")).toBe("foo-1.bar");
341+
});
342+
it("also works with weird corner cases", () => {
343+
expect(dup("asdf-")).toBe("asdf--1");
344+
});
345+
});

src/packages/util/misc.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,6 +2047,7 @@ export function tab_to_path(name: string): string | undefined {
20472047
// suggest a new filename when duplicating it as follows:
20482048
// strip extension, split at '_' or '-' if it exists
20492049
// try to parse a number, if it works, increment it, etc.
2050+
// Handle leading zeros for the number (see https://github.com/sagemathinc/cocalc/issues/2973)
20502051
export function suggest_duplicate_filename(name: string): string {
20512052
let ext;
20522053
({ name, ext } = separate_file_extension(name));
@@ -2059,9 +2060,13 @@ export function suggest_duplicate_filename(name: string): string {
20592060
name.slice(0, idx + 1),
20602061
name.slice(idx + 1),
20612062
]);
2062-
const num = parseInt(ending);
2063+
// Pad the number with leading zeros to maintain the original length
2064+
const paddedEnding = ending.padStart(ending.length, "0");
2065+
const num = parseInt(paddedEnding);
20632066
if (!Number.isNaN(num)) {
2064-
new_name = `${prefix}${num + 1}`;
2067+
// Increment the number and pad it back to the original length
2068+
const newNum = (num + 1).toString().padStart(ending.length, "0");
2069+
new_name = `${prefix}${newNum}`;
20652070
}
20662071
}
20672072
if (new_name == null) {

0 commit comments

Comments
 (0)