JSDoc
(parts: string[], allowAboveRoot?: boolean)
| 24 | |
| 25 | /** JSDoc */ |
| 26 | function normalizeArray(parts: string[], allowAboveRoot?: boolean): string[] { |
| 27 | // if the path tries to go above the root, `up` ends up > 0 |
| 28 | let up = 0; |
| 29 | for (let i = parts.length - 1; i >= 0; i--) { |
| 30 | const last = parts[i]; |
| 31 | if (last === '.') { |
| 32 | parts.splice(i, 1); |
| 33 | } else if (last === '..') { |
| 34 | parts.splice(i, 1); |
| 35 | up++; |
| 36 | } else if (up) { |
| 37 | parts.splice(i, 1); |
| 38 | up--; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // if the path is allowed to go above the root, restore leading ..s |
| 43 | if (allowAboveRoot) { |
| 44 | for (; up--; up) { |
| 45 | parts.unshift('..'); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return parts; |
| 50 | } |
| 51 | |
| 52 | // Split a filename into [root, dir, basename, ext], unix version |
| 53 | // 'root' is just a slash, or nothing. |
no test coverage detected