-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.js
76 lines (71 loc) · 1.36 KB
/
path.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
const { ErrArguments } = require('./errors')
const Separator = '/'
class Path {
IsVirtual() {
return false
}
IsDir() {
return false
}
Exists() {
return false
}
Name() {
return ''
}
String() {
return ''
}
ParentPath() {
return new Path()
}
ExcludePath(p = new Path()) {
return p
}
Info() {
return new FileInfo()
}
}
class FileInfo {
IsDir = false
Size = 0
CreatedAt = 0
Permission = ''
constructor(isDir = false, size = 0, createdAt = 0, permission = '') {
if (size < 0 || createdAt < 0) {
throw ErrArguments
}
this.IsDir = isDir
this.Size = size
this.CreatedAt = createdAt
this.Permission = permission
}
ToJSON() {
return JSON.stringify({
IsDir: this.IsDir,
Size: this.Size,
CreatedAt: this.CreatedAt,
Permission: this.Permission
})
}
FromJSON(jsonString) {
try {
const parsed = JSON.parse(jsonString)
if (parsed.Size < 0 || parsed.CreatedAt < 0) {
return { fileInfo: null, error: ErrArguments }
}
this.IsDir = parsed.IsDir
this.Size = parsed.Size
this.CreatedAt = parsed.CreatedAt
this.Permission = parsed.Permission
return { fileInfo: this, error: null }
} catch (e) {
return { fileInfo: null, error: e }
}
}
}
module.exports = {
Separator,
Path,
FileInfo
}