File tree Expand file tree Collapse file tree 2 files changed +29
-2
lines changed Expand file tree Collapse file tree 2 files changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -321,3 +321,25 @@ test 123`;
321
321
expect ( t ) . toEqual ( "foo" ) ;
322
322
} ) ;
323
323
} ) ;
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
+ } ) ;
Original file line number Diff line number Diff line change @@ -2047,6 +2047,7 @@ export function tab_to_path(name: string): string | undefined {
2047
2047
// suggest a new filename when duplicating it as follows:
2048
2048
// strip extension, split at '_' or '-' if it exists
2049
2049
// 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)
2050
2051
export function suggest_duplicate_filename ( name : string ) : string {
2051
2052
let ext ;
2052
2053
( { name, ext } = separate_file_extension ( name ) ) ;
@@ -2059,9 +2060,13 @@ export function suggest_duplicate_filename(name: string): string {
2059
2060
name . slice ( 0 , idx + 1 ) ,
2060
2061
name . slice ( idx + 1 ) ,
2061
2062
] ) ;
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 ) ;
2063
2066
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 } ` ;
2065
2070
}
2066
2071
}
2067
2072
if ( new_name == null ) {
You can’t perform that action at this time.
0 commit comments