| 1951 | // (so also no leading and trailing slashes - it does not distinguish |
| 1952 | // relative and absolute paths) |
| 1953 | function normalizeArray(parts, allowAboveRoot) { |
| 1954 | // if the path tries to go above the root, `up` ends up > 0 |
| 1955 | var up = 0; |
| 1956 | for (var i = parts.length - 1; i >= 0; i--) { |
| 1957 | var last = parts[i]; |
| 1958 | if (last === '.') { |
| 1959 | parts.splice(i, 1); |
| 1960 | } else if (last === '..') { |
| 1961 | parts.splice(i, 1); |
| 1962 | up++; |
| 1963 | } else if (up) { |
| 1964 | parts.splice(i, 1); |
| 1965 | up--; |
| 1966 | } |
| 1967 | } |
| 1968 | |
| 1969 | // if the path is allowed to go above the root, restore leading ..s |
| 1970 | if (allowAboveRoot) { |
| 1971 | for (; up--; up) { |
| 1972 | parts.unshift('..'); |
| 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | return parts; |
| 1977 | } |
| 1978 | |
| 1979 | // Split a filename into [root, dir, basename, ext], unix version |
| 1980 | // 'root' is just a slash, or nothing. |