normalizeDirs.js
897 Bytes
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
"use strict";
function normalizeDirs(dirArray, leadingSlash)
{
var dir,i,lastOutputDir;
var len = dirArray.length;
var output = { dir:[], leadingSlash:(leadingSlash===true) };
for (i=0; i<len; i++)
{
dir = dirArray[i];
if (dir !== "..")
{
// "/dir/", "dir/"
if (dir !== ".")
{
output.dir.push(dir);
}
}
else
{
lastOutputDir = (output.dir.length>0) ? output.dir[output.dir.length-1] : null;
// "/dir/../", "/dir/dir/../"
if (lastOutputDir!==null && (output.leadingSlash===true || (lastOutputDir!==".." && lastOutputDir!==".")))
{
// Remove parent
output.dir.splice(output.dir.length-1, 1);
}
// "../../dir/", "./../dir/"
else if (output.leadingSlash === false)
{
output.dir.push(dir);
}
// Else: "/../dir/" not pushed because parent to root is root
}
}
return output;
}
module.exports = normalizeDirs;