From efc2976d9b1d22c9794a6cdf3a0dd7d25150a7cd Mon Sep 17 00:00:00 2001 From: Johannes Ewald Date: Tue, 28 May 2019 14:34:14 +0200 Subject: [PATCH] chore: Refactor after merging #70 https://github.com/peerigon/parse-domain/pull/70 --- src/parseDomain.js | 4 +++- test/parseDomain.test.js | 9 ++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/parseDomain.js b/src/parseDomain.js index de42ebd..8db058a 100644 --- a/src/parseDomain.js +++ b/src/parseDomain.js @@ -70,8 +70,8 @@ function parseDomain(url, options) { } domain = urlSplit[3]; // domain will now be something like sub.domain.example.com - tld = matchTld(domain, normalizedOptions); + if (tld === null) { return null; } @@ -83,7 +83,9 @@ function parseDomain(url, options) { // removes the remaining dot, if present (added to handle localhost) tld = tld.slice(1); } + domain = urlSplit.pop(); + const subdomain = urlSplit.join("."); return { diff --git a/test/parseDomain.test.js b/test/parseDomain.test.js index e87d287..bf96656 100644 --- a/test/parseDomain.test.js +++ b/test/parseDomain.test.js @@ -141,14 +141,13 @@ describe("parseDomain(url)", () => { 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); - expect(parseDomain(" ")).to.equal(null); - expect(parseDomain("\xa0")).to.equal(null); }); - it("should return null if the given value contains invalid characters", () => { - expect(parseDomain("http://hell.d\ne.ibm.com")).to.equal(null); + it("should return null if the given string is not a valid URL", () => { expect(parseDomain("\xa0")).to.equal(null); + expect(parseDomain("")).to.equal(null); + expect(parseDomain(" ")).to.equal(null); + expect(parseDomain("http://hell.d\ne.ibm.com")).to.equal(null); }); it("should return null if the given is an empty string with a space character", () => {