Created
November 4, 2020 13:56
-
-
Save takumifukasawa/3b20310b8944c88d43bacaab34e1176a to your computer and use it in GitHub Desktop.
typescript: get file extention
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ファイルの拡張子を抜き出す | |
* ex) test.jpg -> jpg, test.png -> png | |
* | |
* @export | |
* @param {string} str | |
* @returns {(string | null)} | |
*/ | |
export default function getExt(str: string): string | null { | |
// if match regex, it will return [".ext", "ext"]. | |
const result = str.match(/\.([0-9a-zA-Z]+$)/i); | |
// extが大文字の場合があるので小文字に共通化して吸収する | |
// 大文字のまま扱いたい場合はないはず | |
return result ? result[1].toLowerCase() : null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment