Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. export class Graph {
  2. static merge = (g1, g2) => {
  3. const mergedNodes = { ...g1.nodes, ...g2.nodes };
  4. let mergedEdges = {};
  5. const g2Edges = {
  6. ...g2.edges,
  7. };
  8.  
  9.  
  10. Object
  11. .keys(g1.edges)
  12. .forEach((g1SourceId) => {
  13. if (g2Edges[g1SourceId]) {
  14. mergedEdges[g1SourceId] = [...g1.edges[g1SourceId], ...g2Edges[g1SourceId]];
  15. delete g2Edges[g1SourceId];
  16. } else {
  17. mergedEdges[g1SourceId] = [...g1.edges[g1SourceId]];
  18. }
  19. });
  20.  
  21. mergedEdges = {
  22. ...mergedEdges,
  23. ...g2Edges,
  24. };
  25.  
  26. return new Graph(mergedNodes, mergedEdges);
  27. };
  28.  
  29. static empty = {
  30. edges: {},
  31. nodes: {},
  32. };
  33.  
  34. constructor(nodes, edges) {
  35. this.nodes = nodes || {};
  36. this.edges = edges || {};
  37. }
  38.  
  39.  
  40. addNode(node) {
  41. this.nodes[node.id] = { ...node };
  42. }
  43.  
  44.  
  45. connect(subGraph, sourceId, targetId, connectionParams) {
  46. const connected = Graph.merge(this, subGraph);
  47.  
  48. this.nodes = connected.nodes;
  49. this.edges = connected.edges;
  50.  
  51. const { isDirect } = connectionParams;
  52. if (isDirect) {
  53. if (!this.edges[sourceId]) this.edges[sourceId] = [];
  54. if (this.edges[sourceId].indexOf(targetId) === -1) this.edges[sourceId].push(targetId);
  55. } else {
  56. if (!this.edges[targetId]) this.edges[targetId] = [];
  57. if (this.edges[targetId].indexOf(sourceId) === -1) this.edges[targetId].push(sourceId);
  58. }
  59. }
  60. }
  61.  
  62. export const toGraph = (entity, connections, type) => {
  63. const graph = new Graph();
  64. const connectionFields = Object.keys(connections);
  65.  
  66. Object.keys(entity).forEach((fieldName) => {
  67. if (!connectionFields.includes(fieldName)) return;
  68.  
  69. const field = entity[fieldName];
  70. const isFieldAnArray = field instanceof Array;
  71. const isFieldANumber = typeof field === 'number';
  72.  
  73. if (!isFieldAnArray) {
  74. if (isFieldANumber) {
  75. graph.connect(Graph.empty, entity.id, field, connections[fieldName]);
  76. } else {
  77. const subGraph = toGraph(field, connections, connections[fieldName].type);
  78. graph.connect(subGraph, entity.id, field.id, connections[fieldName]);
  79. }
  80. } else {
  81. field.forEach((subEntity) => {
  82. const subGraph = toGraph(subEntity, connections, connections[fieldName].type);
  83. graph.connect(subGraph, entity.id, subEntity.id, connections[fieldName]);
  84. });
  85. }
  86. });
  87.  
  88. const clearEntity = {
  89. ...entity,
  90. __typename: type || 'Not typed',
  91. };
  92.  
  93. connectionFields.forEach(connection => delete clearEntity[connection]);
  94. graph.addNode(clearEntity);
  95.  
  96. return graph;
  97. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement