* Removes everything after (and including) the first single slash (/) in a string, * but skips over '//' (double slashes), which are used in protocols like 'http://'. * This is primarily used to extract the base URL (protocol + host) from a full URL. * * Example: * removeAfterFirstSlash("htt
(inputString: string)
| 13 | * removeAfterFirstSlash("https://example.com") // "https://example.com" |
| 14 | */ |
| 15 | function removeAfterFirstSlash(inputString: string): string { |
| 16 | let i = 0; |
| 17 | const strLen = inputString.length; |
| 18 | while (i < strLen) { |
| 19 | const char = inputString[i]; |
| 20 | if (char === '/') { |
| 21 | // Check for double slash (e.g., 'http://') |
| 22 | if (i + 1 < strLen && inputString[i + 1] === '/') { |
| 23 | i += 2; |
| 24 | continue; |
| 25 | } else { |
| 26 | // Single slash: trim here |
| 27 | return inputString.substring(0, i); |
| 28 | } |
| 29 | } |
| 30 | i++; |
| 31 | } |
| 32 | // No single slash found (or only part of protocol), return the whole input |
| 33 | return inputString; |
| 34 | } |
| 35 | |
| 36 | function relativePath(targetPath: string, goback: number = 0): string { |
| 37 | const levels = Array(Math.max(0, goback - 1)).fill('..'); |