Skip to content

Commit

Permalink
Merge pull request #16 from itsemast/master
Browse files Browse the repository at this point in the history
TypeScript support with allowDotlessTLD
  • Loading branch information
131 authored Oct 27, 2023
2 parents 0788de8 + a3c8cde commit 32aad9b
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
coverage
index.d.ts
index.test-d.ts
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Using a tld as a direct domain name, or [dotless domain](https://en.wikipedia.or
* full code coverage
* easy to read (10 lines)
* easily updatable vs mozilla TLDs source list
* TypeScript support

# Maintenance
You can update the remote hash table using `npm run update`
Expand Down
42 changes: 42 additions & 0 deletions index.d.ts
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('github.com') // { tld: 'com', domain: 'github.com', sub: 'www' }
*/
export function parse_host(host: string, options?: ParseOptions): ParseResult;
}

export = parse_url;
65 changes: 65 additions & 0 deletions index.test-d.ts
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:/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));
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"version": "2.1.0",
"description": "Extract the TLD/domain/subdomain parts of an URL/hostname against mozilla TLDs 'official' listing .",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"update": "node update.js",
"test": "npm run eslint && npm run cover",
"test": "npm run eslint && npm run cover && npm run test:typescript",
"test:typescript": "tsd",
"preversion": "npm run test",
"eslint": "eslint .",
"checkall": "npm run eslint",
Expand All @@ -26,12 +28,12 @@
"eslint-plugin-ivs": "^1.3.0",
"expect.js": "^0.3.1",
"mocha": "^3.1.2",
"nyc": "^15.1.0"
"nyc": "^15.1.0",
"tsd": "^0.17.0"
},
"directories": {
"test": "test"
},
"dependencies": {},
"keywords": [
"tld",
"tlds",
Expand Down

0 comments on commit 32aad9b

Please sign in to comment.