Skip to content

Commit

Permalink
chore: Refactor after merging #65
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed May 27, 2019
1 parent fc80789 commit 98b794d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
19 changes: 14 additions & 5 deletions src/tries/serializeTrie.js
Expand Up @@ -50,7 +50,7 @@ function lineToString(line, i, arr) {
if (indexOfDifference === 0) {
// line and prevLine are completely different
separatorFromPrev = SEPARATORS.RESET;
} else if (prevLine.length <= line.length && indexOfDifference === prevLine.length - 1) {
} else if (indexOfDifference === prevLine.length - 1) {
// only the last part of line and prevLine are different
separatorFromPrev = SEPARATORS.SAME;
} else if (indexOfDifference > prevLine.length - 1) {
Expand All @@ -60,7 +60,18 @@ function lineToString(line, i, arr) {
} else {
// line and prevLine are different, but share a common root at indexOfDifference - 1
// we now need to go up the hierarchy to the common root
separatorFromPrev = new Array(prevLine.length - indexOfDifference - 1).fill(SEPARATORS.UP)
const levelsUp = prevLine.length - indexOfDifference - 1;

/* istanbul ignore if */
if (levelsUp < 1) {
// This is just a sanity check to ensure that serializeTrie() fails early if
// public suffix contains some unexpected rules.
// separatorFromPrev should never be an empty string if i > 0
// See https://github.com/peerigon/parse-domain/pull/65
throw new Error("Cannot serialize trie: The public suffix list contains unexpected rules.");
}

separatorFromPrev = new Array(levelsUp).fill(SEPARATORS.UP)
.join("");
}
}
Expand Down Expand Up @@ -95,9 +106,7 @@ function serializeTrie(parsedList, type) {

if (POSSIBLE_TYPES.indexOf(type) === -1) {
throw new Error(
`Cannot serialize trie: Unknown trie type "${type}". Expected type to be one of ${POSSIBLE_TYPES.map(
JSON.stringify
).join(", ")}`
`Cannot serialize trie: Unknown trie type "${type}". Expected type to be one of ${POSSIBLE_TYPES.join(", ")}`
);
}

Expand Down
1 change: 1 addition & 0 deletions test/parseDomain.test.js
Expand Up @@ -172,6 +172,7 @@ describe("parseDomain(url)", () => {
});
});

// See https://github.com/peerigon/parse-domain/pull/65
it("should parse police.uk as tld", () => {
expect(parseDomain("example.police.uk")).to.eql({
subdomain: "",
Expand Down
3 changes: 2 additions & 1 deletion test/tries/serializeTrie.test.js
Expand Up @@ -78,6 +78,7 @@ describe("serializeTrie()", () => {
[["pl", "gov.pl", "ap.gov.pl", "net.pl"], "pl>gov>ap<net"],
[["pl", "gov.pl", "ap.gov.pl", "uk", "ac.uk", "co.uk"], "pl>gov>ap|uk>ac,co"],
[["jp", "岐阜.jp", "静岡.jp", "موقع"], "jp>岐阜,静岡|موقع"],
// https://github.com/peerigon/parse-domain/pull/65
[["uk", "ac.uk", "police.uk", "*.sch.uk"], "uk>ac,police,sch>*"],
].forEach(args => {
const parsedList = args[0];
Expand All @@ -93,7 +94,7 @@ describe("serializeTrie()", () => {
expect(() => {
serializeTrie([], "unsupported type");
}).to.throw(
'Cannot serialize trie: Unknown trie type "unsupported type". Expected type to be one of "complete", "light"'
'Cannot serialize trie: Unknown trie type "unsupported type". Expected type to be one of complete, light'
);
});
});

0 comments on commit 98b794d

Please sign in to comment.