| 1 | export function normalizeStringPosix(path, allowAboveRoot) { |
| 2 | var res = ''; |
| 3 | var lastSegmentLength = 0; |
| 4 | var lastSlash = -1; |
| 5 | var dots = 0; |
| 6 | var code; |
| 7 | for (var i = 0; i <= path.length; ++i) { |
| 8 | if (i < path.length) |
| 9 | code = path.charCodeAt(i); |
| 10 | else if (code === 47 /*/*/) |
| 11 | break; |
| 12 | else |
| 13 | code = 47 /*/*/; |
| 14 | if (code === 47 /*/*/) { |
| 15 | if (lastSlash === i - 1 || dots === 1) { |
| 16 | // NOOP |
| 17 | } else if (lastSlash !== i - 1 && dots === 2) { |
| 18 | if (res.length < 2 || lastSegmentLength !== 2 || res.charCodeAt(res.length - 1) !== 46 /*.*/ || res.charCodeAt(res.length - 2) !== 46 /*.*/) { |
| 19 | if (res.length > 2) { |
| 20 | var lastSlashIndex = res.lastIndexOf('/'); |
| 21 | if (lastSlashIndex !== res.length - 1) { |
| 22 | if (lastSlashIndex === -1) { |
| 23 | res = ''; |
| 24 | lastSegmentLength = 0; |
| 25 | } else { |
| 26 | res = res.slice(0, lastSlashIndex); |
| 27 | lastSegmentLength = res.length - 1 - res.lastIndexOf('/'); |
| 28 | } |
| 29 | lastSlash = i; |
| 30 | dots = 0; |
| 31 | continue; |
| 32 | } |
| 33 | } else if (res.length === 2 || res.length === 1) { |
| 34 | res = ''; |
| 35 | lastSegmentLength = 0; |
| 36 | lastSlash = i; |
| 37 | dots = 0; |
| 38 | continue; |
| 39 | } |
| 40 | } |
| 41 | if (allowAboveRoot) { |
| 42 | if (res.length > 0) |
| 43 | res += '/..'; |
| 44 | else |
| 45 | res = '..'; |
| 46 | lastSegmentLength = 2; |
| 47 | } |
| 48 | } else { |
| 49 | if (res.length > 0) |
| 50 | res += '/' + path.slice(lastSlash + 1, i); |
| 51 | else |
| 52 | res = path.slice(lastSlash + 1, i); |
| 53 | lastSegmentLength = i - lastSlash - 1; |
| 54 | } |
| 55 | lastSlash = i; |
| 56 | dots = 0; |
| 57 | } else if (code === 46 /*.*/ && dots !== -1) { |
| 58 | ++dots; |
| 59 | } else { |
| 60 | dots = -1; |