forked from rubythonode/javascript-problems-and-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-system.js
54 lines (46 loc) · 911 Bytes
/
file-system.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
/**
* File System
*/
class FileSystem {
constructor() {
this.pathMap = new Map([['', 0]]);
}
/**
* Create a path with value
* @param {string} path
* @param {number} value
* @return {boolean}
*/
create(path, value) {
if (this.pathMap.has(path)) {
return false;
}
const lastSlashIndex = path.lastIndexOf('/');
if (!this.pathMap.has(path.substring(0, lastSlashIndex))) {
return false;
}
this.pathMap.set(path, value);
return true;
}
/**
* Returns the path value
* @param {string} path
* @return {number}
*/
get(path) {
return this.pathMap.get(path);
}
/**
* Update the path value
* @param {string} path
* @param {number} value
* @return {boolean}
*/
set(path, value) {
if (!this.pathMap.has(path)) {
return false;
}
this.pathMap.set(path, value);
}
}
export { FileSystem };