Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Commit 7fd349a

Browse files
committed
Merge branch 'master' into feature/upgrade_to_es6
2 parents 4a159c2 + 79a02a6 commit 7fd349a

File tree

3 files changed

+106
-38
lines changed

3 files changed

+106
-38
lines changed

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright JS Foundation and other contributors
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
'Software'), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

lib/normalize.js

Lines changed: 85 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,86 @@
1-
const doubleSlashWinRegExp = /\\+/g;
2-
const doubleSlashNixRegExp = /\/+/g;
3-
const currentDirectoryWinMiddleRegExp = /\\(\.\\)+/;
4-
const currentDirectoryWinEndRegExp = /\\\.$/;
5-
const parentDirectoryWinMiddleRegExp = /\\+[^\\]+\\+\.\.\\/;
6-
const parentDirectoryWinEndRegExp1 = /([A-Z]:\\)\\*[^\\]+\\+\.\.$/i;
7-
const parentDirectoryWinEndRegExp2 = /\\+[^\\]+\\+\.\.$/;
8-
const currentDirectoryNixMiddleRegExp = /\/+(\.\/)+/;
9-
const currentDirectoryNixEndRegExp1 = /^\/+\.$/;
10-
const currentDirectoryNixEndRegExp2 = /\/+\.$/;
11-
const parentDirectoryNixMiddleRegExp = /(^|\/[^\/]+)\/+\.\.\/+/;
12-
const parentDirectoryNixEndRegExp1 = /^\/[^\/]+\/+\.\.$/;
13-
const parentDirectoryNixEndRegExp2 = /\/+[^\/]+\/+\.\.$/;
14-
const parentDirectoryNixEndRegExp3 = /^\/+\.\.$/;
15-
16-
// RegExp magic :)
17-
181
module.exports = function normalize(path) {
19-
while(currentDirectoryWinMiddleRegExp.test(path))
20-
path = path.replace(currentDirectoryWinMiddleRegExp, "\\");
21-
path = path.replace(currentDirectoryWinEndRegExp, "");
22-
while(parentDirectoryWinMiddleRegExp.test(path))
23-
path = path.replace(parentDirectoryWinMiddleRegExp, "\\");
24-
path = path.replace(parentDirectoryWinEndRegExp1, "$1");
25-
path = path.replace(parentDirectoryWinEndRegExp2, "");
26-
27-
while(currentDirectoryNixMiddleRegExp.test(path))
28-
path = path.replace(currentDirectoryNixMiddleRegExp, "/");
29-
path = path.replace(currentDirectoryNixEndRegExp1, "/");
30-
path = path.replace(currentDirectoryNixEndRegExp2, "");
31-
while(parentDirectoryNixMiddleRegExp.test(path))
32-
path = path.replace(parentDirectoryNixMiddleRegExp, "/");
33-
path = path.replace(parentDirectoryNixEndRegExp1, "/");
34-
path = path.replace(parentDirectoryNixEndRegExp2, "");
35-
path = path.replace(parentDirectoryNixEndRegExp3, "/");
36-
37-
return path.replace(doubleSlashWinRegExp, "\\").replace(doubleSlashNixRegExp, "/");
38-
};
2+
var parts = path.split(/(\\+|\/+)/);
3+
if(parts.length === 1)
4+
return path;
5+
var result = [];
6+
var absolutePathStart = 0;
7+
for(var i = 0, sep = false; i < parts.length; i++, sep = !sep) {
8+
var part = parts[i];
9+
if(i === 0 && /^([A-Z]:)?$/i.test(part)) {
10+
result.push(part);
11+
absolutePathStart = 2;
12+
} else if(sep) {
13+
result.push(part[0]);
14+
} else if(part === "..") {
15+
switch(result.length) {
16+
case 0:
17+
// i. e. ".." => ".."
18+
// i. e. "../a/b/c" => "../a/b/c"
19+
result.push(part);
20+
break;
21+
case 2:
22+
// i. e. "a/.." => ""
23+
// i. e. "/.." => "/"
24+
// i. e. "C:\.." => "C:\"
25+
// i. e. "a/../b/c" => "b/c"
26+
// i. e. "/../b/c" => "/b/c"
27+
// i. e. "C:\..\a\b\c" => "C:\a\b\c"
28+
i++;
29+
sep = !sep;
30+
result.length = absolutePathStart;
31+
break;
32+
case 4:
33+
// i. e. "a/b/.." => "a"
34+
// i. e. "/a/.." => "/"
35+
// i. e. "C:\a\.." => "C:\"
36+
// i. e. "/a/../b/c" => "/b/c"
37+
if(absolutePathStart === 0) {
38+
result.length -= 3;
39+
} else {
40+
i++;
41+
sep = !sep;
42+
result.length = 2;
43+
}
44+
break;
45+
default:
46+
// i. e. "/a/b/.." => "/a"
47+
// i. e. "/a/b/../c" => "/a/c"
48+
result.length -= 3;
49+
break;
50+
}
51+
} else if(part === ".") {
52+
switch(result.length) {
53+
case 0:
54+
// i. e. "." => "."
55+
// i. e. "./a/b/c" => "./a/b/c"
56+
result.push(part);
57+
break;
58+
case 2:
59+
// i. e. "a/." => "a"
60+
// i. e. "/." => "/"
61+
// i. e. "C:\." => "C:\"
62+
// i. e. "C:\.\a\b\c" => "C:\a\b\c"
63+
if(absolutePathStart === 0) {
64+
result.length--;
65+
} else {
66+
i++;
67+
sep = !sep;
68+
}
69+
break;
70+
default:
71+
// i. e. "a/b/." => "a/b"
72+
// i. e. "/a/." => "/"
73+
// i. e. "C:\a\." => "C:\"
74+
// i. e. "a/./b/c" => "a/b/c"
75+
// i. e. "/a/./b/c" => "/a/b/c"
76+
result.length--;
77+
break;
78+
}
79+
} else if(part) {
80+
result.push(part);
81+
}
82+
}
83+
if(result.length === 1 && /^[A-Za-z]:$/.test(result))
84+
return result[0] + "\\";
85+
return result.join("");
86+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "memory-fs",
3-
"version": "0.3.0",
3+
"version": "0.4.1",
44
"description": "A simple in-memory filesystem. Holds data in a javascript object.",
55
"main": "lib/MemoryFileSystem.js",
66
"directories": {

0 commit comments

Comments
 (0)