DevimalPlanet / object-traversal

Flexible and performant utility for traversing javascript objects.
MIT License
22 stars 1 forks source link

Include traversed object with different key #64

Open Serdarrr opened 1 year ago

Serdarrr commented 1 year ago

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);
lveillard commented 11 months ago

I know its been some months, but would this fix it?

  const person = {
      name: "John Doe",
      address: {...address},
      foreignAddress: {...address}
  }