| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4ba97b9 commit 53f8c0d
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,22 +2,27 @@ export default class GraphEdge { | |||
| 2 | 2 | /** | |
| 3 | 3 | * @param {GraphVertex} startVertex | |
| 4 | 4 | * @param {GraphVertex} endVertex | |
| 5 | - * @param {number} [weight=1] | ||
| 5 | + * @param {number} [weight=0] | ||
| 6 | + * @param key | ||
| 6 | 7 | */ | |
| 7 | - constructor(startVertex, endVertex, weight = 0) { | ||
| 8 | + constructor(startVertex, endVertex, weight = 0, key = null) { | ||
| 8 | 9 | this.startVertex = startVertex; | |
| 9 | 10 | this.endVertex = endVertex; | |
| 10 | 11 | this.weight = weight; | |
| 12 | + this.key = key; | ||
| 11 | 13 | } | |
| 12 | 14 | ||
| 13 | - /** | ||
| 14 | - * @return {string} | ||
| 15 | - */ | ||
| 16 | 15 | getKey() { | |
| 16 | + if (this.key) { | ||
| 17 | + return this.key; | ||
| 18 | + } | ||
| 19 | + | ||
| 17 | 20 | const startVertexKey = this.startVertex.getKey(); | |
| 18 | 21 | const endVertexKey = this.endVertex.getKey(); | |
| 19 | 22 | ||
| 20 | - return `${startVertexKey}_${endVertexKey}`; | ||
| 23 | + this.key = `${startVertexKey}_${endVertexKey}`; | ||
| 24 | + | ||
| 25 | + return this.key; | ||
| 21 | 26 | } | |
| 22 | 27 | ||
| 23 | 28 | /** | |
@@ -35,6 +40,6 @@ export default class GraphEdge { | |||
| 35 | 40 | * @return {string} | |
| 36 | 41 | */ | |
| 37 | 42 | toString() { | |
| 38 | - return this.getKey(); | ||
| 43 | + return this.getKey().toString(); | ||
| 39 | 44 | } | |
| 40 | 45 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,8 +7,6 @@ describe('GraphEdge', () => { | |||
| 7 | 7 | const endVertex = new GraphVertex('B'); | |
| 8 | 8 | const edge = new GraphEdge(startVertex, endVertex); | |
| 9 | 9 | ||
| 10 | - expect(edge.getKey()).toBe('A_B'); | ||
| 11 | - expect(edge.toString()).toBe('A_B'); | ||
| 12 | 10 | expect(edge.startVertex).toEqual(startVertex); | |
| 13 | 11 | expect(edge.endVertex).toEqual(endVertex); | |
| 14 | 12 | expect(edge.weight).toEqual(0); | |
@@ -39,4 +37,18 @@ describe('GraphEdge', () => { | |||
| 39 | 37 | expect(edge.endVertex).toEqual(vertexA); | |
| 40 | 38 | expect(edge.weight).toEqual(10); | |
| 41 | 39 | }); | |
| 40 | + | ||
| 41 | + it('should return edges names as key', () => { | ||
| 42 | + const edge = new GraphEdge(new GraphVertex('A'), new GraphVertex('B'), 0); | ||
| 43 | + | ||
| 44 | + expect(edge.getKey()).toBe('A_B'); | ||
| 45 | + expect(edge.toString()).toBe('A_B'); | ||
| 46 | + }); | ||
| 47 | + | ||
| 48 | + it('should return custom key if defined', () => { | ||
| 49 | + const edge = new GraphEdge(new GraphVertex('A'), new GraphVertex('B'), 0, 'custom_key'); | ||
| 50 | + | ||
| 51 | + expect(edge.getKey()).toEqual('custom_key'); | ||
| 52 | + expect(edge.toString()).toEqual('custom_key'); | ||
| 53 | + }); | ||
| 42 | 54 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments