g6/plugins/util.dataCleaner/index.js
huangtong.ht 6ac5e03c30 dev
2018-08-23 11:15:49 +08:00

35 lines
875 B
JavaScript

/**
* @fileOverview clean data
* @author huangtonger@aliyun.com
*/
const G6 = require('@antv/g6');
const Util = G6.Util;
const dataCleaner = {
clearData(data) {
const nodeMap = {};
const invalidEdges = [];
data.nodes.forEach(node => {
nodeMap[node.id] = node;
});
data.edges.forEach(edge => {
// console.log(edge, nodeMap[edge.source], nodeMap[edge.target]);
if (!nodeMap[edge.source]) {
console.warn('remove ', edge, 'because there is no source node');
invalidEdges.push(edge);
return;
}
if (!nodeMap[edge.target]) {
console.warn('remove ', edge, 'because there is no target node');
invalidEdges.push(edge);
return;
}
});
data.edges = data.edges.filter(edge => {
return invalidEdges.indexOf(edge) === -1;
});
}
};
Util.mix(Util, dataCleaner);