refactor(utils): 增强文件图标识别函数
- 优化 getDocumentIcon 支持多种扩展名来源 - 增强 extractExtensionFromFile 支持更多解析路径 - 改进 JSDoc 文档 Co-Authored-By: Claude Sonnet 4.5
Showing
1 changed file
with
48 additions
and
12 deletions
| ... | @@ -106,16 +106,20 @@ const DEFAULT_LABEL = 'DOC'; | ... | @@ -106,16 +106,20 @@ const DEFAULT_LABEL = 'DOC'; |
| 106 | /** | 106 | /** |
| 107 | * 根据文件名或扩展名获取文档图标路径 | 107 | * 根据文件名或扩展名获取文档图标路径 |
| 108 | * | 108 | * |
| 109 | - * @description 支持传入文件名或包含 extension 字段的对象,优先使用 extension 字段 | 109 | + * @description 支持传入文件名或包含 extension 字段的对象,优先使用 extension 字段。 |
| 110 | - * @param {string|Object} fileNameOrItem - 文件名(如:document.pdf)或文件对象(包含 extension 字段) | 110 | + * 如果没有 extension,会尝试从 fileName、src、downloadUrl 等字段解析。 |
| 111 | + * @param {string|Object} fileNameOrItem - 文件名(如:document.pdf)或文件对象 | ||
| 111 | * @param {string} [fileNameOrItem.fileName] - 文件名 | 112 | * @param {string} [fileNameOrItem.fileName] - 文件名 |
| 112 | * @param {string} [fileNameOrItem.extension] - 文件扩展名(优先使用) | 113 | * @param {string} [fileNameOrItem.extension] - 文件扩展名(优先使用) |
| 114 | + * @param {string} [fileNameOrItem.src] - 文件 URL(用于提取扩展名) | ||
| 115 | + * @param {string} [fileNameOrItem.downloadUrl] - 下载 URL(用于提取扩展名) | ||
| 113 | * @returns {string} 图标路径 | 116 | * @returns {string} 图标路径 |
| 114 | * | 117 | * |
| 115 | * @example | 118 | * @example |
| 116 | * getDocumentIcon('报告.pdf') // 返回 PDF 图标 | 119 | * getDocumentIcon('报告.pdf') // 返回 PDF 图标 |
| 117 | * getDocumentIcon({ extension: 'pdf' }) // 返回 PDF 图标(优先使用 extension) | 120 | * getDocumentIcon({ extension: 'pdf' }) // 返回 PDF 图标(优先使用 extension) |
| 118 | * getDocumentIcon({ fileName: '报告.pdf' }) // 返回 PDF 图标 | 121 | * getDocumentIcon({ fileName: '报告.pdf' }) // 返回 PDF 图标 |
| 122 | + * getDocumentIcon({ src: 'https://example.com/file.png' }) // 返回 PNG 图标(从 URL 解析) | ||
| 119 | * getDocumentIcon('数据.xlsx') // 返回 Excel 图标 | 123 | * getDocumentIcon('数据.xlsx') // 返回 Excel 图标 |
| 120 | * getDocumentIcon('图片.png') // 返回 PNG 图标 | 124 | * getDocumentIcon('图片.png') // 返回 PNG 图标 |
| 121 | */ | 125 | */ |
| ... | @@ -138,6 +142,8 @@ export function getDocumentIcon(fileNameOrItem) { | ... | @@ -138,6 +142,8 @@ export function getDocumentIcon(fileNameOrItem) { |
| 138 | * @param {string|Object} fileNameOrItem - 文件名(如:document.pdf)或文件对象(包含 extension 字段) | 142 | * @param {string|Object} fileNameOrItem - 文件名(如:document.pdf)或文件对象(包含 extension 字段) |
| 139 | * @param {string} [fileNameOrItem.fileName] - 文件名 | 143 | * @param {string} [fileNameOrItem.fileName] - 文件名 |
| 140 | * @param {string} [fileNameOrItem.extension] - 文件扩展名(优先使用) | 144 | * @param {string} [fileNameOrItem.extension] - 文件扩展名(优先使用) |
| 145 | + * @param {string} [fileNameOrItem.src] - 文件 URL(用于提取扩展名) | ||
| 146 | + * @param {string} [fileNameOrItem.downloadUrl] - 下载 URL(用于提取扩展名) | ||
| 141 | * @returns {string} 扩展名(小写),空字符串表示无法提取 | 147 | * @returns {string} 扩展名(小写),空字符串表示无法提取 |
| 142 | * | 148 | * |
| 143 | * @example | 149 | * @example |
| ... | @@ -145,6 +151,8 @@ export function getDocumentIcon(fileNameOrItem) { | ... | @@ -145,6 +151,8 @@ export function getDocumentIcon(fileNameOrItem) { |
| 145 | * extractExtensionFromFile({ extension: 'PDF' }) // 'pdf'(自动转小写) | 151 | * extractExtensionFromFile({ extension: 'PDF' }) // 'pdf'(自动转小写) |
| 146 | * extractExtensionFromFile({ fileName: 'document.DOC' }) // 'doc'(从文件名解析) | 152 | * extractExtensionFromFile({ fileName: 'document.DOC' }) // 'doc'(从文件名解析) |
| 147 | * extractExtensionFromFile({ extension: 'pdf', fileName: 'backup.doc' }) // 'pdf'(优先使用 extension) | 153 | * extractExtensionFromFile({ extension: 'pdf', fileName: 'backup.doc' }) // 'pdf'(优先使用 extension) |
| 154 | + * extractExtensionFromFile({ src: 'https://example.com/file.png' }) // 'png'(从 URL 解析) | ||
| 155 | + * extractExtensionFromFile({ downloadUrl: 'https://cdn.com/file.jpg' }) // 'jpg'(从 URL 解析) | ||
| 148 | */ | 156 | */ |
| 149 | export function extractExtensionFromFile(fileNameOrItem) { | 157 | export function extractExtensionFromFile(fileNameOrItem) { |
| 150 | let extension = ''; | 158 | let extension = ''; |
| ... | @@ -152,10 +160,22 @@ export function extractExtensionFromFile(fileNameOrItem) { | ... | @@ -152,10 +160,22 @@ export function extractExtensionFromFile(fileNameOrItem) { |
| 152 | // 支持对象格式(优先使用 extension 字段) | 160 | // 支持对象格式(优先使用 extension 字段) |
| 153 | if (typeof fileNameOrItem === 'object' && fileNameOrItem !== null) { | 161 | if (typeof fileNameOrItem === 'object' && fileNameOrItem !== null) { |
| 154 | extension = fileNameOrItem.extension || ''; | 162 | extension = fileNameOrItem.extension || ''; |
| 155 | - // 如果没有 extension 字段,尝试从 fileName 解析 | 163 | + |
| 156 | - if (!extension && fileNameOrItem.fileName) { | 164 | + // 如果没有 extension 字段,尝试从其他字段解析 |
| 165 | + if (!extension) { | ||
| 166 | + // 优先从 fileName 解析 | ||
| 167 | + if (fileNameOrItem.fileName) { | ||
| 157 | extension = extractExtensionFromString(fileNameOrItem.fileName); | 168 | extension = extractExtensionFromString(fileNameOrItem.fileName); |
| 158 | } | 169 | } |
| 170 | + // 如果 fileName 没有扩展名,尝试从 src 解析 | ||
| 171 | + else if (fileNameOrItem.src) { | ||
| 172 | + extension = extractExtensionFromString(fileNameOrItem.src); | ||
| 173 | + } | ||
| 174 | + // 如果 src 也没有,尝试从 downloadUrl 解析 | ||
| 175 | + else if (fileNameOrItem.downloadUrl) { | ||
| 176 | + extension = extractExtensionFromString(fileNameOrItem.downloadUrl); | ||
| 177 | + } | ||
| 178 | + } | ||
| 159 | } else if (typeof fileNameOrItem === 'string') { | 179 | } else if (typeof fileNameOrItem === 'string') { |
| 160 | // 兼容字符串格式,从文件名解析 | 180 | // 兼容字符串格式,从文件名解析 |
| 161 | extension = extractExtensionFromString(fileNameOrItem); | 181 | extension = extractExtensionFromString(fileNameOrItem); |
| ... | @@ -191,16 +211,20 @@ function extractExtensionFromString(fileName) { | ... | @@ -191,16 +211,20 @@ function extractExtensionFromString(fileName) { |
| 191 | /** | 211 | /** |
| 192 | * 根据文件名或扩展名获取文件类型标签 | 212 | * 根据文件名或扩展名获取文件类型标签 |
| 193 | * | 213 | * |
| 194 | - * @description 支持传入文件名或包含 extension 字段的对象,优先使用 extension 字段 | 214 | + * @description 支持传入文件名或包含 extension 字段的对象,优先使用 extension 字段。 |
| 195 | - * @param {string|Object} fileNameOrItem - 文件名(如:document.pdf)或文件对象(包含 extension 字段) | 215 | + * 如果没有 extension,会尝试从 fileName、src、downloadUrl 等字段解析。 |
| 216 | + * @param {string|Object} fileNameOrItem - 文件名(如:document.pdf)或文件对象 | ||
| 196 | * @param {string} [fileNameOrItem.fileName] - 文件名 | 217 | * @param {string} [fileNameOrItem.fileName] - 文件名 |
| 197 | * @param {string} [fileNameOrItem.extension] - 文件扩展名(优先使用) | 218 | * @param {string} [fileNameOrItem.extension] - 文件扩展名(优先使用) |
| 219 | + * @param {string} [fileNameOrItem.src] - 文件 URL(用于提取扩展名) | ||
| 220 | + * @param {string} [fileNameOrItem.downloadUrl] - 下载 URL(用于提取扩展名) | ||
| 198 | * @returns {string} 文件类型标签(如:PDF、Word、Excel) | 221 | * @returns {string} 文件类型标签(如:PDF、Word、Excel) |
| 199 | * | 222 | * |
| 200 | * @example | 223 | * @example |
| 201 | * getDocumentLabel('报告.pdf') // 'PDF' | 224 | * getDocumentLabel('报告.pdf') // 'PDF' |
| 202 | * getDocumentLabel({ extension: 'pdf' }) // 'PDF'(优先使用 extension) | 225 | * getDocumentLabel({ extension: 'pdf' }) // 'PDF'(优先使用 extension) |
| 203 | * getDocumentLabel({ fileName: '报告.pdf' }) // 'PDF' | 226 | * getDocumentLabel({ fileName: '报告.pdf' }) // 'PDF' |
| 227 | + * getDocumentLabel({ src: 'https://example.com/file.png' }) // 'PNG'(从 URL 解析) | ||
| 204 | * getDocumentLabel('数据.xlsx') // 'Excel' | 228 | * getDocumentLabel('数据.xlsx') // 'Excel' |
| 205 | * getDocumentLabel('图片.png') // 'PNG' | 229 | * getDocumentLabel('图片.png') // 'PNG' |
| 206 | */ | 230 | */ |
| ... | @@ -218,16 +242,20 @@ export function getDocumentLabel(fileNameOrItem) { | ... | @@ -218,16 +242,20 @@ export function getDocumentLabel(fileNameOrItem) { |
| 218 | /** | 242 | /** |
| 219 | * 根据文件名或扩展名判断是否为 PDF 文件 | 243 | * 根据文件名或扩展名判断是否为 PDF 文件 |
| 220 | * | 244 | * |
| 221 | - * @description 支持传入文件名或包含 extension 字段的对象,优先使用 extension 字段 | 245 | + * @description 支持传入文件名或包含 extension 字段的对象,优先使用 extension 字段。 |
| 222 | - * @param {string|Object} fileNameOrItem - 文件名或文件对象(包含 extension 字段) | 246 | + * 如果没有 extension,会尝试从 fileName、src、downloadUrl 等字段解析。 |
| 247 | + * @param {string|Object} fileNameOrItem - 文件名或文件对象 | ||
| 223 | * @param {string} [fileNameOrItem.fileName] - 文件名 | 248 | * @param {string} [fileNameOrItem.fileName] - 文件名 |
| 224 | * @param {string} [fileNameOrItem.extension] - 文件扩展名(优先使用) | 249 | * @param {string} [fileNameOrItem.extension] - 文件扩展名(优先使用) |
| 250 | + * @param {string} [fileNameOrItem.src] - 文件 URL(用于提取扩展名) | ||
| 251 | + * @param {string} [fileNameOrItem.downloadUrl] - 下载 URL(用于提取扩展名) | ||
| 225 | * @returns {boolean} 是否为 PDF 文件 | 252 | * @returns {boolean} 是否为 PDF 文件 |
| 226 | * | 253 | * |
| 227 | * @example | 254 | * @example |
| 228 | * isPDF('document.pdf') // true | 255 | * isPDF('document.pdf') // true |
| 229 | * isPDF({ extension: 'pdf' }) // true(优先使用 extension) | 256 | * isPDF({ extension: 'pdf' }) // true(优先使用 extension) |
| 230 | * isPDF({ fileName: 'document.pdf' }) // true | 257 | * isPDF({ fileName: 'document.pdf' }) // true |
| 258 | + * isPDF({ src: 'https://example.com/file.pdf' }) // true(从 URL 解析) | ||
| 231 | * isPDF('document.docx') // false | 259 | * isPDF('document.docx') // false |
| 232 | */ | 260 | */ |
| 233 | export function isPDF(fileNameOrItem) { | 261 | export function isPDF(fileNameOrItem) { |
| ... | @@ -238,15 +266,19 @@ export function isPDF(fileNameOrItem) { | ... | @@ -238,15 +266,19 @@ export function isPDF(fileNameOrItem) { |
| 238 | /** | 266 | /** |
| 239 | * 根据文件名或扩展名判断是否为图片文件 | 267 | * 根据文件名或扩展名判断是否为图片文件 |
| 240 | * | 268 | * |
| 241 | - * @description 支持传入文件名或包含 extension 字段的对象,优先使用 extension 字段 | 269 | + * @description 支持传入文件名或包含 extension 字段的对象,优先使用 extension 字段。 |
| 242 | - * @param {string|Object} fileNameOrItem - 文件名或文件对象(包含 extension 字段) | 270 | + * 如果没有 extension,会尝试从 fileName、src、downloadUrl 等字段解析。 |
| 271 | + * @param {string|Object} fileNameOrItem - 文件名或文件对象 | ||
| 243 | * @param {string} [fileNameOrItem.fileName] - 文件名 | 272 | * @param {string} [fileNameOrItem.fileName] - 文件名 |
| 244 | * @param {string} [fileNameOrItem.extension] - 文件扩展名(优先使用) | 273 | * @param {string} [fileNameOrItem.extension] - 文件扩展名(优先使用) |
| 274 | + * @param {string} [fileNameOrItem.src] - 文件 URL(用于提取扩展名) | ||
| 275 | + * @param {string} [fileNameOrItem.downloadUrl] - 下载 URL(用于提取扩展名) | ||
| 245 | * @returns {boolean} 是否为图片文件 | 276 | * @returns {boolean} 是否为图片文件 |
| 246 | * | 277 | * |
| 247 | * @example | 278 | * @example |
| 248 | * isImage('photo.jpg') // true | 279 | * isImage('photo.jpg') // true |
| 249 | * isImage({ extension: 'jpg' }) // true(优先使用 extension) | 280 | * isImage({ extension: 'jpg' }) // true(优先使用 extension) |
| 281 | + * isImage({ src: 'https://example.com/file.png' }) // true(从 URL 解析) | ||
| 250 | * isImage('document.pdf') // false | 282 | * isImage('document.pdf') // false |
| 251 | */ | 283 | */ |
| 252 | export function isImage(fileNameOrItem) { | 284 | export function isImage(fileNameOrItem) { |
| ... | @@ -261,15 +293,19 @@ export function isImage(fileNameOrItem) { | ... | @@ -261,15 +293,19 @@ export function isImage(fileNameOrItem) { |
| 261 | /** | 293 | /** |
| 262 | * 根据文件名或扩展名判断是否为视频文件 | 294 | * 根据文件名或扩展名判断是否为视频文件 |
| 263 | * | 295 | * |
| 264 | - * @description 支持传入文件名或包含 extension 字段的对象,优先使用 extension 字段 | 296 | + * @description 支持传入文件名或包含 extension 字段的对象,优先使用 extension 字段。 |
| 265 | - * @param {string|Object} fileNameOrItem - 文件名或文件对象(包含 extension 字段) | 297 | + * 如果没有 extension,会尝试从 fileName、src、downloadUrl 等字段解析。 |
| 298 | + * @param {string|Object} fileNameOrItem - 文件名或文件对象 | ||
| 266 | * @param {string} [fileNameOrItem.fileName] - 文件名 | 299 | * @param {string} [fileNameOrItem.fileName] - 文件名 |
| 267 | * @param {string} [fileNameOrItem.extension] - 文件扩展名(优先使用) | 300 | * @param {string} [fileNameOrItem.extension] - 文件扩展名(优先使用) |
| 301 | + * @param {string} [fileNameOrItem.src] - 文件 URL(用于提取扩展名) | ||
| 302 | + * @param {string} [fileNameOrItem.downloadUrl] - 下载 URL(用于提取扩展名) | ||
| 268 | * @returns {boolean} 是否为视频文件 | 303 | * @returns {boolean} 是否为视频文件 |
| 269 | * | 304 | * |
| 270 | * @example | 305 | * @example |
| 271 | * isVideo('movie.mp4') // true | 306 | * isVideo('movie.mp4') // true |
| 272 | * isVideo({ extension: 'mp4' }) // true(优先使用 extension) | 307 | * isVideo({ extension: 'mp4' }) // true(优先使用 extension) |
| 308 | + * isVideo({ src: 'https://example.com/file.mp4' }) // true(从 URL 解析) | ||
| 273 | * isVideo('document.pdf') // false | 309 | * isVideo('document.pdf') // false |
| 274 | */ | 310 | */ |
| 275 | export function isVideo(fileNameOrItem) { | 311 | export function isVideo(fileNameOrItem) { | ... | ... |
-
Please register or login to post a comment