Compare commits

...

5 Commits

Author SHA1 Message Date
Alex Rock
8e75388d57
Merge 4b0dfd4dcd into 2c67b48c21 2024-04-25 08:18:59 +08:00
Alex Rock Ancelet
4b0dfd4dcd
Make sure a vertex can't be added twice to a graph 2019-12-18 09:05:58 +01:00
Alex Rock Ancelet
6045f67230
Make sure toString is called on edge key when calling edge.toString() 2019-12-18 08:55:29 +01:00
Alex Rock Ancelet
9465d0e4c4
Make sure graph vertex value is converted to string 2019-12-18 08:49:35 +01:00
Alex Rock Ancelet
37f3d54aff
Allow graph edges with custom keys 2019-12-18 08:43:53 +01:00
6 changed files with 74 additions and 11 deletions

View File

@ -13,7 +13,13 @@ export default class Graph {
* @returns {Graph} * @returns {Graph}
*/ */
addVertex(newVertex) { addVertex(newVertex) {
this.vertices[newVertex.getKey()] = newVertex; const key = newVertex.getKey();
if (this.vertices[key]) {
throw new Error('Vertex has already been added before');
}
this.vertices[key] = newVertex;
return this; return this;
} }

View File

@ -2,22 +2,27 @@ export default class GraphEdge {
/** /**
* @param {GraphVertex} startVertex * @param {GraphVertex} startVertex
* @param {GraphVertex} endVertex * @param {GraphVertex} endVertex
* @param {number} [weight=1] * @param {number} [weight=0]
* @param key
*/ */
constructor(startVertex, endVertex, weight = 0) { constructor(startVertex, endVertex, weight = 0, key = null) {
this.startVertex = startVertex; this.startVertex = startVertex;
this.endVertex = endVertex; this.endVertex = endVertex;
this.weight = weight; this.weight = weight;
this.key = key;
} }
/**
* @return {string}
*/
getKey() { getKey() {
if (this.key) {
return this.key;
}
const startVertexKey = this.startVertex.getKey(); const startVertexKey = this.startVertex.getKey();
const endVertexKey = this.endVertex.getKey(); const endVertexKey = this.endVertex.getKey();
return `${startVertexKey}_${endVertexKey}`; this.key = `${startVertexKey}_${endVertexKey}`;
return this.key;
} }
/** /**
@ -35,6 +40,6 @@ export default class GraphEdge {
* @return {string} * @return {string}
*/ */
toString() { toString() {
return this.getKey(); return this.getKey().toString();
} }
} }

View File

@ -133,6 +133,6 @@ export default class GraphVertex {
* @returns {string} * @returns {string}
*/ */
toString(callback) { toString(callback) {
return callback ? callback(this.value) : `${this.value}`; return callback ? callback(this.value).toString() : this.value.toString();
} }
} }

View File

@ -158,6 +158,19 @@ describe('Graph', () => {
expect(addSameEdgeTwice).toThrow(); expect(addSameEdgeTwice).toThrow();
}); });
it('should throw an error when trying to add vertex twice', () => {
function addSameEdgeTwice() {
const graph = new Graph(true);
const vertexA = new GraphVertex('A');
graph
.addVertex(vertexA)
.addVertex(vertexA);
}
expect(addSameEdgeTwice).toThrow();
});
it('should return the list of all added edges', () => { it('should return the list of all added edges', () => {
const graph = new Graph(true); const graph = new Graph(true);

View File

@ -7,8 +7,6 @@ describe('GraphEdge', () => {
const endVertex = new GraphVertex('B'); const endVertex = new GraphVertex('B');
const edge = new GraphEdge(startVertex, endVertex); const edge = new GraphEdge(startVertex, endVertex);
expect(edge.getKey()).toBe('A_B');
expect(edge.toString()).toBe('A_B');
expect(edge.startVertex).toEqual(startVertex); expect(edge.startVertex).toEqual(startVertex);
expect(edge.endVertex).toEqual(endVertex); expect(edge.endVertex).toEqual(endVertex);
expect(edge.weight).toEqual(0); expect(edge.weight).toEqual(0);
@ -39,4 +37,29 @@ describe('GraphEdge', () => {
expect(edge.endVertex).toEqual(vertexA); expect(edge.endVertex).toEqual(vertexA);
expect(edge.weight).toEqual(10); expect(edge.weight).toEqual(10);
}); });
it('should return edges names as key', () => {
const edge = new GraphEdge(new GraphVertex('A'), new GraphVertex('B'), 0);
expect(edge.getKey()).toBe('A_B');
expect(edge.toString()).toBe('A_B');
});
it('should return custom key if defined', () => {
const edge = new GraphEdge(new GraphVertex('A'), new GraphVertex('B'), 0, 'custom_key');
expect(edge.getKey()).toEqual('custom_key');
expect(edge.toString()).toEqual('custom_key');
});
it('should execute toString on key when calling toString on edge', () => {
const customKey = {
toString() { return 'custom_key'; },
};
const edge = new GraphEdge(new GraphVertex('A'), new GraphVertex('B'), 0, customKey);
expect(edge.getKey()).toEqual(customKey);
expect(edge.toString()).toEqual('custom_key');
});
}); });

View File

@ -185,4 +185,20 @@ describe('GraphVertex', () => {
expect(vertexA.getEdges().length).toEqual(3); expect(vertexA.getEdges().length).toEqual(3);
}); });
it('should execute callback when passed to toString', () => {
const vertex = new GraphVertex('A');
expect(vertex.toString(() => 'B')).toEqual('B');
});
it('should execute toString on value when calling toString on vertex', () => {
const value = {
toString() { return 'A'; },
};
const vertex = new GraphVertex(value);
expect(vertex.toString()).toEqual('A');
});
}); });