Skip to content

Commit

Permalink
Path tree can now store paths that end with slash(/) and dont end wit…
Browse files Browse the repository at this point in the history
…h slash(/)
  • Loading branch information
AlexParra03 committed Mar 4, 2019
1 parent e804f58 commit f56bfb8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
17 changes: 10 additions & 7 deletions src/WebsiteState.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@ class WebsiteState {
return this.pathTree.__pathsGenerator("", this.pathTree.root);
}

addPath(path) {
this.pathTree.addPath(path);
}
addPath(path) { this.pathTree.addPath(path); }
}

/**
* Tree structure used to store path routes
*/
class PathTree {
constructor() {
this.root = new PathNode('');
}
constructor() { this.root = new PathNode(''); }

/**
* TODO handle ending slashed. Not all paths end in slash
Expand All @@ -40,6 +36,7 @@ class PathTree {
* @param {string} path should be a path starting with '/'
*/
addPath(path) {
const endsWithSlash = path[path.length - 1] === '/';
const paths = path.split('/').filter(elem => elem.trim() !== '');
let currentPathNode = this.root;
paths.forEach(function(currentPath) {
Expand All @@ -49,6 +46,9 @@ class PathTree {
const newPathNode = new PathNode(currentPath);
currentPathNode.children.push(newPathNode);
currentPathNode = newPathNode;
if (currentPath == paths[paths.length - 1]) {
newPathNode.endsWithSlash = endsWithSlash;
}
} else {
currentPathNode = childPathNode;
}
Expand All @@ -59,7 +59,9 @@ class PathTree {
* Generator that yields a path recursively when it has not been visited
*/
* __pathsGenerator(path, node) {
const currentPath = path + node.name + "/";
const endingSlash =
(node.children.length > 0 || node.endsWithSlash) ? '/' : '';
const currentPath = path + node.name + endingSlash;
if (!node.visited) {
yield currentPath;
node.visited = true;
Expand All @@ -74,6 +76,7 @@ class PathNode {
constructor(name) {
this.name = name;
this.visited = false;
this.endsWithSlash = true;
this.children = [];
}
}
Expand Down
29 changes: 26 additions & 3 deletions test/WebsiteState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@ const {WebsiteState} = require("../src/WebsiteState");

describe("Website State", function() {
describe("Generator", function() {

it("should iterate over path with no slash", function() {
const websiteState = new WebsiteState();
websiteState.pathTree.addPath("/a");

const paths = [];
for (const path of websiteState.getUnvisitedPaths()) {
paths.push(path);
}
expect(paths).to.eql(["/", "/a"]);
});

it("should iterate over path with slash", function() {
const websiteState = new WebsiteState();
websiteState.pathTree.addPath("/a/");

const paths = [];
for (const path of websiteState.getUnvisitedPaths()) {
paths.push(path);
}
expect(paths).to.eql(["/", "/a/"]);
});

it("should iterate only over new paths added", function() {
const websiteState = new WebsiteState();
websiteState.pathTree.addPath("/a/b/c");
Expand All @@ -18,7 +41,7 @@ describe("Website State", function() {
for (const path of websiteState.getUnvisitedPaths()) {
paths.push(path);
}
expect(paths).to.eql(["/a/b1/c1/", "/a/b1/c2/"]);
expect(paths).to.eql(["/a/b1/c1", "/a/b1/c2"]);
});

it("should iterate over path added", function() {
Expand All @@ -29,7 +52,7 @@ describe("Website State", function() {
for (const path of websiteState.getUnvisitedPaths()) {
paths.push(path);
}
expect(paths).to.eql(["/", "/a/"]);
expect(paths).to.eql(["/", "/a"]);
});

it("should iterate over parent paths added", function() {
Expand All @@ -40,7 +63,7 @@ describe("Website State", function() {
for (const path of websiteState.getUnvisitedPaths()) {
paths.push(path);
}
expect(paths).to.eql(["/", "/a/", "/a/b/"]);
expect(paths).to.eql(["/", "/a/", "/a/b"]);
});
});

Expand Down

0 comments on commit f56bfb8

Please sign in to comment.