
はじめましてこんにちは、ひょうどうです。
今回は、ファイルの拡張子をJavaScript(TypeScript)で判断する方法をメモします。
画像・動画・音声がメインです。
コード
JavaScriptとTypeScriptそれぞれ記述してありますが、型がついているかどうかの違いだけです❗
画像
対応している拡張子は、jpg、png、gif
JavaScript
const isImageExt = (fileName) => {
fileName = fileName.substring(fileName.lastIndexOf('.'))
if (fileName.toUpperCase().match(/\.(jpg)$/i)) {
return true
} if (fileName.toUpperCase().match(/\.(jpeg)$/i)) {
return true
} if (fileName.toUpperCase().match(/\.(png)$/i)) {
return true
} if (fileName.toUpperCase().match(/\.(gif)$/i)) {
return true
}
return false
}
TypeScript
const isImageExt = (fileName: string): boolean => {
fileName = fileName.substring(fileName.lastIndexOf('.'))
if (fileName.toUpperCase().match(/\.(jpg)$/i)) {
return true
} if (fileName.toUpperCase().match(/\.(jpeg)$/i)) {
return true
} if (fileName.toUpperCase().match(/\.(png)$/i)) {
return true
} if (fileName.toUpperCase().match(/\.(gif)$/i)) {
return true
}
return false
}
動画
対応している拡張子は、mp4、avi、fiv、mov、wmv
JavaScript
const isVideoExt = (fileName) => {
fileName = fileName.substring(fileName.lastIndexOf('.'))
if (fileName.toUpperCase().match(/\.(mp4)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(avi)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(fiv)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(mov)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(wmv)$/i)) {
return true
}
return false
}
TypeScript
const isVideoExt = (fileName: string): boolean => {
fileName = fileName.substring(fileName.lastIndexOf('.'))
if (fileName.toUpperCase().match(/\.(mp4)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(avi)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(fiv)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(mov)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(wmv)$/i)) {
return true
}
return false
}
音声
対応している拡張子は、wav、mp3、wma、aac、m4a、flac、ogg
JavaScript
const isAudioExt = (fileName) => {
fileName = fileName.substring(fileName.lastIndexOf('.'))
if (fileName.toUpperCase().match(/\.(wav)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(mp3)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(wma)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(aac)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(m4a)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(flac)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(ogg)$/i)) {
return true
}
return false
}
TypeScript
const isAudioExt = (fileName: string): boolean => {
fileName = fileName.substring(fileName.lastIndexOf('.'))
if (fileName.toUpperCase().match(/\.(wav)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(mp3)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(wma)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(aac)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(m4a)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(flac)$/i)) {
return true
}
if (fileName.toUpperCase().match(/\.(ogg)$/i)) {
return true
}
return false
}
それではまたどこかでお会いしましょう!