mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 07:43:04 +08:00
Make sure graph vertex value is converted to string
This commit is contained in:
parent
37f3d54aff
commit
9465d0e4c4
@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user