Using ID from existing randomly generated ID without a circular dependency

Hi,

I need to feed mock data into my treeview component for performance testing.

My treeview expects a FLAT item list - Item[] - where Item is a TypeScript interface

interface Item {
  name: string;
  id: string;
  parentID?: string;
}

Now, how do I randomly create a data set where the parentID will be using a random ID from within the same set AND no circular dependency be created (an item should not have a parentID referring to one of its descendants’ id).

So for example this should not be allowed:

[
  {name: 'ABC', id: 'abc', parentID: 'def'},
  {name: 'DEF', id: 'def', parentID: 'abc'},
  {name: 'ROOT', id: 'root'},
]

Please note that I am use the term data set in a general meaning, not necessarily matching the mockaroo concept of dataset.

Let’s start with this schema:

Can you explain a little more of the constraints?

Just create a set of items and try to build a valid tree using ID and parentID. The constraints are very intuitive, just as in a case where you try to build a folder/file structure. There cannot be circular references where a descendant of an item would be referred to as an ancestor of that item.

In the example I gave there is a circ reference because (the first array item) ABC is marked as a child of def, but DEF is then marked as child of abc (the next array item).

Also an indirect circular reference, such as abc > def > ghi > jkl > abc would be invalid too.

If you try to move a folder into any of its descendants, OS would give you an error message. So this explains the constraint: use parentID from within a set of ID, but in such a way only that the reference don’t form a circle.