function cluster({ network, nodeMap, id = "id", debug = false } = {}) {
let res = { nodes: network.nodes, links: network.links };
debug && console.log("🕸️ cluster network", network);
const before = performance.now();
if (!res?.links?.length) {
throw new Error("No Links provided");
}
debug &&
console.log(
"🕸️ res.links[0] !== 'object'",
typeof res.links[0],
typeof res.links[0] !== "object"
);
if (res.links[0][id] === undefined) {
if (!nodeMap) {
nodeMap = new Map(network.nodes.map((d) => [d[id], d]));
}
res.links = network.links.map((l) => ({
...l,
source: nodeMap.get(l.source),
target: nodeMap.get(l.target)
}));
debug &&
console.log("🕸️ Clustering... converting links to pointers ", res);
}
debug &&
console.log(
"🕸️ Clustering... nodes: ",
network.nodes.length,
" links: ",
network.links.length,
res
);
netClustering.cluster(res.nodes, res.links);
debug &&
console.log(
"🕸️ Clustering computed in ",
(performance.now() - before) / 1000
);
return res;
}