From 1a93e73a5bc1e4cb66e0482c6ef9963443748574 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Wed, 20 Apr 2022 19:04:41 +0530 Subject: [PATCH] remove usage of depreciated substr --- src/components/pages/gallery/Upload.tsx | 22 ++++++++++++---------- src/utils/file/index.ts | 14 +++++++------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/components/pages/gallery/Upload.tsx b/src/components/pages/gallery/Upload.tsx index 0b7def229..99d8a4771 100644 --- a/src/components/pages/gallery/Upload.tsx +++ b/src/components/pages/gallery/Upload.tsx @@ -187,19 +187,21 @@ export default function Upload(props: Props) { paths.sort((path1, path2) => getCharCount(path1) - getCharCount(path2)); const firstPath = paths[0]; const lastPath = paths[paths.length - 1]; + const L = firstPath.length; let i = 0; - const firstFileFolder = firstPath.substr(0, firstPath.lastIndexOf('/')); - const lastFileFolder = lastPath.substr(0, lastPath.lastIndexOf('/')); + const firstFileFolder = firstPath.slice(0, firstPath.lastIndexOf('/')); + const lastFileFolder = lastPath.slice(0, lastPath.lastIndexOf('/')); while (i < L && firstPath.charAt(i) === lastPath.charAt(i)) i++; - let commonPathPrefix = firstPath.substring(0, i); + let commonPathPrefix = firstPath.slice(0, i); + if (commonPathPrefix) { - commonPathPrefix = commonPathPrefix.substr( - 1, - commonPathPrefix.lastIndexOf('/') - 1 + commonPathPrefix = commonPathPrefix.slice( + 0, + commonPathPrefix.lastIndexOf('/') ); if (commonPathPrefix) { - commonPathPrefix = commonPathPrefix.substr( + commonPathPrefix = commonPathPrefix.slice( commonPathPrefix.lastIndexOf('/') + 1 ); } @@ -214,11 +216,11 @@ export default function Upload(props: Props) { for (const file of toUploadFiles.current) { const filePath = file['path'] as string; - let folderPath = filePath.substr(0, filePath.lastIndexOf('/')); + let folderPath = filePath.slice(0, filePath.lastIndexOf('/')); if (folderPath.endsWith(METADATA_FOLDER_NAME)) { - folderPath = folderPath.substr(0, folderPath.lastIndexOf('/')); + folderPath = folderPath.slice(0, folderPath.lastIndexOf('/')); } - const folderName = folderPath.substr( + const folderName = folderPath.slice( folderPath.lastIndexOf('/') + 1 ); if (!collectionWiseFiles.has(folderName)) { diff --git a/src/utils/file/index.ts b/src/utils/file/index.ts index 502e49c71..d49053040 100644 --- a/src/utils/file/index.ts +++ b/src/utils/file/index.ts @@ -298,25 +298,25 @@ export const preservePhotoswipeProps = return fileWithPreservedProperty; }; -export function fileNameWithoutExtension(filename) { +export function fileNameWithoutExtension(filename: string) { const lastDotPosition = filename.lastIndexOf('.'); if (lastDotPosition === -1) return filename; - else return filename.substr(0, lastDotPosition); + else return filename.slice(0, lastDotPosition); } -export function fileExtensionWithDot(filename) { +export function fileExtensionWithDot(filename: string) { const lastDotPosition = filename.lastIndexOf('.'); if (lastDotPosition === -1) return ''; - else return filename.substr(lastDotPosition); + else return filename.slice(lastDotPosition); } -export function splitFilenameAndExtension(filename): [string, string] { +export function splitFilenameAndExtension(filename: string): [string, string] { const lastDotPosition = filename.lastIndexOf('.'); if (lastDotPosition === -1) return [filename, null]; else return [ - filename.substr(0, lastDotPosition), - filename.substr(lastDotPosition + 1), + filename.slice(0, lastDotPosition), + filename.slice(lastDotPosition + 1), ]; }