frontend-collective / frontend-collective/react-sortable-tree

Support for comparator on isDescendant

Open
#447 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
5k
Forks
914
PR merge metrics
No merged PRs in 30d

Description

So I have come across a situation where I need to check if a folder is a descendant of another folder, but the object has changed shape but the ID is the same. Currently, isDescendant is comparing the references of the two objects. I would like to add a feature that would support the ability for the user to define their own comparing method (is comparator the right terminology here?).

```js
// example implementation (untested)
export function isDescendant(older, younger, comparator) {
return (
!!older.children &&
typeof older.children !== 'function' &&
older.children.some((child) => {
if (comparator && typeof comparator === 'function') {
return comparator(older, younger) || isDescendant(child, younger, comparator)
} else {
return child === younger || isDescendant(child, younger)
}
})
)
}

// example usage
isDescendant(node1, node2, (parent, child) => {
return parent.id === child.parent_id
})
```

If anyone is interested I can spend some time thinking more into this and submit a PR.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by locating the isDescendant entry point and inspect how its current reference comparison is covered. Clarify the comparator API against the example, then add coverage for matching changed-shape nodes while preserving reference-based behavior when no comparator is supplied. Done means the new option works for the parent/child relationship described and existing callers remain valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.