Debian Codegraph @

README.md

raw symbols SCIP
1 [![npm version](https://badge.fury.io/js/url-toolkit.svg)](https://badge.fury.io/js/url-toolkit)
2
3 # URL Toolkit
4
5 Lightweight 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).
6
7 ## Differences to JS `URL()`
8
9 The 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.
10
11 That 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.
12
13 One 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.
14
15 ## Methods
16
17 ### `buildAbsoluteURL(baseURL, relativeURL, opts={})`
18
19 Build an absolute URL from a relative and base one.
20
21 ```javascript
22 URLToolkit.buildAbsoluteURL('http://a.com/b/cd', 'e/f/../g'); // => http://a.com/b/e/g
23 ```
24
25 If you want to ensure that the URL is treated as a relative one you should prefix it with `./`.
26
27 ```javascript
28 URLToolkit.buildAbsoluteURL('http://a.com/b/cd', 'a:b'); // => a:b
29 URLToolkit.buildAbsoluteURL('http://a.com/b/cd', './a:b'); // => http://a.com/b/a:b
30 ```
31
32 By 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`.
33
34 ```javascript
35 URLToolkit.buildAbsoluteURL('http://a.com/b/cd', '/e/f/../g'); // => http://a.com/e/f/../g
36 URLToolkit.buildAbsoluteURL('http://a.com/b/cd', '/e/f/../g', {
37 alwaysNormalize: true,
38 }); // => http://a.com/e/g
39 ```
40
41 ### `normalizePath(url)`
42
43 Normalizes a path.
44
45 ```javascript
46 URLToolkit.normalizePath('a/b/../c'); // => a/c
47 ```
48
49 ### `parseURL(url)`
50
51 Parse a URL into its separate components.
52
53 ```javascript
54 URLToolkit.parseURL('http://a/b/c/d;p?q#f'); // =>
55 /* {
56 scheme: 'http:',
57 netLoc: '//a',
58 path: '/b/c/d',
59 params: ';p',
60 query: '?q',
61 fragment: '#f'
62 } */
63 ```
64
65 ### `buildURLFromParts(parts)`
66
67 Puts all the parts from `parseURL()` back together into a string.
68
69 ## Example
70
71 ```javascript
72 var URLToolkit = require('url-toolkit');
73 var url = URLToolkit.buildAbsoluteURL(
74 'https://a.com/b/cd/e.m3u8?test=1#something',
75 '../z.ts?abc=1#test'
76 );
77 console.log(url); // 'https://a.com/b/z.ts?abc=1#test'
78 ```
79
80 ## Browser
81
82 This can also be used in the browser thanks to [jsDelivr](https://github.com/jsdelivr/jsdelivr):
83
84 ```html
85 <head>
86 <script
87 type="text/javascript"
88 src="https://cdn.jsdelivr.net/npm/url-toolkit@2"
89 ></script>
90 <script type="text/javascript">
91 var url = URLToolkit.buildAbsoluteURL(
92 'https://a.com/b/cd/e.m3u8?test=1#something',
93 '../z.ts?abc=1#test'
94 );
95 console.log(url); // 'https://a.com/b/z.ts?abc=1#test'
96 </script>
97 </head>
98 ```