-
Notifications
You must be signed in to change notification settings - Fork 15
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 #16 from itsemast/master
TypeScript support with allowDotlessTLD
- Loading branch information
Showing
5 changed files
with
115 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
coverage | ||
index.d.ts | ||
index.test-d.ts |
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,42 @@ | ||
interface ParseOptions { | ||
/** | ||
* Allow parsing URLs that contain the TLDs specified under the "private" property | ||
* in the effective_tld_names.json file. | ||
*/ | ||
allowPrivateTLD?: boolean; | ||
/** | ||
* Allow parsing URLs that contain TLDs that are not in the effective_tld_names.json file. | ||
* | ||
* @note This option can also be used when parsing URLs that contain IP addresses. | ||
*/ | ||
allowUnknownTLD?: boolean; | ||
/** | ||
* Allow parsing URLs that contain a top-level domain (TLD) used as a direct domain name, | ||
* also known as a dotless domain. | ||
* | ||
* @note Using dotless domains is highly not recommended by ICANN and IAB. Use with caution. | ||
*/ | ||
allowDotlessTLD?: boolean; | ||
} | ||
|
||
interface ParseResult { | ||
tld: string; | ||
domain: string; | ||
sub: string; | ||
} | ||
|
||
declare function parse_url( | ||
remote_url: string, | ||
options?: ParseOptions | ||
): ParseResult; | ||
|
||
declare namespace parse_url { | ||
/** | ||
* Parse the hostname of a URL instead of the entire URL. | ||
* | ||
* @example parse_host('www.github.com') // { tld: 'com', domain: 'github.com', sub: 'www' } | ||
*/ | ||
export function parse_host(host: string, options?: ParseOptions): ParseResult; | ||
} | ||
|
||
export = parse_url; |
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,65 @@ | ||
import parse_url, { parse_host } from "."; | ||
import { expectAssignable, expectError, expectType } from "tsd"; | ||
|
||
interface ParseResult { | ||
tld: string; | ||
domain: string; | ||
sub: string; | ||
} | ||
|
||
expectType<ParseResult>(parse_url("")); | ||
expectType<ParseResult>(parse_url("", {})); | ||
expectType<ParseResult>(parse_url("", { allowPrivateTLD: true })); | ||
expectType<ParseResult>(parse_url("", { allowUnknownTLD: true })); | ||
expectType<ParseResult>( | ||
parse_url("", { allowPrivateTLD: true, allowUnknownTLD: true }) | ||
); | ||
expectType<ParseResult>( | ||
parse_url("", { | ||
allowPrivateTLD: true, | ||
allowUnknownTLD: true, | ||
allowDotlessTLD: true, | ||
}) | ||
); | ||
|
||
expectType<ParseResult>(parse_host("")); | ||
expectType<ParseResult>(parse_host("", {})); | ||
expectType<ParseResult>(parse_host("", { allowPrivateTLD: true })); | ||
expectType<ParseResult>(parse_host("", { allowUnknownTLD: true })); | ||
expectType<ParseResult>( | ||
parse_host("", { allowPrivateTLD: true, allowUnknownTLD: true }) | ||
); | ||
expectType<ParseResult>( | ||
parse_host("", { | ||
allowPrivateTLD: true, | ||
allowUnknownTLD: true, | ||
allowDotlessTLD: true, | ||
}) | ||
); | ||
|
||
expectError<ParseResult>(parse_url()); | ||
expectError<ParseResult>(parse_url({})); | ||
expectError<ParseResult>(parse_host()); | ||
expectError<ParseResult>(parse_host({})); | ||
|
||
const testUrl = "https://github.com/131/node-tld/blob/master/index.js"; | ||
const testHostname = "github.com"; | ||
const testOptions = { | ||
allowPrivateTLD: true, | ||
allowUnknownTLD: true, | ||
allowDotlessTLD: true, | ||
}; | ||
|
||
interface ParseOptions { | ||
allowPrivateTLD?: boolean; | ||
allowUnknownTLD?: boolean; | ||
allowDotlessTLD?: boolean; | ||
} | ||
|
||
expectAssignable<string>(testUrl); | ||
expectAssignable<string>(testHostname); | ||
expectAssignable<ParseOptions>(testOptions); | ||
expectType<ParseResult>(parse_url(testUrl)); | ||
expectType<ParseResult>(parse_url(testUrl, testOptions)); | ||
expectType<ParseResult>(parse_host(testHostname)); | ||
expectType<ParseResult>(parse_host(testHostname, testOptions)); |
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