chore: fix duplicate file name error when exporting notes and refactor file name utils (#2877) [skip e2e]
This commit is contained in:
38
packages/utils/src/Domain/FileName/FileNameUtils.ts
Normal file
38
packages/utils/src/Domain/FileName/FileNameUtils.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
export function parseFileName(fileName: string): {
|
||||
name: string
|
||||
ext: string
|
||||
} {
|
||||
const pattern = /(?:\.([^.]+))$/
|
||||
const extMatches = pattern.exec(fileName)
|
||||
const ext = extMatches?.[1] || ''
|
||||
const name = fileName.includes('.') ? fileName.substring(0, fileName.lastIndexOf('.')) : fileName
|
||||
|
||||
return { name, ext }
|
||||
}
|
||||
|
||||
export function sanitizeFileName(name: string): string {
|
||||
return name.trim().replace(/[.\\/:"?*|<>]/g, '_')
|
||||
}
|
||||
|
||||
export function truncateFileName(name: string, maxLength: number): string {
|
||||
return name.length > maxLength ? name.slice(0, maxLength) : name
|
||||
}
|
||||
|
||||
const MaxFileNameLength = 100
|
||||
|
||||
export function createZippableFileName(
|
||||
name: string,
|
||||
suffix = '',
|
||||
format = 'txt',
|
||||
maxLength = MaxFileNameLength,
|
||||
): string {
|
||||
const sanitizedName = sanitizeFileName(name)
|
||||
const truncatedName = truncateFileName(sanitizedName, maxLength)
|
||||
const nameEnd = suffix + '.' + format
|
||||
return truncatedName + nameEnd
|
||||
}
|
||||
|
||||
export function parseAndCreateZippableFileName(name: string, suffix = '') {
|
||||
const { name: parsedName, ext } = parseFileName(name)
|
||||
return createZippableFileName(parsedName, suffix, ext)
|
||||
}
|
||||
@@ -11,3 +11,4 @@ export * from './Utils/Utils'
|
||||
export * from './Uuid/Utils'
|
||||
export * from './Uuid/UuidGenerator'
|
||||
export * from './Uuid/UuidMap'
|
||||
export * from './FileName/FileNameUtils'
|
||||
|
||||
Reference in New Issue
Block a user