mobx-keystone
Version:
A MobX powered state management solution based on data trees with first class support for TypeScript, snapshots, patches and much more
37 lines (31 loc) • 914 B
text/typescript
import { assertTweakedObject } from "../tweaker/core"
import { fastGetParent } from "./path"
/**
* Returns if the target is a "child" of the tree of the given "parent" object.
*
* @param child Target object.
* @param parent Parent object.
* @returns
*/
export function isChildOfParent(child: object, parent: object): boolean {
assertTweakedObject(child, "child")
assertTweakedObject(parent, "parent")
let currentParent = fastGetParent(child, true)
while (currentParent) {
if (currentParent === parent) {
return true
}
currentParent = fastGetParent(currentParent, true)
}
return false
}
/**
* Returns if the target is a "parent" that has in its tree the given "child" object.
*
* @param parent Target object.
* @param child Child object.
* @returns
*/
export function isParentOfChild(parent: object, child: object): boolean {
return isChildOfParent(child, parent)
}