jriecken / dependency-graph

A simple dependency graph for Node.js
http://jriecken.github.io/dependency-graph/
MIT License
333 stars 49 forks source link

API method to clone a graph instance #14

Closed tivac closed 7 years ago

tivac commented 8 years ago

First, I :heart: dependency-graph and thank you so much for making it. :+1: x :100:

I've got a use-case where I need to create a copy/clone of an existing graph. It'd be really handy to that built-in so I'm not accessing private-ish properties on the graph to manually create clones of them. Thoughts?

jriecken commented 8 years ago

That seems like a great idea! Something like newGraph = oldGraph.clone().

It would have to do a deep-copy of the nodes as arbitrary data can be stored there as of the latest release.

Do you want to implement it and submit a PR? Otherwise, I'll can probably take a look in a few days!

tivac commented 8 years ago

I don't have a ton of free time for a PR right now, I've just gone ahead and built a simplified version for my use-case (where I don't store arbitrary node data).

https://github.com/tivac/modular-css/blob/master/src/lib/clone-graph.js

Cloning the arbitrary data storage part is gonna complicate things, I wonder what the right approach there would even be?

jriecken commented 8 years ago

Probably something similar to the Structured Clone Algorithm - though that's pretty complex.

I might just start with a clone implementation that doesn't copy the data (or just does a shallow copy of the data) that has a caveat in the API docs.

tivac commented 8 years ago

Based on my needs that sounds like a great first step!

jriecken commented 7 years ago

Finally got around to this - it's in version 0.6.0

tivac commented 7 years ago

@jriecken oh awesome! I got my workaround in and kinda forgot about this issue, for which I apologize. I'll give 0.6.0 a whirl when I can, thanks!