-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathpath.mjs
64 lines (56 loc) · 1002 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
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
/* eslint require-jsdoc:0 -- shim */
function dirname(p) {
return p.split("/").slice(0, -1).join("/") || p
}
function extname(p) {
return /\.[\w$-]+$/iu.exec(p)[0]
}
function relative(s) {
return s
}
function resolve(s) {
return s
}
function isAbsolute() {
return false
}
function join(...args) {
return args.length ? normalize(args.join("/")) : "."
}
function normalize(path) {
let 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) {
// noop
} else {
result.unshift(part)
}
}
return result.reverse().join("/")
}
const sep = "/"
const posix = {
dirname,
extname,
resolve,
relative,
sep,
isAbsolute,
join,
normalize,
}
posix.posix = posix
export {
dirname,
extname,
posix,
resolve,
relative,
sep,
isAbsolute,
join,
normalize,
}
export default posix