-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #231 from remusao/update
feature: add new 'getDomainWithoutSuffix(...)' method
- Loading branch information
Showing
11 changed files
with
98 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
/** | ||
* Return the part of domain without suffix. | ||
* | ||
* Example: for domain 'foo.com', the result would be 'foo'. | ||
*/ | ||
export default function getDomainWithoutSuffix(domain: string, suffix: string): string { | ||
// Note: here `domain` and `suffix` cannot have the same length because in | ||
// this case we set `domain` to `null` instead. It is thus safe to assume | ||
// that `suffix` is shorter than `domain`. | ||
return domain.slice(0, -suffix.length - 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ Using the command-line interface: | |
$ npx tldts 'http://www.writethedocs.org/conf/eu/2017/' | ||
{ | ||
"domain": "writethedocs.org", | ||
"domainWithoutSuffix": "writethedocs", | ||
"hostname": "www.writethedocs.org", | ||
"isIcann": true, | ||
"isIp": false, | ||
|
@@ -42,6 +43,7 @@ const { parse } = require('tldts'); | |
// Retrieving hostname related informations of a given URL | ||
parse('http://www.writethedocs.org/conf/eu/2017/'); | ||
// { domain: 'writethedocs.org', | ||
// domainWithoutSuffix: 'writethedocs', | ||
// hostname: 'www.writethedocs.org', | ||
// isIcann: true, | ||
// isIp: false, | ||
|
@@ -65,6 +67,7 @@ Alternatively, you can try it *directly in your browser* here: https://npm.runki | |
* `tldts.getDomain(url | hostname, options)` | ||
* `tldts.getPublicSuffix(url | hostname, options)` | ||
* `tldts.getSubdomain(url, | hostname, options)` | ||
* `tldts.getDomainWithoutSuffix(url | hostname, options)` | ||
|
||
The behavior of `tldts` can be customized using an `options` argument for all | ||
the functions exposed as part of the public API. This is useful to both change | ||
|
@@ -103,6 +106,7 @@ const tldts = require('tldts'); | |
|
||
tldts.parse('https://spark-public.s3.amazonaws.com/dataanalysis/loansData.csv'); | ||
// { domain: 'amazonaws.com', | ||
// domainWithoutSuffix: 'amazonaws', | ||
// hostname: 'spark-public.s3.amazonaws.com', | ||
// isIcann: true, | ||
// isIp: false, | ||
|
@@ -112,6 +116,7 @@ tldts.parse('https://spark-public.s3.amazonaws.com/dataanalysis/loansData.csv'); | |
|
||
tldts.parse('https://spark-public.s3.amazonaws.com/dataanalysis/loansData.csv', { allowPrivateDomains: true }) | ||
// { domain: 'spark-public.s3.amazonaws.com', | ||
// domainWithoutSuffix: 'spark-public', | ||
// hostname: 'spark-public.s3.amazonaws.com', | ||
// isIcann: false, | ||
// isIp: false, | ||
|
@@ -121,6 +126,7 @@ tldts.parse('https://spark-public.s3.amazonaws.com/dataanalysis/loansData.csv', | |
|
||
tldts.parse('gopher://domain.unknown/'); | ||
// { domain: 'domain.unknown', | ||
// domainWithoutSuffix: 'domain', | ||
// hostname: 'domain.unknown', | ||
// isIcann: false, | ||
// isIp: false, | ||
|
@@ -130,6 +136,7 @@ tldts.parse('gopher://domain.unknown/'); | |
|
||
tldts.parse('https://192.168.0.0') // IPv4 | ||
// { domain: null, | ||
// domainWithoutSuffix: null, | ||
// hostname: '192.168.0.0', | ||
// isIcann: null, | ||
// isIp: true, | ||
|
@@ -139,6 +146,7 @@ tldts.parse('https://192.168.0.0') // IPv4 | |
|
||
tldts.parse('https://[::1]') // IPv6 | ||
// { domain: null, | ||
// domainWithoutSuffix: null, | ||
// hostname: '::1', | ||
// isIcann: null, | ||
// isIp: true, | ||
|
@@ -148,6 +156,7 @@ tldts.parse('https://[::1]') // IPv6 | |
|
||
tldts.parse('[email protected]') // email | ||
// { domain: 'emailprovider.co.uk', | ||
// domainWithoutSuffix: 'emailprovider', | ||
// hostname: 'emailprovider.co.uk', | ||
// isIcann: true, | ||
// isIp: false, | ||
|
@@ -156,15 +165,16 @@ tldts.parse('[email protected]') // email | |
// subdomain: '' } | ||
``` | ||
|
||
| Property Name | Type | Description | | ||
|:-------------- |:------ |:------------------------------------------- | | ||
| `hostname` | `str` | `hostname` of the input extracted automatically | | ||
| `domain` | `str` | Domain (tld + sld) | | ||
| `subdomain` | `str` | Sub domain (what comes after `domain`) | | ||
| `publicSuffix` | `str` | Public Suffix (tld) of `hostname` | | ||
| `isIcann` | `bool` | Does TLD come from ICANN part of the list | | ||
| `isPrivate` | `bool` | Does TLD come from Private part of the list | | ||
| `isIP` | `bool` | Is `hostname` an IP address? | | ||
| Property Name | Type | Description | | ||
|:--------------------- |:------ |:----------------------------------------------- | | ||
| `hostname` | `str` | `hostname` of the input extracted automatically | | ||
| `domain` | `str` | Domain (tld + sld) | | ||
| `domainWithoutSuffix` | `str` | Domain without public suffix | | ||
| `subdomain` | `str` | Sub domain (what comes after `domain`) | | ||
| `publicSuffix` | `str` | Public Suffix (tld) of `hostname` | | ||
| `isIcann` | `bool` | Does TLD come from ICANN part of the list | | ||
| `isPrivate` | `bool` | Does TLD come from Private part of the list | | ||
| `isIP` | `bool` | Is `hostname` an IP address? | | ||
|
||
|
||
## Single purpose methods | ||
|
@@ -204,6 +214,22 @@ getDomain('fr.t.co'); // returns `t.co` | |
getDomain('https://user:[email protected]:8080/some/path?and&query#hash'); // returns `example.co.uk` | ||
``` | ||
|
||
### getDomainWithoutSuffix(url | hostname, options?) | ||
|
||
Returns the domain (as returned by `getDomain(...)`) without the public suffix part. | ||
|
||
```javascript | ||
const { getDomainWithoutSuffix } = require('tldts'); | ||
|
||
getDomainWithoutSuffix('google.com'); // returns `google` | ||
getDomainWithoutSuffix('fr.google.com'); // returns `google` | ||
getDomainWithoutSuffix('fr.google.google'); // returns `google` | ||
getDomainWithoutSuffix('foo.google.co.uk'); // returns `google` | ||
getDomainWithoutSuffix('t.co'); // returns `t` | ||
getDomainWithoutSuffix('fr.t.co'); // returns `t` | ||
getDomainWithoutSuffix('https://user:[email protected]:8080/some/path?and&query#hash'); // returns `example` | ||
``` | ||
|
||
### getSubdomain(url | hostname, options?) | ||
|
||
Returns the complete subdomain for a given string. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Submodule publicsuffix
updated
3 files
+2 −16 | public_suffix_list.dat | |
+7 −1 | tools/newgtlds.go | |
+6 −0 | tools/newgtlds_test.go |