Skip to content

Commit

Permalink
refactor(index): Replaces var with let/const and cleans up style
Browse files Browse the repository at this point in the history
Closes #329
  • Loading branch information
lupomontero committed Dec 2, 2024
1 parent 4e1bba4 commit a3eb7aa
Showing 1 changed file with 28 additions and 43 deletions.
71 changes: 28 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import Punycode from 'punycode/punycode.js';

import punycode from 'punycode/punycode.js';
import rules from './data/rules.js';

var internals = {};


//
// Parse rules from file.
//
const rulesByPunySuffix = rules.reduce(
(map, rule) => {
const suffix = rule.replace(/^(\*\.|\!)/, '');
const punySuffix = Punycode.toASCII(suffix);
const punySuffix = punycode.toASCII(suffix);
const firstChar = rule.charAt(0);

if (map.has(punySuffix)) {
Expand All @@ -31,25 +27,24 @@ const rulesByPunySuffix = rules.reduce(
new Map(),
);


//
// Find rule for a given domain.
//
internals.findRule = function (domain) {
const findRule = (domain) => {
const punyDomain = punycode.toASCII(domain);
const punyDomainChunks = punyDomain.split('.');

var punyDomain = Punycode.toASCII(domain);
var punyDomainChunks = punyDomain.split('.');
for (var i = 0; i < punyDomainChunks.length; i++) {
var suffix = punyDomainChunks.slice(i).join('.');
var matchingRules = rulesByPunySuffix.get(suffix);
for (let i = 0; i < punyDomainChunks.length; i++) {
const suffix = punyDomainChunks.slice(i).join('.');
const matchingRules = rulesByPunySuffix.get(suffix);
if (matchingRules) {
return matchingRules;
}
}

return null;
};


//
// Error codes and messages.
//
Expand All @@ -63,7 +58,6 @@ export const errorCodes = {
LABEL_INVALID_CHARS: 'Domain name label can only contain alphanumeric characters or dashes.'
};


//
// Validate domain name and throw if not valid.
//
Expand All @@ -83,10 +77,9 @@ export const errorCodes = {
// * http://en.wikipedia.org/wiki/Domain_name
// * http://en.wikipedia.org/wiki/Hostname
//
internals.validate = function (input) {

const validate = (input) => {
// Before we can validate we need to take care of IDNs with unicode chars.
var ascii = Punycode.toASCII(input);
const ascii = punycode.toASCII(input);

if (ascii.length < 1) {
return 'DOMAIN_TOO_SHORT';
Expand All @@ -96,10 +89,10 @@ internals.validate = function (input) {
}

// Check each part's length and allowed chars.
var labels = ascii.split('.');
var label;
const labels = ascii.split('.');
let label;

for (var i = 0; i < labels.length; ++i) {
for (let i = 0; i < labels.length; ++i) {
label = labels[i];
if (!label.length) {
return 'LABEL_TOO_SHORT';
Expand All @@ -119,23 +112,20 @@ internals.validate = function (input) {
}
};


//
// Public API
//


//
// Parse domain.
//
export const parse = function (input) {

export const parse = (input) => {
if (typeof input !== 'string') {
throw new TypeError('Domain name must be a string.');
}

// Force domain to lowercase.
var domain = input.slice(0).toLowerCase();
let domain = input.slice(0).toLowerCase();

// Handle FQDN.
// TODO: Simply remove trailing dot?
Expand All @@ -144,7 +134,7 @@ export const parse = function (input) {
}

// Validate and sanitise input.
var error = internals.validate(domain);
const error = validate(domain);
if (error) {
return {
input: input,
Expand All @@ -155,7 +145,7 @@ export const parse = function (input) {
};
}

var parsed = {
const parsed = {
input: input,
tld: null,
sld: null,
Expand All @@ -164,28 +154,27 @@ export const parse = function (input) {
listed: false
};

var domainParts = domain.split('.');
const domainParts = domain.split('.');

// Non-Internet TLD
if (domainParts[domainParts.length - 1] === 'local') {
return parsed;
}

var handlePunycode = function () {

const handlePunycode = () => {
if (!/xn--/.test(domain)) {
return parsed;
}
if (parsed.domain) {
parsed.domain = Punycode.toASCII(parsed.domain);
parsed.domain = punycode.toASCII(parsed.domain);
}
if (parsed.subdomain) {
parsed.subdomain = Punycode.toASCII(parsed.subdomain);
parsed.subdomain = punycode.toASCII(parsed.subdomain);
}
return parsed;
};

var rule = internals.findRule(domain);
const rule = findRule(domain);

// Unlisted tld.
if (!rule) {
Expand All @@ -205,8 +194,8 @@ export const parse = function (input) {
// At this point we know the public suffix is listed.
parsed.listed = true;

var tldParts = rule.suffix.split('.');
var privateParts = domainParts.slice(0, domainParts.length - tldParts.length);
const tldParts = rule.suffix.split('.');
const privateParts = domainParts.slice(0, domainParts.length - tldParts.length);

if (rule.exception) {
privateParts.push(tldParts.shift());
Expand Down Expand Up @@ -237,25 +226,21 @@ export const parse = function (input) {
return handlePunycode();
};


//
// Get domain.
//
export const get = function (domain) {

export const get = (domain) => {
if (!domain) {
return null;
}
return parse(domain).domain || null;
};


//
// Check whether domain belongs to a known public suffix.
//
export const isValid = function (domain) {

var parsed = parse(domain);
export const isValid = (domain) => {
const parsed = parse(domain);
return Boolean(parsed.domain && parsed.listed);
};

Expand Down

0 comments on commit a3eb7aa

Please sign in to comment.