Skip to content

Commit 32aad9b

Browse files
authored
Merge pull request #16 from itsemast/master
TypeScript support with allowDotlessTLD
2 parents 0788de8 + a3c8cde commit 32aad9b

File tree

5 files changed

+115
-3
lines changed

5 files changed

+115
-3
lines changed

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
coverage
2+
index.d.ts
3+
index.test-d.ts

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ Using a tld as a direct domain name, or [dotless domain](https://en.wikipedia.or
7777
* full code coverage
7878
* easy to read (10 lines)
7979
* easily updatable vs mozilla TLDs source list
80+
* TypeScript support
8081

8182
# Maintenance
8283
You can update the remote hash table using `npm run update`

index.d.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
interface ParseOptions {
2+
/**
3+
* Allow parsing URLs that contain the TLDs specified under the "private" property
4+
* in the effective_tld_names.json file.
5+
*/
6+
allowPrivateTLD?: boolean;
7+
/**
8+
* Allow parsing URLs that contain TLDs that are not in the effective_tld_names.json file.
9+
*
10+
* @note This option can also be used when parsing URLs that contain IP addresses.
11+
*/
12+
allowUnknownTLD?: boolean;
13+
/**
14+
* Allow parsing URLs that contain a top-level domain (TLD) used as a direct domain name,
15+
* also known as a dotless domain.
16+
*
17+
* @note Using dotless domains is highly not recommended by ICANN and IAB. Use with caution.
18+
*/
19+
allowDotlessTLD?: boolean;
20+
}
21+
22+
interface ParseResult {
23+
tld: string;
24+
domain: string;
25+
sub: string;
26+
}
27+
28+
declare function parse_url(
29+
remote_url: string,
30+
options?: ParseOptions
31+
): ParseResult;
32+
33+
declare namespace parse_url {
34+
/**
35+
* Parse the hostname of a URL instead of the entire URL.
36+
*
37+
* @example parse_host('www.github.com') // { tld: 'com', domain: 'github.com', sub: 'www' }
38+
*/
39+
export function parse_host(host: string, options?: ParseOptions): ParseResult;
40+
}
41+
42+
export = parse_url;

index.test-d.ts

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import parse_url, { parse_host } from ".";
2+
import { expectAssignable, expectError, expectType } from "tsd";
3+
4+
interface ParseResult {
5+
tld: string;
6+
domain: string;
7+
sub: string;
8+
}
9+
10+
expectType<ParseResult>(parse_url(""));
11+
expectType<ParseResult>(parse_url("", {}));
12+
expectType<ParseResult>(parse_url("", { allowPrivateTLD: true }));
13+
expectType<ParseResult>(parse_url("", { allowUnknownTLD: true }));
14+
expectType<ParseResult>(
15+
parse_url("", { allowPrivateTLD: true, allowUnknownTLD: true })
16+
);
17+
expectType<ParseResult>(
18+
parse_url("", {
19+
allowPrivateTLD: true,
20+
allowUnknownTLD: true,
21+
allowDotlessTLD: true,
22+
})
23+
);
24+
25+
expectType<ParseResult>(parse_host(""));
26+
expectType<ParseResult>(parse_host("", {}));
27+
expectType<ParseResult>(parse_host("", { allowPrivateTLD: true }));
28+
expectType<ParseResult>(parse_host("", { allowUnknownTLD: true }));
29+
expectType<ParseResult>(
30+
parse_host("", { allowPrivateTLD: true, allowUnknownTLD: true })
31+
);
32+
expectType<ParseResult>(
33+
parse_host("", {
34+
allowPrivateTLD: true,
35+
allowUnknownTLD: true,
36+
allowDotlessTLD: true,
37+
})
38+
);
39+
40+
expectError<ParseResult>(parse_url());
41+
expectError<ParseResult>(parse_url({}));
42+
expectError<ParseResult>(parse_host());
43+
expectError<ParseResult>(parse_host({}));
44+
45+
const testUrl = "https://github.com/131/node-tld/blob/master/index.js";
46+
const testHostname = "github.com";
47+
const testOptions = {
48+
allowPrivateTLD: true,
49+
allowUnknownTLD: true,
50+
allowDotlessTLD: true,
51+
};
52+
53+
interface ParseOptions {
54+
allowPrivateTLD?: boolean;
55+
allowUnknownTLD?: boolean;
56+
allowDotlessTLD?: boolean;
57+
}
58+
59+
expectAssignable<string>(testUrl);
60+
expectAssignable<string>(testHostname);
61+
expectAssignable<ParseOptions>(testOptions);
62+
expectType<ParseResult>(parse_url(testUrl));
63+
expectType<ParseResult>(parse_url(testUrl, testOptions));
64+
expectType<ParseResult>(parse_host(testHostname));
65+
expectType<ParseResult>(parse_host(testHostname, testOptions));

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
"version": "2.1.0",
44
"description": "Extract the TLD/domain/subdomain parts of an URL/hostname against mozilla TLDs 'official' listing .",
55
"main": "index.js",
6+
"types": "index.d.ts",
67
"scripts": {
78
"update": "node update.js",
8-
"test": "npm run eslint && npm run cover",
9+
"test": "npm run eslint && npm run cover && npm run test:typescript",
10+
"test:typescript": "tsd",
911
"preversion": "npm run test",
1012
"eslint": "eslint .",
1113
"checkall": "npm run eslint",
@@ -26,12 +28,12 @@
2628
"eslint-plugin-ivs": "^1.3.0",
2729
"expect.js": "^0.3.1",
2830
"mocha": "^3.1.2",
29-
"nyc": "^15.1.0"
31+
"nyc": "^15.1.0",
32+
"tsd": "^0.17.0"
3033
},
3134
"directories": {
3235
"test": "test"
3336
},
34-
"dependencies": {},
3537
"keywords": [
3638
"tld",
3739
"tlds",

0 commit comments

Comments
 (0)