gqty
Version:
The No-GraphQL Client for TypeScript
16 lines (14 loc) • 436 B
JavaScript
function select(node, path, onNext) {
const probedNode = onNext ? onNext(node, path) : node;
if (path.length === 0) {
return node;
} else if (probedNode == null || typeof probedNode !== "object") {
return void 0;
}
if (Array.isArray(probedNode)) {
return probedNode.map((item) => select(item, path, onNext));
}
const [key, ...rest] = path;
return select(probedNode[key], rest, onNext);
}
export { select };