Source: graph/basic.js

  1. /**
  2. * @overview Includes basic graph functions like creating a graph or cloning it.
  3. */
  4. import cloneObj from 'lodash/fp/clone'
  5. import curry from 'lodash/fp/curry'
  6. import {create, isomorph as cIsomorph} from '../compound'
  7. import {isomorphComponents} from './component'
  8. import {setMetaKey} from './meta'
  9. import {packageVersion} from '../internals'
  10. /**
  11. * Creates a new graph that has the exact same nodes and edges.
  12. * @param {PortGraph} graph The graph to clone
  13. * @returns {PortGraph} A clone of the input graph.
  14. */
  15. export function clone (graph) {
  16. return cloneObj(graph)
  17. }
  18. /**
  19. * Compares two graphs for structural equality (i.e. tests if they are isomorph, where it is
  20. * allowed to change the ids and paths of every node).
  21. * @param {Portgraph} graph1 One of the graphs to compare.
  22. * @param {Portgraph} graph2 The other the graph to compare.
  23. * @returns {boolean} True if both graphs are structually equal, false otherwise.
  24. */
  25. export const isomorph = curry((graph1, graph2) => {
  26. return cIsomorph(graph1, graph2) && isomorphComponents(graph1, graph2)
  27. })
  28. /**
  29. * Create a new compound node. Each compound node is itself a graph that can contain further nodes.
  30. * @param {Node} node The node that should be converted into a compound node.
  31. * @returns {PortGraph} The graph representing the compound node.
  32. */
  33. export const compound = create
  34. /**
  35. * Returns a new empty graph.
  36. * @returns {PortGraph} A new empty port graph.
  37. */
  38. export function empty () {
  39. return setMetaKey('version', packageVersion(), create())
  40. }