-
Notifications
You must be signed in to change notification settings - Fork 2k
/
get-tld.js
32 lines (27 loc) · 1.04 KB
/
get-tld.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* Internal dependencies
*/
import { parseDomainAgainstTldList } from './utils';
import wpcomMultiLevelTlds from './tlds/wpcom-multi-level-tlds.json';
/**
* Parse the tld from a given domain name, semi-naively. The function
* first parses against a list of tlds that have been sold on WP.com
* and falls back to a simplistic "everything after the last dot" approach
* if the list of explicitly allowed tlds failed. This is ultimately not comprehensive as that
* is a poor base assumption (lots of second level tlds, etc). However,
* for our purposes, the approach should be "good enough" for a long time.
*
* @param {string} domainName The domain name parse the tld from
* @returns {string} The TLD or an empty string
*/
export function getTld( domainName ) {
const lastIndexOfDot = domainName.lastIndexOf( '.' );
if ( lastIndexOfDot === -1 ) {
return '';
}
let tld = parseDomainAgainstTldList( domainName, wpcomMultiLevelTlds );
if ( ! tld ) {
tld = domainName.substring( lastIndexOfDot + 1 );
}
return tld;
}