起因
因在项目业务场景中需要根据文件后缀显示对应文件图标,因此有了以下CODE,听取了同事的建议适用 哈希Map进行键值对匹配
// 识别文件后缀返回对应的图标
export function adapterFileType(e) {
const typeObject = new Map([
['default', 'other'],
['doc', 'word'],
['docx', 'word'],
['xls', 'excel'],
['xlsx', 'excel'],
['txt', 'txt'],
['png', 'jpg'],
['jpg', 'jpg'],
['jpeg', 'jpg'],
['swf', 'jpg'],
['svg', 'jpg'],
['pdf', 'pdf'],
['ppt', 'ppt'],
['pptx', 'ppt'],
['webm', 'video'],
['mp4', 'video'],
['ogg', 'video'],
['zip', 'zip'],
['7z', 'zip'],
['rar', 'zip'],
['cab', 'zip']
])
try {
if (!e || e == undefined || e == "") throw "this's empty object"
let suffix = e.split(".")
if (suffix.length == 0) throw "The length of the corresponding filters no results"
else {
// 文件后缀
let suffix_type = suffix[suffix.length - 1].toLowerCase(),
suffix_icon;
let data = typeObject.get(suffix_type)
data = data ? data : typeObject.get("default")
return {
img: require(`@/staff-pages/static/icons/${data}.svg`)
}
}
} catch (err) {
return {
img: typeObject.get("default")
}
}
}