Skip to content

Commit

Permalink
feat[#542]: add more flexible logic into duplicate creator function t…
Browse files Browse the repository at this point in the history
…o avoid infinite copy creation
  • Loading branch information
sergiozeppo committed Sep 25, 2024
1 parent 976e37c commit 5f57155
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions frontend/src/utils/genDuplicateSnippetName.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@ import { SNIPPET_NAME_MAX_LENGTH } from './validationSchemas';

const genDuplicateSnippetName = (fileName) => {
const extensionIndex = fileName.lastIndexOf('.');
const copyTextIndex = fileName.lastIndexOf('-copy');
const dashIndex = fileName.lastIndexOf('_');
const existCopyNumber = Number(fileName.slice(dashIndex + 1));
console.log(copyTextIndex, dashIndex, existCopyNumber, extensionIndex);

let finalName = '';
const extension = extensionIndex !== -1 ? fileName.slice(extensionIndex) : '';

if (extensionIndex === -1) {
finalName = `${fileName}-copy`;
} else {
const [name, extension] = [
fileName.slice(0, extensionIndex),
fileName.slice(extensionIndex + 1),
];

finalName = `${name}-copy.${extension}`;
}
if (copyTextIndex !== -1) {
if (dashIndex > copyTextIndex) {
finalName = `${fileName.slice(0, dashIndex)}_${
existCopyNumber + 1
}${extension}`;
} else if (extensionIndex !== -1) {
finalName = `${fileName.slice(0, extensionIndex)}_${1}${extension}`;
} else {
finalName = `${fileName}_${1}${extension}`;
}
} else finalName = `${fileName}-copy${extension}`;

if (finalName.length >= SNIPPET_NAME_MAX_LENGTH) {
return finalName.slice(finalName.length - SNIPPET_NAME_MAX_LENGTH);
Expand Down

0 comments on commit 5f57155

Please sign in to comment.