This commit is contained in:
Alex Rock 2024-04-25 08:18:59 +08:00 committed by GitHub
commit 8e75388d57
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 74 additions and 11 deletions

View File

@ -13,7 +13,13 @@ export default class Graph {
* @returns {Graph}
*/
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;
}

View File

@ -2,22 +2,27 @@ export default class GraphEdge {
/**
* @param {GraphVertex} startVertex
* @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.endVertex = endVertex;
this.weight = weight;
this.key = key;
}
/**
* @return {string}
*/
getKey() {
if (this.key) {
return this.key;
}
const startVertexKey = this.startVertex.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}
*/
toString() {
return this.getKey();
return this.getKey().toString();
}
}

View File

@ -133,6 +133,6 @@ export default class GraphVertex {
* @returns {string}
*/
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();
});
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', () => {
const graph = new Graph(true);

View File

@ -7,8 +7,6 @@ describe('GraphEdge', () => {
const endVertex = new GraphVertex('B');
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.endVertex).toEqual(endVertex);
expect(edge.weight).toEqual(0);
@ -39,4 +37,29 @@ describe('GraphEdge', () => {
expect(edge.endVertex).toEqual(vertexA);
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);
});
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');
});
});