Skip to content

Instantly share code, notes, and snippets.

@yene
Created November 23, 2018 16:06
Show Gist options
  • Save yene/ad05483a242cd05179d51e8c65ef79e1 to your computer and use it in GitHub Desktop.
Save yene/ad05483a242cd05179d51e8c65ef79e1 to your computer and use it in GitHub Desktop.
Quick and dirty functions to replicate macOS behaviour if filename is already taken.
var folderName = 'untitledfolder';
var nameTests = [
{
nr: 1,
fileSystemIn: [],
fileSystemOut: ['untitledfolder'],
},
{
nr: 2,
fileSystemIn: ['untitledfolder'],
fileSystemOut: ['untitledfolder', 'untitledfolder 2'],
},
{
nr: 3,
fileSystemIn: ['untitledfolder', 'untitledfolder 2', 'untitledfolder 4', 'untitledfolder 5'],
fileSystemOut: ['untitledfolder', 'untitledfolder 2', 'untitledfolder 3', 'untitledfolder 4', 'untitledfolder 5'],
},
{
nr: 4,
fileSystemIn: ['untitledfolder 2', 'untitledfolder 3', 'untitledfolder 4', 'untitledfolder 5'],
fileSystemOut: ['untitledfolder', 'untitledfolder 2', 'untitledfolder 3', 'untitledfolder 4', 'untitledfolder 5'],
},
{
nr: 5,
fileSystemIn: ['untitledfolder', 'untitledfolder 2', 'untitledfolder 3', 'untitledfolder 4'],
fileSystemOut: ['untitledfolder', 'untitledfolder 2', 'untitledfolder 3', 'untitledfolder 4', 'untitledfolder 5'],
},
{
nr: 6,
fileSystemIn: ['untitledfolder', 'untitledfolder 2', 'untitledfolder 3.'],
fileSystemOut: ['untitledfolder', 'untitledfolder 2', 'untitledfolder 3', 'untitledfolder 3.'],
},
{
nr: 7,
fileSystemIn: ['untitledfolder', 'untitledfolder -2'],
fileSystemOut: ['untitledfolder', 'untitledfolder -2', 'untitledfolder 2'],
}
];
nameTests.forEach(function(test) {
var inn = test.fileSystemIn;
inn.push(createNewFolder(folderName, inn));
inn.sort();
if (JSON.stringify(inn) === JSON.stringify(test.fileSystemOut)) {
console.log(test.nr, 'Test pass');
} else {
console.log(test.nr, 'Test failed', JSON.stringify(inn));
}
});
// Add an incrementing number to the end of the new name, starting from number 2.
// If there is a gap it fills in the gap.
function createNewFolder(folderName, fileSystem) {
var isPositiveInteger = function(s) {
return /^\+?[1-9][\d]*$/.test(s);
};
var newName = folderName;
while(fileSystem.includes(newName)) {
var p = newName.split(' ');
if (p.length >= 2) {
var last = p[p.length - 1]
if (isPositiveInteger(last)) {
newName = newName.substr(0, newName.length - last.length);
newName = newName + (Number(last) + 1);
continue
}
}
newName = newName + ' 2';
}
return newName;
}
a
var duplicateTest = [
{
nr: 1,
fileSystemIn: ['untitledfolder'],
fileSystemOut: ['untitledfolder', 'untitledfolder copy'],
},
{
nr: 2,
fileSystemIn: ['untitledfolder copy'],
fileSystemOut: ['untitledfolder copy', 'untitledfolder copy 2'],
},
{
nr: 3,
fileSystemIn: ['untitledfolder copy 2', 'untitledfolder copy 3'],
fileSystemOut: ['untitledfolder copy 2', 'untitledfolder copy 3', 'untitledfolder copy 4'],
},
{
nr: 4,
fileSystemIn: ['untitledfolder copy '],
fileSystemOut: ['untitledfolder copy ', 'untitledfolder copy copy'],
},
{
nr: 5,
fileSystemIn: ['untitledfolder 1 copy 100000'],
fileSystemOut: ['untitledfolder 1 copy 100000', 'untitledfolder 1 copy 100001'],
},
];
duplicateTest.forEach(function(test) {
var inn = test.fileSystemIn;
var folderName = inn[inn.length - 1];
inn.push(duplicateName(folderName, inn));
inn.sort();
if (JSON.stringify(inn) === JSON.stringify(test.fileSystemOut)) {
console.log(test.nr, 'Test pass');
} else {
console.log(test.nr, 'Test failed', JSON.stringify(inn));
}
});
// duplicateName returns a new name that is not yet in the list.
// If the name is already taken add copy to the end.
// If the name is already taken add an incrementing number at the end, starting from number 2.
// If the user duplicates a folder with the name ' copy %d' then it should increment the number.
function duplicateName(name, nameList, copyWord) {
copyWord = copyWord === undefined ? 'copy' : copyWord;
var isPositiveInteger = function(s) {
return /^\+?[1-9][\d]*$/.test(s);
};
var newName = name;
while(nameList.includes(newName)) {
if (newName.endsWith(' ' + copyWord)) {
newName = newName + ' 2';
continue;
}
var p = newName.split(' ');
if (p.length >= 3) {
var secondLast = p[p.length - 2];
var last = p[p.length - 1];
if (secondLast === copyWord && isPositiveInteger(last)) {
newName = newName.substr(0, newName.length - last.length);
newName = newName + (Number(last) + 1);
continue;
}
}
newName = newName + ' ' + copyWord;
}
return newName;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment