Skip to content

Commit

Permalink
refactor: Update code to node 4
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
node 4 or newer is required
  • Loading branch information
jhnns committed Dec 3, 2017
1 parent 536b657 commit 4d87f43
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 70 deletions.
5 changes: 1 addition & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{
"extends": [
"peerigon/base",
"peerigon/es5"
"peerigon/node"
],
"parserOptions": {
"ecmaVersion": 5
},
"env": {
"node": true
},
Expand Down
34 changes: 12 additions & 22 deletions lib/build/buildRegex.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,33 @@
"use strict";

var fs = require("fs");
var path = require("path");

var txtPath = path.resolve(__dirname, "./tld.txt");
var regexPath = path.resolve(__dirname, "../tld.js");
var txtContent;
var icannContent;
var privateContent;
var icannTld;
var privateTld;
var src;
var regex;
const fs = require("fs");
const path = require("path");

console.log("Reading " + txtPath + " ...");
const txtPath = path.resolve(__dirname, "./tld.txt");
const regexPath = path.resolve(__dirname, "../tld.js");

txtContent = fs.readFileSync(txtPath, "utf8");
console.log("Reading " + txtPath + " ...");

icannContent = txtContent.slice(
const txtContent = fs.readFileSync(txtPath, "utf8");
const icannContent = txtContent.slice(
txtContent.indexOf("// ===BEGIN ICANN DOMAINS==="),
txtContent.indexOf("// ===END ICANN DOMAINS===")
);
privateContent = txtContent.slice(
const privateContent = txtContent.slice(
txtContent.indexOf("// ===BEGIN PRIVATE DOMAINS==="),
txtContent.indexOf("// ===END PRIVATE DOMAINS===")
);

icannTld = icannContent.replace(/(\/\/.+)\r?\n/gi, "")
const icannTld = icannContent.replace(/(\/\/.+)\r?\n/gi, "")
.replace(/[\r?\n]+/g, "|")
.replace(/\./g, "\\.")
.replace(/\*/g, "[^.]+")
.slice(1, -1);
privateTld = privateContent.replace(/(\/\/.+)\r?\n/gi, "")
const privateTld = privateContent.replace(/(\/\/.+)\r?\n/gi, "")
.replace(/[\r?\n]+/g, "|")
.replace(/\./g, "\\.")
.replace(/\*/g, "[^.]+")
.slice(1, -1);

src = [
const src = [
"exports = module.exports = /\\.(" + icannTld + "|$" + privateTld + ")$/;",
"exports.icann = /\\.(" + icannTld + ")$/;",
"exports.private = /\\.(" + privateTld + ")$/;",
Expand All @@ -49,7 +39,7 @@ fs.writeFileSync(regexPath, src, "utf8");

console.log("Checking if it's valid JavaScript ...");

regex = require(regexPath); // eslint-disable-line import/no-dynamic-require
const regex = require(regexPath); // eslint-disable-line import/no-dynamic-require

if (regex instanceof RegExp === false) {
throw new Error("Generated regex is not instanceof RegExp. Instead saw " + regex);
Expand Down
2 changes: 1 addition & 1 deletion lib/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function normalizeUrl(url) {
}

function normalizeOptions(options) {
var normalized = !options || typeof options !== "object" ? Object.create(null) : options;
const normalized = !options || typeof options !== "object" ? Object.create(null) : options;

if ("privateTlds" in normalized === false) {
normalized.privateTlds = false;
Expand Down
30 changes: 14 additions & 16 deletions lib/parseDomain.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use strict";

var knownTlds = require("./tld.js");
var normalize = require("./normalize.js");
const knownTlds = require("./tld.js");
const normalize = require("./normalize.js");

var urlParts = /^(https?:\/\/)?([^/]*@)?(.+?)(:\d{2,5})?([/?].*)?$/; // 1 = protocol, 2 = auth, 3 = domain, 4 = port, 5 = path
var dot = /\./g;
const urlParts = /^(https?:\/\/)?([^/]*@)?(.+?)(:\d{2,5})?([/?].*)?$/; // 1 = protocol, 2 = auth, 3 = domain, 4 = port, 5 = path
const dot = /\./g;

function matchTld(domain, options) {
var tld = null;
let tld = null;

// for potentially unrecognized tlds, try matching against custom tlds
if (options.customTlds) {
Expand Down Expand Up @@ -42,18 +42,16 @@ function matchTld(domain, options) {
* @returns {Object|null}
*/
function parseDomain(url, options) {
var normalizedUrl = normalize.url(url);
var tld = null;
var normalizedOptions;
var urlSplit;
var domain;
var subdomain;
const normalizedUrl = normalize.url(url);
let tld = null;
let urlSplit;
let domain;

if (normalizedUrl === null) {
return null;
}

normalizedOptions = normalize.options(options);
const normalizedOptions = normalize.options(options);

// urlSplit can't be null because urlParts will always match at the third capture
urlSplit = normalizedUrl.match(urlParts);
Expand All @@ -72,12 +70,12 @@ function parseDomain(url, options) {
tld = tld.slice(1);
}
domain = urlSplit.pop();
subdomain = urlSplit.join(".");
const subdomain = urlSplit.join(".");

return {
tld: tld,
domain: domain,
subdomain: subdomain,
tld,
domain,
subdomain,
};
}

Expand Down
2 changes: 1 addition & 1 deletion test/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"extends": [
"peerigon/tests"
]
}
}
52 changes: 26 additions & 26 deletions test/parseDomain.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"use strict";

var chai = require("chai");
const chai = require("chai");

var expect = chai.expect;
var parseDomain = require("../lib/parseDomain.js");
const expect = chai.expect;
const parseDomain = require("../lib/parseDomain.js");

chai.config.includeStack = true;

describe("parseDomain(url)", function () {
describe("parseDomain(url)", () => {

it("should remove the protocol", function () {
it("should remove the protocol", () => {
expect(parseDomain("http://example.com")).to.eql({
subdomain: "",
domain: "example",
Expand All @@ -22,7 +22,7 @@ describe("parseDomain(url)", function () {
});
});

it("should remove sub-domains", function () {
it("should remove sub-domains", () => {
expect(parseDomain("www.example.com")).to.eql({
subdomain: "www",
domain: "example",
Expand All @@ -35,7 +35,7 @@ describe("parseDomain(url)", function () {
});
});

it("should remove the path", function () {
it("should remove the path", () => {
expect(parseDomain("example.com/some/path?and&query")).to.eql({
subdomain: "",
domain: "example",
Expand All @@ -48,107 +48,107 @@ describe("parseDomain(url)", function () {
});
});

it("should remove the query string", function () {
it("should remove the query string", () => {
expect(parseDomain("example.com?and&query")).to.eql({
subdomain: "",
domain: "example",
tld: "com",
});
});

it("should remove special characters", function () {
it("should remove special characters", () => {
expect(parseDomain("http://m.example.com\r")).to.eql({
subdomain: "m",
domain: "example",
tld: "com",
});
});

it("should remove the port", function () {
it("should remove the port", () => {
expect(parseDomain("example.com:8080")).to.eql({
subdomain: "",
domain: "example",
tld: "com",
});
});

it("should remove the authentication", function () {
it("should remove the authentication", () => {
expect(parseDomain("user:password@example.com")).to.eql({
subdomain: "",
domain: "example",
tld: "com",
});
});

it("should allow @ characters in the path", function () {
it("should allow @ characters in the path", () => {
expect(parseDomain("https://medium.com/@username/")).to.eql({
subdomain: "",
domain: "medium",
tld: "com",
});
});

it("should also work with three-level domains like .co.uk", function () {
it("should also work with three-level domains like .co.uk", () => {
expect(parseDomain("www.example.co.uk")).to.eql({
subdomain: "www",
domain: "example",
tld: "co.uk",
});
});

it("should not include private domains like blogspot.com by default", function () {
it("should not include private domains like blogspot.com by default", () => {
expect(parseDomain("foo.blogspot.com")).to.eql({
subdomain: "foo",
domain: "blogspot",
tld: "com",
});
});

it("should include private tlds", function () {
it("should include private tlds", () => {
expect(parseDomain("foo.blogspot.com", { privateTlds: true })).to.eql({
subdomain: "",
domain: "foo",
tld: "blogspot.com",
});
});

it("should work when all url parts are present", function () {
it("should work when all url parts are present", () => {
expect(parseDomain("https://user@www.some.other.subdomain.example.co.uk:8080/some/path?and&query#hash")).to.eql({
subdomain: "www.some.other.subdomain",
domain: "example",
tld: "co.uk",
});
});

it("should also work with the minimum", function () {
it("should also work with the minimum", () => {
expect(parseDomain("example.com")).to.eql({
subdomain: "",
domain: "example",
tld: "com",
});
});

it("should return null if the given url contains an unsupported top-level domain", function () {
it("should return null if the given url contains an unsupported top-level domain", () => {
expect(parseDomain("example.kk")).to.equal(null);
});

it("should return null if the given value is not a string", function () {
it("should return null if the given value is not a string", () => {
expect(parseDomain(undefined)).to.equal(null);
expect(parseDomain({})).to.equal(null);
expect(parseDomain("")).to.equal(null);
});

it("should work with domains that could match multiple tlds", function () {
it("should work with domains that could match multiple tlds", () => {
expect(parseDomain("http://hello.de.ibm.com")).to.eql({
subdomain: "hello.de",
domain: "ibm",
tld: "com",
});
});

it("should work with custom top-level domains (eg .local)", function () {
it("should work with custom top-level domains (eg .local)", () => {
function parseCustomTlds(url) {
var options = {
const options = {
customTlds: ["local"],
};

Expand All @@ -168,7 +168,7 @@ describe("parseDomain(url)", function () {
});
});

it("should behave itself when standard top-level domains sit atom custom top-level domains (eg .dev.local)", function () {
it("should behave itself when standard top-level domains sit atom custom top-level domains (eg .dev.local)", () => {
expect(parseDomain("ohno.dev.local")).to.eql(null);
expect(parseDomain("ohno.dev.local", { customTlds: ["local"] })).to.eql({
subdomain: "ohno",
Expand All @@ -182,7 +182,7 @@ describe("parseDomain(url)", function () {
});
});

it("should also work with custom top-level domains (eg .local) passed as regexps", function () {
it("should also work with custom top-level domains (eg .local) passed as regexps", () => {
expect(parseDomain("mymachine.local")).to.eql(null);
expect(parseDomain("mymachine.local", { customTlds: /\.local$/ })).to.eql({
subdomain: "",
Expand All @@ -191,9 +191,9 @@ describe("parseDomain(url)", function () {
});
});

it("should also work with custom hostnames (eg localhost) when passed as a regexp", function () {
it("should also work with custom hostnames (eg localhost) when passed as a regexp", () => {
function parseLocalDomains(url) {
var options = {
const options = {
customTlds: /localhost|\.local/,
};

Expand Down

0 comments on commit 4d87f43

Please sign in to comment.