/
path.mjs
38 lines (37 loc) · 885 Bytes
/
path.mjs
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
// @ts-nocheck
export const sep = '/'
export function basename(path, ext) {
const b = (/[^\/]*$/u.exec(path) || [''])[0]
return ext && b.endsWith(ext) ? b.slice(0, -ext.length) : b
}
export function extname(path) {
return (/[^.\/]*$/u.exec(path) || [''])[0]
}
export function isAbsolute() {
return false
}
export function join(...args) {
return args.length > 0 ? normalize(args.join('/')) : '.'
}
function normalize(path) {
const result = []
for (const part of path.replace(/\/+/gu, '/').split('/')) {
if (part === '..') {
if (result[0] && result[0] !== '..' && result[0] !== '.') result.shift()
} else if (part === '.' && result.length > 0) {
// noop
} else {
result.unshift(part)
}
}
return result.reverse().join('/')
}
const posix = {
sep,
basename,
extname,
isAbsolute,
join
}
posix.posix = posix
export default posix