mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2026-03-13 08:51:02 +08:00
Make sure graph vertex value is converted to string
This commit is contained in:
committed by
Oleksii Trekhleb
parent
53f8c0dc24
commit
0d956c2253
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user