object-traversal icon indicating copy to clipboard operation
object-traversal copied to clipboard

Include traversed object with different key

Open Serdarrr opened this issue 1 year ago • 1 comments

Hello, nice and fast traversing lib. I am trying to filter an large hierarchiel Open API schema object, beforehand I dereferenced the schema's to one root object. I am using this object to search and filter properties. So far traversing is working but it excludes properties with same values. I believe it has to do with line 86 in traverse.ts where it skips nodes (values) that have already been visited.

Is there a workaround to include nodes that have different keys but are using the same objects/ models which are visited?

Example Lets say I have a Person model with two address properties that have the same Address model (see input code). When I run the code below, I will only get access to person.address, but not person.foreignAddress because it is not included. I would like to all the nodes included when traversing.

When I set cycleHandling option to off, it works but I need this otherwise I will have performance issues where the maximum size will exceed.

Input

let address = {
      address: {
          street: {
              type: "string"
          },
          houseNumber: {
              type: "integer"
          },
          postalCode: {
              type: "string"
          }
      }
  };
  const person = {
      name: "John Doe",
      address: address,
      foreignAddress: address
  }

Code

let nodes = []
function filterData(n) {
    nodes.push(n)
}

traverse(person, filterData);

Serdarrr avatar Jun 26 '23 15:06 Serdarrr