{"id":16487403,"url":"https://github.com/tjenkinson/url-toolkit","last_synced_at":"2025-04-04T20:14:56.954Z","repository":{"id":13157389,"uuid":"73744538","full_name":"tjenkinson/url-toolkit","owner":"tjenkinson","description":"Build an absolute URL from a base URL and a relative URL (RFC 1808).","archived":false,"fork":false,"pushed_at":"2024-10-07T18:14:00.000Z","size":1343,"stargazers_count":31,"open_issues_count":2,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-10-12T13:34:03.827Z","etag":null,"topics":["absolute-url","jsdelivr","normalize","relative-url","rfc-1808","types","typescript","url","url-parsing","urls"],"latest_commit_sha":null,"homepage":"https://tools.ietf.org/html/rfc1808","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tjenkinson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-11-14T20:33:00.000Z","updated_at":"2024-10-03T18:14:41.000Z","dependencies_parsed_at":"2023-02-10T05:30:46.619Z","dependency_job_id":"9bc37117-f5f7-41e2-80b0-76a40807d040","html_url":"https://github.com/tjenkinson/url-toolkit","commit_stats":{"total_commits":198,"total_committers":8,"mean_commits":24.75,"dds":0.5151515151515151,"last_synced_commit":"41a815755e90b9c449d61e46f7365577fa5bc429"},"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tjenkinson%2Furl-toolkit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tjenkinson%2Furl-toolkit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tjenkinson%2Furl-toolkit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tjenkinson%2Furl-toolkit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tjenkinson","download_url":"https://codeload.github.com/tjenkinson/url-toolkit/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247242681,"owners_count":20907134,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["absolute-url","jsdelivr","normalize","relative-url","rfc-1808","types","typescript","url","url-parsing","urls"],"created_at":"2024-10-11T13:34:06.876Z","updated_at":"2025-04-04T20:14:56.938Z","avatar_url":"https://github.com/tjenkinson.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![npm version](https://badge.fury.io/js/url-toolkit.svg)](https://badge.fury.io/js/url-toolkit)\n\n# URL Toolkit\n\nLightweight library to build an absolute URL from a base URL and a relative URL, written from [the spec (RFC 1808)](https://tools.ietf.org/html/rfc1808). Initially part of [HLS.JS](https://github.com/video-dev/hls.js).\n\n## Differences to JS `URL()`\n\nThe JS [URL()](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) function also lets you calculate a new URL from a base and relative one.\n\nThat uses the [URL Living Standard](https://url.spec.whatwg.org/) which is slightly different to [RFC 1808](https://tools.ietf.org/html/rfc1808) that this library implements.\n\nOne of the key differences is that the [URL Living Standard](https://url.spec.whatwg.org/) has the concept of a ['special url'](https://url.spec.whatwg.org/#is-special) and ['special scheme'](https://url.spec.whatwg.org/#special-scheme). For these special URL's, such as a URL with the `http` scheme, they normalise them in a way that results in `http:///example.com/something` becoming `http://example.com/something`. This library does not do that and [`parseURL()`](#parseurlurl) would give you `//` as the `netLoc` and `/example.com` as the path.\n\n## Methods\n\n### `buildAbsoluteURL(baseURL, relativeURL, opts={})`\n\nBuild an absolute URL from a relative and base one.\n\n```javascript\nURLToolkit.buildAbsoluteURL('http://a.com/b/cd', 'e/f/../g'); // =\u003e http://a.com/b/e/g\n```\n\nIf you want to ensure that the URL is treated as a relative one you should prefix it with `./`.\n\n```javascript\nURLToolkit.buildAbsoluteURL('http://a.com/b/cd', 'a:b'); // =\u003e a:b\nURLToolkit.buildAbsoluteURL('http://a.com/b/cd', './a:b'); // =\u003e http://a.com/b/a:b\n```\n\nBy default the paths will not be normalized unless necessary, according to the spec. However you can ensure paths are always normalized by setting the `opts.alwaysNormalize` option to `true`.\n\n```javascript\nURLToolkit.buildAbsoluteURL('http://a.com/b/cd', '/e/f/../g'); // =\u003e http://a.com/e/f/../g\nURLToolkit.buildAbsoluteURL('http://a.com/b/cd', '/e/f/../g', {\n  alwaysNormalize: true,\n}); // =\u003e http://a.com/e/g\n```\n\n### `normalizePath(url)`\n\nNormalizes a path.\n\n```javascript\nURLToolkit.normalizePath('a/b/../c'); // =\u003e a/c\n```\n\n### `parseURL(url)`\n\nParse a URL into its separate components.\n\n```javascript\nURLToolkit.parseURL('http://a/b/c/d;p?q#f'); // =\u003e\n/* {\n\tscheme: 'http:',\n\tnetLoc: '//a',\n\tpath: '/b/c/d',\n\tparams: ';p',\n\tquery: '?q',\n\tfragment: '#f'\n} */\n```\n\n### `buildURLFromParts(parts)`\n\nPuts all the parts from `parseURL()` back together into a string.\n\n## Example\n\n```javascript\nvar URLToolkit = require('url-toolkit');\nvar url = URLToolkit.buildAbsoluteURL(\n  'https://a.com/b/cd/e.m3u8?test=1#something',\n  '../z.ts?abc=1#test',\n);\nconsole.log(url); // 'https://a.com/b/z.ts?abc=1#test'\n```\n\n## Browser\n\nThis can also be used in the browser thanks to [jsDelivr](https://github.com/jsdelivr/jsdelivr):\n\n```html\n\u003chead\u003e\n  \u003cscript\n    type=\"text/javascript\"\n    src=\"https://cdn.jsdelivr.net/npm/url-toolkit@2\"\n  \u003e\u003c/script\u003e\n  \u003cscript type=\"text/javascript\"\u003e\n    var url = URLToolkit.buildAbsoluteURL(\n      'https://a.com/b/cd/e.m3u8?test=1#something',\n      '../z.ts?abc=1#test',\n    );\n    console.log(url); // 'https://a.com/b/z.ts?abc=1#test'\n  \u003c/script\u003e\n\u003c/head\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftjenkinson%2Furl-toolkit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftjenkinson%2Furl-toolkit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftjenkinson%2Furl-toolkit/lists"}