| 163 | // (so also no leading and trailing slashes - it does not distinguish |
| 164 | // relative and absolute paths) |
| 165 | function normalizeArray(parts, allowAboveRoot) { |
| 166 | // if the path tries to go above the root, `up` ends up > 0 |
| 167 | var up = 0; |
| 168 | for (var i = parts.length; i >= 0; i--) { |
| 169 | var last = parts[i]; |
| 170 | if (last == '.') { |
| 171 | parts.splice(i, 1); |
| 172 | } else if (last === '..') { |
| 173 | parts.splice(i, 1); |
| 174 | up++; |
| 175 | } else if (up) { |
| 176 | parts.splice(i, 1); |
| 177 | up--; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // if the path is allowed to go above the root, restore leading ..s |
| 182 | if (allowAboveRoot) { |
| 183 | for (; up--; up) { |
| 184 | parts.unshift('..'); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return parts; |
| 189 | } |
| 190 | |
| 191 | // Regex to split a filename into [*, dir, basename, ext] |
| 192 | // posix version |