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
|
/**
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* URI-Encodes a file path but keep the path slashes.
*
* @param {String} path path
* @returns {String} encoded path
*/
export const encodePath = path => {
if (!path) {
return path
}
const parts = path.split('/')
const result = []
for (let i = 0; i < parts.length; i++) {
result.push(encodeURIComponent(parts[i]))
}
return result.join('/')
}
/**
* Returns the base name of the given path.
* For example for "/abc/somefile.txt" it will return "somefile.txt"
*
* @param {String} path path
* @returns {String} base name
*/
export const basename = path => path.replace(/\\/g, '/').replace(/.*\//, '')
/**
* Returns the dir name of the given path.
* For example for "/abc/somefile.txt" it will return "/abc"
*
* @param {String} path path
* @returns {String} dir name
*/
export const dirname = path => path.replace(/\\/g, '/').replace(/\/[^/]*$/, '')
/**
* Returns whether the given paths are the same, without
* leading, trailing or doubled slashes and also removing
* the dot sections.
*
* @param {String} path1 first path
* @param {String} path2 second path
* @returns {bool} true if the paths are the same
*
* @since 9.0
*/
export const isSamePath = (path1, path2) => {
const pathSections1 = (path1 || '').split('/').filter(p => p !== '.')
const pathSections2 = (path2 || '').split('/').filter(p => p !== '.')
path1 = joinPaths.apply(undefined, pathSections1)
path2 = joinPaths.apply(undefined, pathSections2)
return path1 === path2
}
/**
* Join path sections
*
* @param {...String} path sections
*
* @returns {String} joined path, any leading or trailing slash
* will be kept
*
* @since 8.2
*/
export const joinPaths = (...args) => {
if (args.length < 1) {
return ''
}
// discard empty arguments
const nonEmptyArgs = args.filter(arg => arg.length > 0)
if (nonEmptyArgs.length < 1) {
return ''
}
const lastArg = nonEmptyArgs[nonEmptyArgs.length - 1]
const leadingSlash = nonEmptyArgs[0].charAt(0) === '/'
const trailingSlash = lastArg.charAt(lastArg.length - 1) === '/'
const sections = nonEmptyArgs.reduce((acc, section) => acc.concat(section.split('/')), [])
let first = !leadingSlash
const path = sections.reduce((acc, section) => {
if (section === '') {
return acc
}
if (first) {
first = false
return acc + section
}
return acc + '/' + section
}, '')
if (trailingSlash) {
// add it back
return path + '/'
}
return path
}
|