I am working with jQuery like elements from the cheerio library to manipulate SVG images.
These objects represent XML nodes and have a hierarchical internal structure.
I am writing a function (in JavaScript of course ) that visits each child node and, if it is a Basic Shape (ellipse, circle, rect...), calculates a set of points for the shape and invokes a user supplied function with the point set and element information.
Along the way, the function keeps track of all transform operations that have been specified upto that node, so that when it generates the point set for a shape, it will apply the accumulated scale, rotate, translate etc operations for that node.
The user supplied function will do things like generate polygon objects or calculate bounding boxes.
Is there a standard design pattern for this kind of function? It seems vaguely like a Visitor pattern but in that case the nodes would have a Visitor interface and invoke an operation themselves. I don't want to modify the cheerio objects. Furthermore, the nodes don't know where they are in the tree and cannot supply their transformation context.
This is not a difficult routine to write but I am interested to know if there is any formal theory that's relevant?
I don't want to modify the cheerio objects
-- Then you can't use the Visitor pattern, unless you wrap it in another object that can hold your ongoing state operations, and then construct and return a brand new tree when you're done. Alternatively, you can write a recursive function that constructs a new tree from the existing one.