chore: fix duplicate file name error when exporting notes and refactor file name utils (#2877) [skip e2e]

This commit is contained in:
Aman Harwara
2024-05-07 15:45:50 +05:30
committed by GitHub
parent 71d2dbca6c
commit c3265d7930
16 changed files with 61 additions and 56 deletions

View 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)
}

View File

@@ -11,3 +11,4 @@ export * from './Utils/Utils'
export * from './Uuid/Utils'
export * from './Uuid/UuidGenerator'
export * from './Uuid/UuidMap'
export * from './FileName/FileNameUtils'