youtube-maze/explore/graph.js

54 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const util = require('util');
const GRAPH_NODE = ' "%s" [label="%s"]';
const GRAPH_NODE_URL = ' "%s" [label="%s", URL="%s", fontcolor=blue]';
const GRAPH_LINK = ' "%s" -> "%s"';
/**
* Convertit un graphe en format DOT.
*
* @param graph Graphe à convertir.
* @param [title=identity] Fonction donnant le titre de chaque nœud du graphe.
* @param [url=none] Fonction donnant lURL de chaque nœud du graphe, ou la
* chaîne vide si un nœud na pas dURL.
* @return Représentation DOT du graphe.
*/
const graphToDOT = (graph, title = id => id, url = () => '') =>
{
const ser = graph.serialize();
// Conversion des nœuds de vidéo
const nodes = ser.nodes.map(({id}) =>
{
const nodeTitle = title(id);
const nodeUrl = url(id);
if (url === '')
{
return util.format(GRAPH_NODE, id, nodeTitle);
}
else
{
return util.format(GRAPH_NODE_URL, id, nodeTitle, nodeUrl);
}
}
).join('\n');
// Conversion des liens de vidéo
const links = ser.links.map(({source, target}) =>
util.format(GRAPH_LINK, source, target)
).join('\n');
return (
'digraph epenser {\n'
+ nodes
+ '\n'
+ links
+ '\n}'
);
};
exports.graphToDOT = graphToDOT;