-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw-1.js
140 lines (117 loc) · 4.26 KB
/
hw-1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// NOTE: Folder icinde Folder olmayacak
// Data
const folders = [
{
id: 6,
name: 'Klasör 2',
files: [
{ id: 21, name: 'foto.png' },
{ id: 22, name: 'dosya.xls' },
],
},
{
id: 7,
name: 'Klasör 3',
},
{
id: 5,
name: 'Klasör 1',
files: [
{ id: 17, name: 'profil.jpg' },
{ id: 18, name: 'manzara.jpg' },
],
},
];
// Ortak kullanilan fonksiyonlar bir Object altina toplandi.
const utils = {
// Verilen klasor ID'si uzerinden klasorun indeksini doner.
getFolderIndex: (folderID) => {
return folders.findIndex((item) => item.id === folderID);
},
// Verilen dosya ID'si uzerinden ebeveyn klasorun ve dosyanin indeksini doner.
getParentFolderAndFileIndex: (fileID) => {
const parentFolderIndex = folders.findIndex((item) => {
if (item.files && item.files.length > 0) {
return item.files.find((file) => file.id === fileID);
}
});
const fileIndex = folders[parentFolderIndex]
? folders[parentFolderIndex].files.findIndex((item) => item.id === fileID)
: -1;
return { parentFolderIndex, fileIndex };
},
};
// Move ve Copy fonksiyonlari %95 ayni islemleri yaptigi icin tek fonksiyon altina toplandi.
function moveOrCopyFile(type = 'move', fileID, folderID) {
// Degiskenler tanimlaniyor ve indeksler aliniyor.
const { parentFolderIndex, fileIndex } =
utils.getParentFolderAndFileIndex(fileID);
const destFolderIndex = utils.getFolderIndex(folderID);
// Dogru ID kontrolu yapiliyor
if (fileIndex === -1 || destFolderIndex === -1)
return 'Verilen dosya veya klasor ID numarasi hatali';
// Ebeveyn klasor ile Tasima/kopyalama yapilacak klasor ayniysa hata mesaji donuluyor
if (parentFolderIndex === destFolderIndex)
return 'Ayni klasor icerisinde islem yapilamaz';
// Islemin turune gore splice veya slice kullanilarak dosya aliniyor.
const file =
type === 'move'
? folders[parentFolderIndex].files.splice(fileIndex, 1)
: folders[parentFolderIndex].files.slice(fileIndex, fileIndex + 1);
// Dosya, hedef klasor icerisine gonderiliyor.
// Eger klasorde files bulunmuyorsa yeni files olusturuluyor
folders[destFolderIndex]['files']
? folders[destFolderIndex]['files'].push(file[0])
: (folders[destFolderIndex]['files'] = [file[0]]);
// Islemin tamamlandigina dair mesaji donuluyor
return `${fileID} ID numarali dosya, ${
folders[destFolderIndex].id
} ID numarali klasore ${type === 'move' ? 'tasindi' : 'kopyalandi'}.`;
}
// TODO: move fonksiyonu, girilen dosyayi girilen klasore tasiyacak
function move(fileID, folderID) {
return moveOrCopyFile('move', fileID, folderID);
}
// TODO: copy fonksiyonu, girilen dosyayi girilen klasore kopyalayacak
function copy(fileID, folderID) {
return moveOrCopyFile('copy', fileID, folderID);
}
// TODO: remove fonksiyonu, girilen dosyayi silecek.
function remove(fileID) {
// Degiskenler tanimlaniyor ve indeksler aliniyor.
const { parentFolderIndex, fileIndex } =
utils.getParentFolderAndFileIndex(fileID);
// Dogru ID kontrolu yapiliyor
if (fileIndex === -1) return 'Verilen dosya ID numarasi hatali';
// Dosya splice ile klasorden siliniyor
folders[parentFolderIndex].files.splice(fileIndex, 1);
// Islemin tamamlandigina dair mesaji donuluyor
return `${fileID} ID numarali dosya silinmistir.`;
}
// TODO: removeFolder fonksiyonu, girilen klasoru butun icerigiyle silecek.
function removeFolder(folderID) {
// Degisken tanimlaniyor ve indeks aliniyor.
const folderIndex = utils.getFolderIndex(folderID);
// Dogru ID kontrolu yapiliyor
if (folderIndex === -1) return 'Verilen klasor ID numarasi hatali';
// Klasor siliniyor
folders.splice(folderIndex, 1);
// Islemin tamamlandigina dair mesaji donuluyor
return `${folderID}, ID numarali klasor silindi.`;
}
// TODO: parentFolderOf fonksiyonu, girilen dosyanin icerisinde bulundugu klasorun id'sini donecek.
function parentFolderOf(fileID) {
// Degiskenler tanimlaniyor ve indeksler aliniyor.
const { parentFolderIndex, fileIndex } =
utils.getParentFolderAndFileIndex(fileID);
// Dogru ID kontrolu yapiliyor
if (fileIndex === -1) return 'Verilen dosya ID numarasi hatali';
// Klasor id'si donuluyor
return `Klasor ID: ${folders[parentFolderIndex]['id']}`;
}
// Fonksiyon Cagrilari
// console.log(move(17, 7));
// console.log(copy(17, 6));
// console.log(remove(17));
// console.log(removeFolder(6));
// console.log(parentFolderOf(17));