spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / mods / diff / tests / fixtures / tree / element-in.ts
39 lines · 1.1 KBRaw
1import type { ElementQuery } from './element-query.js'
2
3/**
4 * The first element in a rendered tree of that type whose `props.key` or
5 * `props.label` is the name, depth-first.
6 *
7 * @param tree what `$.ui.render` resolved to
8 * @param wanted the element's type and its key or label
9 * @returns the element as a plain record, or undefined
10 */
11export function elementIn(
12 tree: unknown,
13 wanted: ElementQuery,
14): Record<string, unknown> | undefined {
15 if (typeof tree !== 'object' || !tree) {
16 return undefined
17 }
18
19 const node: Record<string, unknown> = Object.fromEntries(Object.entries(tree))
20 const isPropped = typeof node.props === 'object' && node.props !== null
21
22 const named: Record<string, unknown> = isPropped
23 ? Object.fromEntries(Object.entries(node.props ?? {}))
24 : {}
25
26 const isMatch =
27 node.type === wanted.type &&
28 (named.key === wanted.name || named.label === wanted.name)
29
30 const children: readonly unknown[] = Array.isArray(node.children)
31 ? node.children
32 : []
33
34 return isMatch
35 ? node
36 : children
37 .map(child => elementIn(child, wanted))
38 .find(found => found !== undefined)
39}