Skip to content

Commit 0d956c2

Browse files
Pierstovaltrekhleb
authored andcommitted
Make sure graph vertex value is converted to string
1 parent 53f8c0d commit 0d956c2

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/data-structures/graph/GraphVertex.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,6 @@ export default class GraphVertex {
133133
* @returns {string}
134134
*/
135135
toString(callback) {
136-
return callback ? callback(this.value) : `${this.value}`;
136+
return callback ? callback(this.value).toString() : this.value.toString();
137137
}
138138
}

src/data-structures/graph/__test__/GraphVertex.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,20 @@ describe('GraphVertex', () => {
185185

186186
expect(vertexA.getEdges().length).toEqual(3);
187187
});
188+
189+
it('should execute callback when passed to toString', () => {
190+
const vertex = new GraphVertex('A');
191+
192+
expect(vertex.toString(() => 'B')).toEqual('B');
193+
});
194+
195+
it('should execute toString on value when calling toString on vertex', () => {
196+
const value = {
197+
toString() { return 'A'; },
198+
};
199+
200+
const vertex = new GraphVertex(value);
201+
202+
expect(vertex.toString()).toEqual('A');
203+
});
188204
});

0 commit comments

Comments
 (0)