-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
209 lines (186 loc) · 4.65 KB
/
index.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const pathModule = require('path')
const extensionsToType = require('./extensionsToType')
module.exports = class Path {
constructor (pathObject) {
const argType = typeof pathObject
if (argType === 'undefined') {
return
}
else if (argType !== 'object') {
throw new Error(`Argument must be an object and not "${argType}"`)
}
const object = Object.assign({}, pathObject)
delete object.path
delete object.fileName
if (object.directoryPath) {
delete object.root
delete object.directoryName
}
if (object.extensions) {
delete object.extension
}
if (object.fileRoot) {
if (object.extensions) {
delete object.baseName
}
}
else if (!object.baseName) {
throw new Error('Not enough data to create a path')
}
Object.assign(this, pathObject)
}
static fromString (pathString) {
const pathInstance = new Path()
pathInstance.path = pathString
return pathInstance
}
set path (pathString) {
this.directoryPath = pathModule.dirname(pathString)
this.fileName = pathModule.basename(pathString)
}
setPath (pathString) {
this.path = pathString
return this
}
get path () {
return pathModule.join(this.directoryPath, this.fileName)
}
set directoryPath (directoryPath) {
const nativePathObject = pathModule.parse(directoryPath)
this._isAbsolute = pathModule.isAbsolute(directoryPath)
this._root = nativePathObject.root
this._grandParentDirectory = nativePathObject.dir
this._directoryName = nativePathObject.base
}
setDirectoryPath (directoryPath) {
this.directoryPath = directoryPath
return this
}
get directoryPath () {
return pathModule.join(
this.root,
this._grandParentDirectory || '',
this.directoryName
)
}
set root (root) {
this._root = root
}
setRoot (root) {
this._root = root
return this
}
get root () {
return this._root || ''
}
set directoryName (directoryName) {
this._directoryName = directoryName
}
setDirectoryName (directoryName) {
this._directoryName = directoryName
return this
}
get directoryName () {
return this._directoryName || ''
}
set fileName (fileName) {
this._isDotfile = fileName[0] === '.'
this._extension = pathModule
.extname(fileName)
.slice(1)
this._baseName = pathModule.basename(
fileName,
`.${this._extension}`
)
this._fileRoot = this._baseName.split('.')[0]
this._extensions = fileName
.slice(this._fileRoot.length + 1)
.split('.')
}
setFileName (fileName) {
this.fileName = fileName
return this
}
get fileName () {
if (this.fileRoot) {
return [this.fileRoot]
.concat(this.extensions)
.join('.')
}
return [this.baseName, this.extension].join('.')
}
set baseName (baseName) {
this._baseName = baseName
this._fileRoot = baseName.split('.')[0]
const extString = baseName.slice(this.fileRoot.length + 1)
this._extensions = extString
? extString.split('.')
: []
this._extensions.push(this.extension)
}
setBaseName (baseName) {
this.baseName = baseName
return this
}
get baseName () {
return [this.fileRoot]
.concat(this.extensions.slice(0, -1))
.join('.')
}
set extension (extension) {
this._extension = extension
if (Array.isArray(this._extensions)) this._extensions.pop()
else this._extensions = []
this._extensions.push(extension)
}
setExtension (extension) {
this.extension = extension
return this
}
get extension () {
return this._extension || ''
}
set fileRoot (fileRoot) {
this._fileRoot = fileRoot
}
setFileRoot (fileRoot) {
this._fileRoot = fileRoot
return this
}
get fileRoot () {
return this._fileRoot || ''
}
set extensions (extensions) {
this._extensions = extensions
this._basename = [this._fileRoot]
.concat(extensions.slice(0, -1))
.join('.')
this._extension = extensions.slice(-1)[0]
}
setExtensions (extensions) {
this.extensions = extensions
return this
}
get extensions () {
return this._extensions || []
}
get fileType () {
return extensionsToType(this.extensions)
}
toString () {
return this.path
}
get toObject () {
const {
path, root, directoryPath, directoryName, fileName, baseName,
fileRoot, extensions, extension, fileType, isDotfile, isAbsolute,
} = this
return {
path, root, directoryPath, directoryName, fileName, baseName,
fileRoot, extensions, extension, fileType, isDotfile, isAbsolute,
}
}
toJSON () {
return this.toObject
}
}