Skip to content

Commit 76516be

Browse files
committed
refactor code to use helping functions
1 parent 56ef541 commit 76516be

File tree

2 files changed

+51
-88
lines changed

2 files changed

+51
-88
lines changed

src/contents.ts

Lines changed: 28 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { Signal, ISignal } from '@lumino/signaling';
33
import { Contents, ServerConnection } from '@jupyterlab/services';
44
import { PathExt } from '@jupyterlab/coreutils';
55

6-
import { IDriveInfo, IRegisteredFileTypes } from './token';
6+
import {
7+
extractCurrentDrive,
8+
formatPath,
9+
IDriveInfo,
10+
IRegisteredFileTypes
11+
} from './token';
712
import {
813
saveObject,
914
getContents,
@@ -206,16 +211,8 @@ export class Drive implements Contents.IDrive {
206211
localPath: string,
207212
options?: Contents.IFetchOptions
208213
): Promise<Contents.IModel> {
209-
let relativePath = '';
210214
if (localPath !== '') {
211-
// extract current drive name
212-
const currentDrive = this._drivesList.filter(
213-
x =>
214-
x.name ===
215-
(localPath.indexOf('/') !== -1
216-
? localPath.substring(0, localPath.indexOf('/'))
217-
: localPath)
218-
)[0];
215+
const currentDrive = extractCurrentDrive(localPath, this._drivesList);
219216

220217
// when accessed the first time, mount drive
221218
if (currentDrive.mounted === false) {
@@ -230,14 +227,8 @@ export class Drive implements Contents.IDrive {
230227
}
231228
}
232229

233-
// eliminate drive name from path
234-
relativePath =
235-
localPath.indexOf('/') !== -1
236-
? localPath.substring(localPath.indexOf('/') + 1)
237-
: '';
238-
239230
data = await getContents(currentDrive.name, {
240-
path: relativePath,
231+
path: formatPath(localPath),
241232
registeredFileTypes: this._registeredFileTypes
242233
});
243234
} else {
@@ -291,14 +282,7 @@ export class Drive implements Contents.IDrive {
291282
const path = options.path ?? '';
292283

293284
if (path !== '') {
294-
// extract current drive name
295-
const currentDrive = this._drivesList.filter(
296-
x =>
297-
x.name ===
298-
(path.indexOf('/') !== -1
299-
? path.substring(0, path.indexOf('/'))
300-
: path)
301-
)[0];
285+
const currentDrive = extractCurrentDrive(path, this._drivesList);
302286

303287
// eliminate drive name from path
304288
const relativePath =
@@ -396,23 +380,10 @@ export class Drive implements Contents.IDrive {
396380
*/
397381
async delete(localPath: string): Promise<void> {
398382
if (localPath !== '') {
399-
// extract current drive name
400-
const currentDrive = this._drivesList.filter(
401-
x =>
402-
x.name ===
403-
(localPath.indexOf('/') !== -1
404-
? localPath.substring(0, localPath.indexOf('/'))
405-
: localPath)
406-
)[0];
407-
408-
// eliminate drive name from path
409-
const relativePath =
410-
localPath.indexOf('/') !== -1
411-
? localPath.substring(localPath.indexOf('/') + 1)
412-
: '';
383+
const currentDrive = extractCurrentDrive(localPath, this._drivesList);
413384

414385
await deleteObjects(currentDrive.name, {
415-
path: relativePath
386+
path: formatPath(localPath)
416387
});
417388
} else {
418389
// create new element at root would mean modifying a drive
@@ -445,34 +416,27 @@ export class Drive implements Contents.IDrive {
445416
options: Contents.ICreateOptions = {}
446417
): Promise<Contents.IModel> {
447418
if (oldLocalPath !== '') {
448-
// extract current drive name
449-
const currentDrive = this._drivesList.filter(
450-
x =>
451-
x.name ===
452-
(oldLocalPath.indexOf('/') !== -1
453-
? oldLocalPath.substring(0, oldLocalPath.indexOf('/'))
454-
: oldLocalPath)
455-
)[0];
419+
const currentDrive = extractCurrentDrive(oldLocalPath, this._drivesList);
456420

457421
// eliminate drive name from path
458-
const relativePath =
459-
oldLocalPath.indexOf('/') !== -1
460-
? oldLocalPath.substring(oldLocalPath.indexOf('/') + 1)
461-
: '';
462-
const newRelativePath =
463-
newLocalPath.indexOf('/') !== -1
464-
? newLocalPath.substring(newLocalPath.indexOf('/') + 1)
465-
: '';
422+
const relativePath = formatPath(oldLocalPath);
423+
const newRelativePath = formatPath(newLocalPath);
424+
console.log('rel: ', relativePath);
425+
console.log('new: ', newRelativePath);
466426

467427
// extract new file name
468-
let newFileName = PathExt.basename(newLocalPath);
428+
let newFileName = PathExt.basename(newRelativePath);
469429

470430
try {
471431
// check if object with chosen name already exists
472432
await checkObject(currentDrive.name, {
473-
path: newLocalPath
433+
path: newRelativePath
474434
});
475-
newFileName = await this.incrementName(newLocalPath, this._name);
435+
newFileName = await this.incrementName(
436+
newRelativePath,
437+
currentDrive.name
438+
);
439+
console.log(newFileName);
476440
} catch (error) {
477441
// HEAD request failed for this file name, continue, as name doesn't already exist.
478442
} finally {
@@ -549,23 +513,10 @@ export class Drive implements Contents.IDrive {
549513
options: Partial<Contents.IModel> = {}
550514
): Promise<Contents.IModel> {
551515
if (localPath !== '') {
552-
// extract current drive name
553-
const currentDrive = this._drivesList.filter(
554-
x =>
555-
x.name ===
556-
(localPath.indexOf('/') !== -1
557-
? localPath.substring(0, localPath.indexOf('/'))
558-
: localPath)
559-
)[0];
560-
561-
// eliminate drive name from path
562-
const relativePath =
563-
localPath.indexOf('/') !== -1
564-
? localPath.substring(localPath.indexOf('/') + 1)
565-
: '';
516+
const currentDrive = extractCurrentDrive(localPath, this._drivesList);
566517

567518
data = await saveObject(currentDrive.name, {
568-
path: relativePath,
519+
path: formatPath(localPath),
569520
param: options,
570521
registeredFileTypes: this._registeredFileTypes
571522
});
@@ -636,22 +587,11 @@ export class Drive implements Contents.IDrive {
636587
options: Contents.ICreateOptions = {}
637588
): Promise<Contents.IModel> {
638589
if (path !== '') {
639-
// extract current drive name
640-
const currentDrive = this._drivesList.filter(
641-
x =>
642-
x.name ===
643-
(path.indexOf('/') !== -1
644-
? path.substring(0, path.indexOf('/'))
645-
: path)
646-
)[0];
590+
const currentDrive = extractCurrentDrive(path, this._drivesList);
647591

648592
// eliminate drive name from path
649-
const relativePath =
650-
path.indexOf('/') !== -1 ? path.substring(path.indexOf('/') + 1) : '';
651-
const toRelativePath =
652-
toDir.indexOf('/') !== -1
653-
? toDir.substring(toDir.indexOf('/') + 1)
654-
: '';
593+
const relativePath = formatPath(path);
594+
const toRelativePath = formatPath(toDir);
655595

656596
// construct new file or directory name for the copy
657597
const newFileName = await this.incrementCopyName(

src/token.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,26 @@ export function getFileType(
7878

7979
return [fileType, fileMimetype, fileFormat];
8080
}
81+
82+
/**
83+
* Helping function to extract current drive.
84+
* @param path
85+
* @param drivesList
86+
* @returns current drive
87+
*/
88+
export function extractCurrentDrive(path: string, drivesList: IDriveInfo[]) {
89+
return drivesList.filter(
90+
x =>
91+
x.name ===
92+
(path.indexOf('/') !== -1 ? path.substring(0, path.indexOf('/')) : path)
93+
)[0];
94+
}
95+
96+
/**
97+
* Helping function to eliminate drive name from path
98+
* @param path
99+
* @returns fornatted path without drive name
100+
*/
101+
export function formatPath(path: string) {
102+
return path.indexOf('/') !== -1 ? path.substring(path.indexOf('/') + 1) : '';
103+
}

0 commit comments

Comments
 (0)