-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (44 loc) · 1.42 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
'use strict';
exports.resolve = function resolve() {
return Array.prototype.slice
.apply(arguments)
.reduce(function(acc, curr) {
if (curr.charAt(0) === '/') acc = [''];
return curr.split('/').reduce(function(acc, curr) {
if (curr === '') return acc;
if (curr === '.' && acc.length) return acc;
if (curr === '..' && acc.length) {
if (acc.length === 1 && !acc[0]) return acc;
if (acc[acc.length - 1] !== '..') {
return acc.slice(0, acc.length - 1);
}
}
return acc.concat(curr);
}, acc);
}, [])
.join('/');
};
exports.join = function join() {
return Array.prototype.join.call(arguments, '/').replace(/\/+/g, '/');
};
exports.dedupeSlashes = function dedupeSlashes(path) {
return path.replace(/\/+/g, '/');
};
exports.trimSlashes = function trimSlashes(path) {
return path.replace(/^\/*(.*?)\/*$/, '$1');
};
exports.ensureSlashes = function ensureSlashes(path) {
return path.replace(/^\/*(.*?)\/*$/, '/$1/');
};
exports.trimLeadingSlash = function trimLeadingSlash(path) {
return path.replace(/^\/+/, '');
};
exports.trimTrailingSlash = function trimTrailingSlash(path) {
return path.replace(/\/+$/, '');
};
exports.ensureLeadingSlash = function ensureLeadingSlash(path) {
return path.replace(/^\/*/, '/');
};
exports.ensureTrailingSlash = function ensureTrailingSlash(path) {
return path.replace(/\/*$/, '/');
};