Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x | /**
* 任務:實作一個函式 `parseUrl`,嘗試用 URL 方法,解析網址並 return 其組成部分。
*
* 範例:
* parseUrl('https://www.example.com/path') 應該回傳
* {
* protocol: 'https:',
* hostname: 'www.example.com',
* path: '/path'
* }
*
* @param url - 一個需要被解析的 URL
* @returns - 回傳一個物件,包含 protocol、hostname 和 path
*/
interface UrlParts {
protocol: string;
hostname: string;
path: string;
}
export function parseUrl(url: string): UrlParts {
// 請在此處寫下你的程式碼
const urlObj = new URL(url);
const result = {
protocol: urlObj.protocol,
hostname: urlObj.hostname,
path: urlObj.pathname
}
return result;
}
parseUrl('https://www.example.com/path'); |