-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Romuald Caplier
committed
Sep 30, 2024
1 parent
df10494
commit 83481bf
Showing
6 changed files
with
122 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
libs/util/shared/src/lib/utils/no-duplicate-file-name.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { noDuplicateFileName } from './no-duplicate-file-name' | ||
|
||
describe('noDuplicateFileName', () => { | ||
it('should return the original file name if it does not exist in the fileNameList', () => { | ||
const fileName = 'testfile.txt' | ||
const fileNameList = ['otherfile.txt', 'anotherfile.txt'] | ||
|
||
const result = noDuplicateFileName(fileName, fileNameList) | ||
|
||
expect(result).toBe(fileName) | ||
}) | ||
|
||
it('should return a new file name with a timestamp if the file name exists in the fileNameList', () => { | ||
const fileName = 'testfile.txt' | ||
const fileNameList = ['testfile.txt', 'otherfile.txt'] | ||
|
||
const result = noDuplicateFileName(fileName, fileNameList) | ||
|
||
// Check if the new file name starts with the base name and contains a timestamp | ||
const regex = /testfile_\d+\.txt/ | ||
expect(result).toMatch(regex) | ||
}) | ||
|
||
it('should handle file names without an extension', () => { | ||
const fileName = 'testfile' | ||
const fileNameList = ['testfile'] | ||
|
||
const result = noDuplicateFileName(fileName, fileNameList) | ||
|
||
// Check if the new file name has a timestamp appended with no extension | ||
const regex = /testfile_\d+/ | ||
expect(result).toMatch(regex) | ||
}) | ||
|
||
it('should handle file names with multiple dots correctly', () => { | ||
const fileName = 'test.file.name.txt' | ||
const fileNameList = ['test.file.name.txt'] | ||
|
||
const result = noDuplicateFileName(fileName, fileNameList) | ||
|
||
// Check if the new file name with multiple dots contains a timestamp | ||
const regex = /test\.file\.name_\d+\.txt/ | ||
expect(result).toMatch(regex) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export function noDuplicateFileName( | ||
fileName: string, | ||
fileNameList: string[] | ||
): string { | ||
if (fileNameList.includes(fileName)) { | ||
const fileNameParts = fileName.split('.') | ||
let extension = '' | ||
let baseName = fileName | ||
|
||
if (fileNameParts.length > 1) { | ||
extension = fileNameParts.pop() as string | ||
baseName = fileNameParts.join('.') | ||
} | ||
|
||
if (extension) { | ||
fileName = `${baseName}_${Date.now()}.${extension}` | ||
} else { | ||
fileName = `${baseName}_${Date.now()}` | ||
} | ||
} | ||
|
||
return fileName | ||
} |