-
-
Notifications
You must be signed in to change notification settings - Fork 687
Expand file tree
/
Copy pathurl.js
More file actions
23 lines (21 loc) · 558 Bytes
/
url.js
File metadata and controls
23 lines (21 loc) · 558 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import * as path from "./path.js";
function encodePathChars(filepath) {
return filepath
.replace(/%/g, "%25")
.replace(/\\/g, "%5C")
.replace(/\n/g, "%0A")
.replace(/\r/g, "%0D")
.replace(/\t/g, "%09");
}
export function pathToFileURL(filepath) {
let resolved = path.resolve(filepath);
if (
filepath.charCodeAt(filepath.length - 1) === /* SLASH */ 47 &&
resolved[resolved.length - 1] !== path.sep
) {
resolved += "/";
}
const url = new URL("file://");
url.pathname = encodePathChars(resolved);
return url;
}