Skip to content

Commit 4f3a5e3

Browse files
committed
📝 Update README and example
1 parent 60acc19 commit 4f3a5e3

File tree

2 files changed

+51
-34
lines changed

2 files changed

+51
-34
lines changed

README.md

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@
66

77
public_suffix is a dart library for identifying the public suffixes (or TLDs), root domains and registrable parts of URLs.
88

9+
## Main features
10+
- Identify suffix, root domain, registrable domain (root + suffix) and subdomain from URLs.
11+
- Provide your own list of suffix rules to keep up to date with the rules you need.
12+
- Get results from matching with only ICANN/IANA rules or also include private ones.
13+
- For example, a private rule can see the full `github.io` as suffix while ICANN only recognises the `io` part.
14+
- Parse punycode encoded URLs and get both encoded and decoded results.
15+
- Check if URLs are subdomains, have valid domain parts, or end with known suffixes.
16+
917
## Usage
10-
1) Import `public_suffix`:
18+
1) Import public_suffix:
1119
- `public_suffix.dart`: If you prefer loading suffix lists on your own.
1220
- `public_suffix_io.dart`: For helper methods to load suffix lists from URIs using `dart:io`.
1321
- `public_suffix_browser.dart`: For helper methods to load suffix lists from URIs using `dart:html`.
@@ -17,31 +25,40 @@ public_suffix is a dart library for identifying the public suffixes (or TLDs), r
1725

1826
**Note:** Don't overload publicsuffix.org's servers by repeatedly retrieving the suffix list from them. Cache a copy somewhere instead, and update that copy only when the master copy is updated.
1927

28+
### Short example
2029
```dart
2130
import 'package:public_suffix/public_suffix_io.dart';
2231
2332
main(List<String> arguments) async {
33+
// Load a list of suffix rules from publicsuffix.org.
2434
await SuffixRulesHelper.initFromUri(
25-
Uri.parse("https://publicsuffix.org/list/public_suffix_list.dat"));
26-
PublicSuffix suffix =
27-
PublicSuffix(Uri.parse("https://www.komposten.github.io"));
28-
29-
//Results when matching against ICANN/IANA and private suffixes.
30-
print(suffix.suffix); // "github.io"
31-
print(suffix.root); // "komposten"
32-
print(suffix.domain); // "komposten.github.io"
33-
34-
//Results when matching against only ICANN/IANA suffixes.
35-
print(suffix.icannSuffix); // "io"
36-
print(suffix.icannRoot); // "github"
37-
print(suffix.icannDomain); // "github.io"
38-
39-
//punycode decoded results.
40-
suffix = PublicSuffix(Uri.parse("https://www.xn--6qq79v.cn"));
41-
print(suffix.domain); // "xn--6qq79v.cn"
42-
print(suffix.punyDecoded.domain); // "你好.cn"
35+
Uri.parse('https://publicsuffix.org/list/public_suffix_list.dat'));
36+
37+
// Parse a URL.
38+
PublicSuffix parsedUrl =
39+
PublicSuffix.fromString('https://www.komposten.github.io');
40+
41+
// Obtain information using the many getters, for example:
42+
print(parsedUrl.suffix); // github.io
43+
print(parsedUrl.root); // komposten
44+
print(parsedUrl.domain); // komposten.github.io
45+
print(parsedUrl.icannDomain); // github.io
46+
47+
// public_suffix also supports punycoded URLs.
48+
parsedUrl = PublicSuffix.fromString('https://www.xn--6qq79v.cn');
49+
print(parsedUrl.domain); // xn--6qq79v.cn
50+
print(parsedUrl.punyDecoded.domain); // 你好.cn
4351
}
4452
```
4553

54+
## Utility functions
55+
Several utility functions can be found in the `DomainUtils` class (imported from `public_suffix.dart`). These currently include:
56+
- `isSubdomainOf`: Checks if a given URL is a subdomain of another domain.
57+
- `isSubdomain`: Checks if a given URL has a subdomain part.
58+
- `isKnownSuffix`: Checks if a given suffix (e.g. `co.uk`) is listed in the suffix rule list.
59+
- `hasValidDomain`: Checks if a given URL contains a registrable domain part.
60+
61+
Most of these create PublicSuffix objects internally. If you are already working with PublicSuffix objects, there are similar methods you can call directly on those to avoid creating new ones.
62+
4663
## License
4764
public_suffix is licensed under the MIT license. See [LICENSE](https://github.com/Komposten/public_suffix/blob/master/LICENSE) for the full license text.

example/main.dart

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
import 'package:public_suffix/public_suffix_io.dart';
22

3-
main(List<String> arguments) async {
3+
Future<void> main() async {
44
// Load a list of suffix rules from publicsuffix.org.
55
await SuffixRulesHelper.initFromUri(
6-
Uri.parse("https://publicsuffix.org/list/public_suffix_list.dat"));
6+
Uri.parse('https://publicsuffix.org/list/public_suffix_list.dat'));
77

88
// Parse a URL.
9-
PublicSuffix suffix =
10-
PublicSuffix(Uri.parse("https://www.komposten.github.io"));
9+
PublicSuffix parsedUrl =
10+
PublicSuffix.fromString('https://www.komposten.github.io');
1111

12-
// Results when matching against ICANN/IANA and private suffixes.
13-
print(suffix.suffix); // "github.io"
14-
print(suffix.root); // "komposten"
15-
print(suffix.domain); // "komposten.github.io"
12+
// Results when matching against both ICANN/IANA and private suffixes.
13+
print(parsedUrl.suffix); // github.io
14+
print(parsedUrl.root); // komposten
15+
print(parsedUrl.domain); // komposten.github.io
1616

1717
// Results when matching against only ICANN/IANA suffixes.
18-
print(suffix.icannSuffix); // "io"
19-
print(suffix.icannRoot); // "github"
20-
print(suffix.icannDomain); // "github.io"
18+
print(parsedUrl.icannSuffix); // io
19+
print(parsedUrl.icannRoot); // github
20+
print(parsedUrl.icannDomain); // github.io
2121

22-
// punycode decoded results.
23-
suffix = PublicSuffix(Uri.parse("https://www.xn--6qq79v.cn"));
24-
print(suffix.domain); // "xn--6qq79v.cn"
25-
print(suffix.punyDecoded.domain); // "你好.cn"
22+
// Punycode decoded results.
23+
parsedUrl = PublicSuffix.fromString('https://www.xn--6qq79v.cn');
24+
print(parsedUrl.domain); // xn--6qq79v.cn
25+
print(parsedUrl.punyDecoded.domain); // 你好.cn
2626

2727
// Dispose the list to unload it from memory if you wish.
2828
// This is probably not needed since the list is relatively small.

0 commit comments

Comments
 (0)