From 6609e59affaa054c4d816935c9465a22a5ecb00f Mon Sep 17 00:00:00 2001 From: Patrizio Bekerle <patrizio@bekerle.com> Date: Tue, 30 Jun 2020 07:58:08 +0200 Subject: [PATCH] Add getFileExtension and getBaseName (VPU/Apps/Signature#28) --- packages/common/utils.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packages/common/utils.js b/packages/common/utils.js index af8d2fd4..a270ffb6 100644 --- a/packages/common/utils.js +++ b/packages/common/utils.js @@ -333,3 +333,29 @@ export async function getMimeTypeOfFile(file) { fileReader.readAsArrayBuffer(file.slice(0, 4)); }); } + +/** + * Get the basename of a filename + * + * @param str + * @returns {string} + */ +export const getBaseName = (str) => { + let base = String(str).substring(str.lastIndexOf('/') + 1); + + if (base.lastIndexOf(".") !== -1) { + base = base.substring(0, base.lastIndexOf(".")); + } + + return base; +}; + +/** + * Get the file extension of a filename + * + * @param str + * @returns {string} + */ +export const getFileExtension = (str) => { + return str.split('.').pop(); +}; -- GitLab