Update files name duplicate counter (#2391) (#2455)

This commit is contained in:
Ivan Saprykin
2025-05-14 02:12:26 +02:00
committed by GitHub
parent 786d7fb257
commit 3ea7f142f0
2 changed files with 13 additions and 4 deletions

View File

@ -1,7 +1,7 @@
import 'package:common/model/file_type.dart';
/// Matches myFile-123 -> 123
final _fileNumberRegex = RegExp(r'^(.*)(?:-(\d+))$');
/// Matches myFile (123) -> "myFile", " (123)"
final _fileNumberRegex = RegExp(r'^(.*)(?:(\s\(\d+\)))$');
extension FilePathStringExt on String {
String get extension {
@ -43,9 +43,9 @@ extension FilePathStringExt on String {
final match = _fileNumberRegex.firstMatch(fileName);
if (match != null) {
return '${match.group(1)}-$count'.withExtension(extension);
return '${match.group(1)} ($count)'.withExtension(extension);
} else {
return '$fileName-$count'.withExtension(extension);
return '$fileName ($count)'.withExtension(extension);
}
}

View File

@ -0,0 +1,9 @@
import 'package:localsend_app/util/file_path_helper.dart';
import 'package:test/test.dart';
void main() {
test('fileName with counter', () async {
expect('myFile'.withCount(1), 'myFile (1)');
expect('myFile (1)'.withCount(2), 'myFile (2)');
});
}