Skip to content

Commit 9018a07

Browse files
committed
Expanded docs
1 parent d702668 commit 9018a07

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

README.md

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,49 @@ URL Tools is a helper library whose sole purpose is making that process just a l
1414
`npm install @autogram/url-tools`
1515

1616
## Usage
17+
`ParsedUrl` and `NormalizedUrl` are meant to work as drop-in replacements for the built-in `URL` class.
1718

18-
UrlSet is the simplest example; toss URLs at it, and parsed URLs come out. Any that couldn't be parsed can be found the `urlSet.unparsable` property.
19+
```
20+
import { ParsedUrl } from '@autogram/url-tools';
21+
22+
const p = new ParsedUrl('http://staging.foo.com');
23+
console.log(p.domain); // 'foo.com';
24+
console.log(p.subdomain); // 'staging';
25+
```
26+
27+
### URL scrubbing and normalization
28+
`NormalizedUrl` applies a given `UrlMutator` function after it parses incoming URLs; by default it applies the relatively aggressive `UrlMutator.defaultNormalizer`; it strips off 'www' subdomains, `utm` querystring parameters, authentication information, ports, common index pages like `index.html` and `default.aspx`, page anchors, and more. It also enforces lowercasing of hostnames, and alphabetizes all remaining queryString parameters.
29+
30+
Individual rules are broken out into discrete `UrlMutator` functions for easy composition of alternate rulesets, and any function that accepts a `NormalizedUrl` and returns a `NormalizedUrl` can be passed in to implement custom rules.
31+
32+
```
33+
import { NormalizedUrl } from '@autogram/url-tools';
34+
35+
const url = 'http://www.mydomain.com:80/index.html?utm_campaign=foo&search=bar#footer';
36+
const n = new NormalizedUrl(url);
37+
console.log(n.href); // 'https://mydomain.com/?search.bar'
38+
39+
NormalizedUrl.normalizer = UrlMutators.stripPort;
40+
const n2 = new NormalizedUrl(url);
41+
console.log(n2.href); // 'http://www.mydomain.com/index.html?utm_campaign=foo&search=bar#footer'
42+
43+
const n3 = new NormalizedUrl(url, undefined, (url) => new NormalizedUrl('http://total-override.com'));
44+
console.log(n3.href); // 'http://total-override.com'
45+
```
46+
47+
### Batch parsing and de-duplication
48+
UrlSet is the simplest of the collection classes; toss URLs at it, and parsed URLs come out. Any that couldn't be parsed can be found the `urlSet.unparsable` property.
1949

2050
```
2151
import { UrlSet } from '@autogram/url-tools';
22-
const rawUrls = [
52+
53+
const us = new UrlSet([
2354
'http://example.com',
2455
'https://127.0.0.1',
2556
'tel:1-800-555-1212',
2657
'definitely-not-a-url'
27-
];
58+
]);
2859
29-
const us = new UrlSet(rawUrls);
3060
for (url of us) {
3161
console.log(url.href);
3262
}
@@ -35,18 +65,25 @@ for (url of us) {
3565
console.log([...us.unparsable]); // ['definitely-not-a-url']
3666
```
3767

68+
### Filtered and Normalized URL Sets
3869
Both `ParsedUrlSet` and `NormalizedUrlSet` can accept a `UrlFilter` function in their constructor options; incoming URLs rejected by that function are shunted to the Set's `parsedUrlSet.rejected` property and not added to the Set proper.
3970

4071
`NormalizedUrlSet` can rely rely on NormalizedUrl's aggressive defaults, or pass in a UrlMutator function to use as an override.
4172

4273
```
4374
import { NormalizedUrlSet, UrlFilters, UrlMutators } from '@autogram/url-tools';
75+
4476
const options = {
45-
filter: UrlFilters.isValidWebUrl,
77+
urlFilter: UrlFilters.isValidWebUrl,
4678
normalizer: (u) => UrlMutators.forceProtocol(u, 'https')
4779
}
80+
const ns = new NormalizedUrlSet([
81+
'http://example.com',
82+
'https://127.0.0.1',
83+
'tel:1-800-555-1212',
84+
'definitely-not-a-url'
85+
], options);
4886
49-
const ns = new NormalizedUrlSet(rawUrls);
5087
for (n of ns) {
5188
console.log(n.href);
5289
}

0 commit comments

Comments
 (0)