This repository was archived by the owner on Mar 17, 2022. It is now read-only.
forked from webpack/memory-fs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalize.js
More file actions
38 lines (34 loc) · 1.73 KB
/
normalize.js
File metadata and controls
38 lines (34 loc) · 1.73 KB
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
var doubleSlashWinRegExp = /\\+/g;
var doubleSlashNixRegExp = /\/+/g;
var currentDirectoryWinMiddleRegExp = /\\(\.\\)+/;
var currentDirectoryWinEndRegExp = /\\\.$/;
var parentDirectoryWinMiddleRegExp = /\\+[^\\]+\\+\.\.\\/;
var parentDirectoryWinEndRegExp1 = /([A-Z]:\\)\\*[^\\]+\\+\.\.$/i;
var parentDirectoryWinEndRegExp2 = /\\+[^\\]+\\+\.\.$/;
var currentDirectoryNixMiddleRegExp = /\/+(\.\/)+/;
var currentDirectoryNixEndRegExp1 = /^\/+\.$/;
var currentDirectoryNixEndRegExp2 = /\/+\.$/;
var parentDirectoryNixMiddleRegExp = /(^|\/[^\/]+)\/+\.\.\/+/;
var parentDirectoryNixEndRegExp1 = /^\/[^\/]+\/+\.\.$/;
var parentDirectoryNixEndRegExp2 = /\/+[^\/]+\/+\.\.$/;
var parentDirectoryNixEndRegExp3 = /^\/+\.\.$/;
// RegExp magic :)
module.exports = function normalize(path) {
while(currentDirectoryWinMiddleRegExp.test(path))
path = path.replace(currentDirectoryWinMiddleRegExp, "\\");
path = path.replace(currentDirectoryWinEndRegExp, "");
while(parentDirectoryWinMiddleRegExp.test(path))
path = path.replace(parentDirectoryWinMiddleRegExp, "\\");
path = path.replace(parentDirectoryWinEndRegExp1, "$1");
path = path.replace(parentDirectoryWinEndRegExp2, "");
while(currentDirectoryNixMiddleRegExp.test(path))
path = path.replace(currentDirectoryNixMiddleRegExp, "/");
path = path.replace(currentDirectoryNixEndRegExp1, "/");
path = path.replace(currentDirectoryNixEndRegExp2, "");
while(parentDirectoryNixMiddleRegExp.test(path))
path = path.replace(parentDirectoryNixMiddleRegExp, "/");
path = path.replace(parentDirectoryNixEndRegExp1, "/");
path = path.replace(parentDirectoryNixEndRegExp2, "");
path = path.replace(parentDirectoryNixEndRegExp3, "/");
return path.replace(doubleSlashWinRegExp, "\\").replace(doubleSlashNixRegExp, "/");
};