forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.js
More file actions
220 lines (197 loc) · 7.56 KB
/
path.js
File metadata and controls
220 lines (197 loc) · 7.56 KB
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
210
211
212
213
214
215
216
217
218
219
220
import { isDefined } from './core.js';
/**
* @namespace path
* @description File path API.
*/
const path = {
/**
* @constant
* @type {string}
* @name path.delimiter
* @description The character that separates path segments.
*/
delimiter: "/",
/**
* @function
* @name path.join
* @description Join two or more sections of file path together, inserting a
* delimiter if needed.
* @param {...string} section - Section of path to join. 2 or more can be
* provided as parameters.
* @returns {string} The joined file path.
* @example
* var path = pc.path.join('foo', 'bar');
* console.log(path); // Prints 'foo/bar'
* @example
* var path = pc.path.join('alpha', 'beta', 'gamma');
* console.log(path); // Prints 'alpha/beta/gamma'
*/
join: function () {
const num = arguments.length;
let result = arguments[0];
for (let index = 0; index < num - 1; ++index) {
const one = arguments[index];
const two = arguments[index + 1];
if (!isDefined(one) || !isDefined(two)) {
throw new Error("undefined argument to pc.path.join");
}
if (two[0] === path.delimiter) {
result = two;
continue;
}
if (one && two && one[one.length - 1] !== path.delimiter && two[0] !== path.delimiter) {
result += (path.delimiter + two);
} else {
result += (two);
}
}
return result;
},
/**
* @function
* @name path.normalize
* @description Normalize the path by removing '.' and '..' instances.
* @param {string} pathname - The path to normalize.
* @returns {string} The normalized path.
*/
normalize: function (pathname) {
const lead = pathname.startsWith(path.delimiter);
const trail = pathname.endsWith(path.delimiter);
const parts = pathname.split('/');
let result = '';
let cleaned = [];
for (let i = 0; i < parts.length; i++) {
if (parts[i] === '') continue;
if (parts[i] === '.') continue;
if (parts[i] === '..' && cleaned.length > 0) {
cleaned = cleaned.slice(0, cleaned.length - 2);
continue;
}
if (i > 0) cleaned.push(path.delimiter);
cleaned.push(parts[i]);
}
result = cleaned.join('');
if (!lead && result[0] === path.delimiter) {
result = result.slice(1);
}
if (trail && result[result.length - 1] !== path.delimiter) {
result += path.delimiter;
}
return result;
},
/**
* @function
* @name path.split
* @description Split the pathname path into a pair [head, tail] where tail is the final part of the path
* after the last delimiter and head is everything leading up to that. tail will never contain a slash.
* @param {string} pathname - The path to split.
* @returns {string[]} The split path which is an array of two strings, the path and the filename.
*/
split: function (pathname) {
const parts = pathname.split(path.delimiter);
const tail = parts.slice(parts.length - 1)[0];
const head = parts.slice(0, parts.length - 1).join(path.delimiter);
return [head, tail];
},
/**
* @function
* @name path.getBasename
* @description Return the basename of the path. That is the second element of the pair returned by
* passing path into {@link path.split}.
* @param {string} pathname - The path to process.
* @returns {string} The basename.
* @example
* pc.path.getBasename("/path/to/file.txt"); // returns "file.txt"
* pc.path.getBasename("/path/to/dir"); // returns "dir"
*/
getBasename: function (pathname) {
return path.split(pathname)[1];
},
/**
* @function
* @name path.getDirectory
* @description Get the directory name from the path. This is everything up to the final instance of {@link path.delimiter}.
* @param {string} pathname - The path to get the directory from.
* @returns {string} The directory part of the path.
*/
getDirectory: function (pathname) {
const parts = pathname.split(path.delimiter);
return parts.slice(0, parts.length - 1).join(path.delimiter);
},
/**
* @function
* @name path.getExtension
* @description Return the extension of the path. Pop the last value of a list after path is split by question mark and comma.
* @param {string} pathname - The path to process.
* @returns {string} The extension.
* @example
* pc.path.getExtension("/path/to/file.txt"); // returns ".txt"
* pc.path.getExtension("/path/to/file.jpg"); // returns ".jpg"
* pc.path.getExtension("/path/to/file.txt?function=getExtension"); // returns ".txt"
*/
getExtension: function (pathname) {
const ext = pathname.split('?')[0].split('.').pop();
if (ext !== pathname) {
return "." + ext;
}
return "";
},
/**
* @function
* @name path.isRelativePath
* @description Check if a string s is relative path.
* @param {string} pathname - The path to process.
* @returns {boolean} True if s doesn't start with slash and doesn't include colon and double slash.
* @example
* pc.path.isRelativePath("file.txt"); // returns true
* pc.path.isRelativePath("path/to/file.txt"); // returns true
* pc.path.isRelativePath("./path/to/file.txt"); // returns true
* pc.path.isRelativePath("../path/to/file.jpg"); // returns true
* pc.path.isRelativePath("/path/to/file.jpg"); // returns false
* pc.path.isRelativePath("http://path/to/file.jpg"); // returns false
*/
isRelativePath: function (pathname) {
return pathname.charAt(0) !== "/" && pathname.match(/:\/\//) === null;
},
/**
* @function
* @name path.extractPath
* @description Return the path without file name. If path is relative path, start with period.
* @param {string} pathname - The full path to process.
* @returns {string} The path without a last element from list split by slash.
* @example
* pc.path.extractPath("path/to/file.txt"); // returns "./path/to"
* pc.path.extractPath("./path/to/file.txt"); // returns "./path/to"
* pc.path.extractPath("../path/to/file.txt"); // returns "../path/to"
* pc.path.extractPath("/path/to/file.txt"); // returns "/path/to"
*/
extractPath: function (pathname) {
let result = "";
const parts = pathname.split("/");
let i = 0;
if (parts.length > 1) {
if (path.isRelativePath(pathname)) {
if (parts[0] === ".") {
for (i = 0; i < parts.length - 1; ++i) {
result += (i === 0) ? parts[i] : "/" + parts[i];
}
} else if (parts[0] === "..") {
for (i = 0; i < parts.length - 1; ++i) {
result += (i === 0) ? parts[i] : "/" + parts[i];
}
} else {
result = ".";
for (i = 0; i < parts.length - 1; ++i) {
result += "/" + parts[i];
}
}
} else {
for (i = 0; i < parts.length - 1; ++i) {
result += (i === 0) ? parts[i] : "/" + parts[i];
}
}
}
return result;
}
};
export { path };