1- import Punycode from 'punycode/punycode.js' ;
2-
1+ import punycode from 'punycode/punycode.js' ;
32import rules from './data/rules.js' ;
43
5- var internals = { } ;
6-
7-
84//
95// Parse rules from file.
106//
117const rulesByPunySuffix = rules . reduce (
128 ( map , rule ) => {
139 const suffix = rule . replace ( / ^ ( \* \. | \! ) / , '' ) ;
14- const punySuffix = Punycode . toASCII ( suffix ) ;
10+ const punySuffix = punycode . toASCII ( suffix ) ;
1511 const firstChar = rule . charAt ( 0 ) ;
1612
1713 if ( map . has ( punySuffix ) ) {
@@ -31,25 +27,24 @@ const rulesByPunySuffix = rules.reduce(
3127 new Map ( ) ,
3228) ;
3329
34-
3530//
3631// Find rule for a given domain.
3732//
38- internals . findRule = function ( domain ) {
33+ const findRule = ( domain ) => {
34+ const punyDomain = punycode . toASCII ( domain ) ;
35+ const punyDomainChunks = punyDomain . split ( '.' ) ;
3936
40- var punyDomain = Punycode . toASCII ( domain ) ;
41- var punyDomainChunks = punyDomain . split ( '.' ) ;
42- for ( var i = 0 ; i < punyDomainChunks . length ; i ++ ) {
43- var suffix = punyDomainChunks . slice ( i ) . join ( '.' ) ;
44- var matchingRules = rulesByPunySuffix . get ( suffix ) ;
37+ for ( let i = 0 ; i < punyDomainChunks . length ; i ++ ) {
38+ const suffix = punyDomainChunks . slice ( i ) . join ( '.' ) ;
39+ const matchingRules = rulesByPunySuffix . get ( suffix ) ;
4540 if ( matchingRules ) {
4641 return matchingRules ;
4742 }
4843 }
44+
4945 return null ;
5046} ;
5147
52-
5348//
5449// Error codes and messages.
5550//
@@ -63,7 +58,6 @@ export const errorCodes = {
6358 LABEL_INVALID_CHARS : 'Domain name label can only contain alphanumeric characters or dashes.'
6459} ;
6560
66-
6761//
6862// Validate domain name and throw if not valid.
6963//
@@ -83,10 +77,9 @@ export const errorCodes = {
8377// * http://en.wikipedia.org/wiki/Domain_name
8478// * http://en.wikipedia.org/wiki/Hostname
8579//
86- internals . validate = function ( input ) {
87-
80+ const validate = ( input ) => {
8881 // Before we can validate we need to take care of IDNs with unicode chars.
89- var ascii = Punycode . toASCII ( input ) ;
82+ const ascii = punycode . toASCII ( input ) ;
9083
9184 if ( ascii . length < 1 ) {
9285 return 'DOMAIN_TOO_SHORT' ;
@@ -96,10 +89,10 @@ internals.validate = function (input) {
9689 }
9790
9891 // Check each part's length and allowed chars.
99- var labels = ascii . split ( '.' ) ;
100- var label ;
92+ const labels = ascii . split ( '.' ) ;
93+ let label ;
10194
102- for ( var i = 0 ; i < labels . length ; ++ i ) {
95+ for ( let i = 0 ; i < labels . length ; ++ i ) {
10396 label = labels [ i ] ;
10497 if ( ! label . length ) {
10598 return 'LABEL_TOO_SHORT' ;
@@ -119,23 +112,20 @@ internals.validate = function (input) {
119112 }
120113} ;
121114
122-
123115//
124116// Public API
125117//
126118
127-
128119//
129120// Parse domain.
130121//
131- export const parse = function ( input ) {
132-
122+ export const parse = ( input ) => {
133123 if ( typeof input !== 'string' ) {
134124 throw new TypeError ( 'Domain name must be a string.' ) ;
135125 }
136126
137127 // Force domain to lowercase.
138- var domain = input . slice ( 0 ) . toLowerCase ( ) ;
128+ let domain = input . slice ( 0 ) . toLowerCase ( ) ;
139129
140130 // Handle FQDN.
141131 // TODO: Simply remove trailing dot?
@@ -144,7 +134,7 @@ export const parse = function (input) {
144134 }
145135
146136 // Validate and sanitise input.
147- var error = internals . validate ( domain ) ;
137+ const error = validate ( domain ) ;
148138 if ( error ) {
149139 return {
150140 input : input ,
@@ -155,7 +145,7 @@ export const parse = function (input) {
155145 } ;
156146 }
157147
158- var parsed = {
148+ const parsed = {
159149 input : input ,
160150 tld : null ,
161151 sld : null ,
@@ -164,28 +154,27 @@ export const parse = function (input) {
164154 listed : false
165155 } ;
166156
167- var domainParts = domain . split ( '.' ) ;
157+ const domainParts = domain . split ( '.' ) ;
168158
169159 // Non-Internet TLD
170160 if ( domainParts [ domainParts . length - 1 ] === 'local' ) {
171161 return parsed ;
172162 }
173163
174- var handlePunycode = function ( ) {
175-
164+ const handlePunycode = ( ) => {
176165 if ( ! / x n - - / . test ( domain ) ) {
177166 return parsed ;
178167 }
179168 if ( parsed . domain ) {
180- parsed . domain = Punycode . toASCII ( parsed . domain ) ;
169+ parsed . domain = punycode . toASCII ( parsed . domain ) ;
181170 }
182171 if ( parsed . subdomain ) {
183- parsed . subdomain = Punycode . toASCII ( parsed . subdomain ) ;
172+ parsed . subdomain = punycode . toASCII ( parsed . subdomain ) ;
184173 }
185174 return parsed ;
186175 } ;
187176
188- var rule = internals . findRule ( domain ) ;
177+ const rule = findRule ( domain ) ;
189178
190179 // Unlisted tld.
191180 if ( ! rule ) {
@@ -205,8 +194,8 @@ export const parse = function (input) {
205194 // At this point we know the public suffix is listed.
206195 parsed . listed = true ;
207196
208- var tldParts = rule . suffix . split ( '.' ) ;
209- var privateParts = domainParts . slice ( 0 , domainParts . length - tldParts . length ) ;
197+ const tldParts = rule . suffix . split ( '.' ) ;
198+ const privateParts = domainParts . slice ( 0 , domainParts . length - tldParts . length ) ;
210199
211200 if ( rule . exception ) {
212201 privateParts . push ( tldParts . shift ( ) ) ;
@@ -237,25 +226,21 @@ export const parse = function (input) {
237226 return handlePunycode ( ) ;
238227} ;
239228
240-
241229//
242230// Get domain.
243231//
244- export const get = function ( domain ) {
245-
232+ export const get = ( domain ) => {
246233 if ( ! domain ) {
247234 return null ;
248235 }
249236 return parse ( domain ) . domain || null ;
250237} ;
251238
252-
253239//
254240// Check whether domain belongs to a known public suffix.
255241//
256- export const isValid = function ( domain ) {
257-
258- var parsed = parse ( domain ) ;
242+ export const isValid = ( domain ) => {
243+ const parsed = parse ( domain ) ;
259244 return Boolean ( parsed . domain && parsed . listed ) ;
260245} ;
261246
0 commit comments