Skip to Content
Bear documentation

query

Import from bear/engine/query.

Classes

BearQuery

Defined in: bear/engine/query.ts:246

Composable query over Bear nodes.

The one rule: find goes down; everything else stays put.

A query holds an immutable current set of nodes. find searches all descendants of the current set. Every other method filters the current set (named, ofType, geometry, assemblies, where, has), hops it structurally (children, parent, ancestors, descendants, siblings, closest), follows relationship edges (follow, trace, with edges for the records themselves), positions within it (nth), or terminates it (one, first, at, all, iteration).

Layered selection falls out of chaining, because the current set is the scope of the next find:

q("Level-1").find(/^Wall/).nth(2).find("Post").one()

AND is a filter chained after a search; OR is variadic selectors:

q().find(NominalLumber).named(/^HDR/)   // lumber that is ALSO named HDR-*
q().find("BP", /^BP-/)                  // either selector

Terminals are strict: one and first throw with the chain's provenance when the set is empty, so ad-hoc scripts fail loudly instead of silently operating on nothing. at is the intentionally quiet, nullable accessor.

A query is re-iterable rather than a one-shot iterator, so it goes straight into anything that takes nodes (for (const n of query), new Set(query), [...query]) and stays safe to hold in a variable and pass around.

Type Parameters

Type ParameterDefault type
N extends TreeNodeTreeNode

Implements

  • Iterable<N>

Constructors

Constructor
new BearQuery<N>(
   nodes, 
   path?, 
   hint?
): BearQuery<N>;

Defined in: bear/engine/query.ts:253

Parameters
ParameterTypeDefault value
nodesN | readonly N[]undefined
path?string"q(…)"
hint?stringundefined
Returns

BearQuery<N>

Accessors

length
Get Signature
get length(): number;

Defined in: bear/engine/query.ts:695

Number of matched nodes.

Returns

number

Methods

[iterator]()
[iterator](): IterableIterator<N>;

Defined in: bear/engine/query.ts:709

Iterate the matched nodes, so a query goes straight into anything that takes nodes: for (const n of query), new Set(query), [...query]. Re-iterable, not a one-shot iterator.

Returns

IterableIterator<N>

Implementation of

Iterable.[iterator]

all()
all(): readonly N[];

Defined in: bear/engine/query.ts:690

All matched nodes as an array.

Returns

readonly N[]

ancestors()
ancestors(): BearQuery<Phase<N>>;

Defined in: bear/engine/query.ts:454

Walk up from each node (excluding the node itself). Nearest first.

Returns

BearQuery<Phase<N>>

assemblies()
assemblies(): BearQuery<Extract<N, {
  kind: "assembly";
}>>;

Defined in: bear/engine/query.ts:398

Keep assembly nodes: the grouping branches.

Returns

BearQuery<Extract<N, { kind: "assembly"; }>>

at()
at(index): N | undefined;

Defined in: bear/engine/query.ts:685

The i-th node or undefined: the only quiet, nullable accessor. Negative indices count from the end, like Array.prototype.at.

Parameters
ParameterType
indexnumber
Returns

N | undefined

children()
children(): BearQuery<Phase<N>>;

Defined in: bear/engine/query.ts:437

Direct children of each node.

Returns

BearQuery<Phase<N>>

closest()
Call Signature
closest(predicate): BearQuery<Phase<N>>;

Defined in: bear/engine/query.ts:490

Nearest ancestor of each node matching any selector (deduplicated). The node itself is not eligible: like find, but pointed up.

Parameters
ParameterType
predicate(node) => boolean
Returns

BearQuery<Phase<N>>

Call Signature
closest(...selectors): BearQuery<Phase<N>>;

Defined in: bear/engine/query.ts:491

Nearest ancestor of each node matching any selector (deduplicated). The node itself is not eligible: like find, but pointed up.

Parameters
ParameterType
...selectorsSelector[]
Returns

BearQuery<Phase<N>>

descendants()
descendants(): BearQuery<Phase<N>>;

Defined in: bear/engine/query.ts:470

Full subtree under each node, excluding the node itself. Equivalent to .find() with no selector; kept as the explicit hop.

Returns

BearQuery<Phase<N>>

edges()
edges(kind?): readonly Edge[];

Defined in: bear/engine/query.ts:594

The relationship edge records incident to the current set or its descendants, optionally filtered by kind. Returns records, not nodes: the accessor for edge props (fastener schedules, flow demands).

q("Deck").edges("bears")          // every bearing edge inside the deck
q(hanger).edges("fastens")[0]!.props.count
Parameters
ParameterType
kind?string
Returns

readonly Edge[]

find()
Call Signature
find<P>(selector): BearQuery<Phase<N> & object>;

Defined in: bear/engine/query.ts:310

Search all descendants of the current set for nodes matching any of the given Selectors. The current set itself is not eligible: find goes down. With no selectors, returns all descendants.

q().find(Drywall)                  // every drywall node in the tree
q("Level-1").find(/^Wall/)         // walls under Level-1
Type Parameters
Type Parameter
P extends object
Parameters
ParameterType
selectorPartDef<P>
Returns

BearQuery<Phase<N> & object>

Call Signature
find(predicate): BearQuery<Phase<N>>;

Defined in: bear/engine/query.ts:313

Search all descendants of the current set for nodes matching any of the given Selectors. The current set itself is not eligible: find goes down. With no selectors, returns all descendants.

q().find(Drywall)                  // every drywall node in the tree
q("Level-1").find(/^Wall/)         // walls under Level-1
Parameters
ParameterType
predicate(node) => boolean
Returns

BearQuery<Phase<N>>

Call Signature
find(...selectors): BearQuery<Phase<N>>;

Defined in: bear/engine/query.ts:314

Search all descendants of the current set for nodes matching any of the given Selectors. The current set itself is not eligible: find goes down. With no selectors, returns all descendants.

q().find(Drywall)                  // every drywall node in the tree
q("Level-1").find(/^Wall/)         // walls under Level-1
Parameters
ParameterType
...selectorsSelector[]
Returns

BearQuery<Phase<N>>

first()
first(): N;

Defined in: bear/engine/query.ts:673

First node. Throws when the set is empty; use at when an absent match is an expected, handled case rather than a bug.

Returns

N

follow()
follow(kind, opts?): BearQuery<TreeNode>;

Defined in: bear/engine/query.ts:523

Follow relationship edges of kind one hop from each node in the current set: with the edge's direction by default, against it with { dir: "in" }. Results are deduplicated. Edges exist only on a model's tree; trace is the transitive closure.

q("Stud-3").follow("bears")             // what the stud bears on
q(lav).follow("feeds", { dir: "in" })   // the branch feeding this fixture
Parameters
ParameterType
kindstring
opts?{ dir?: "in" | "out"; }
opts.dir?"in" | "out"
Returns

BearQuery<TreeNode>

geometry()
geometry(): BearQuery<Extract<N, {
  kind: "geometry";
}>>;

Defined in: bear/engine/query.ts:390

Keep geometry nodes: the physical parts with manifold and extents.

Returns

BearQuery<Extract<N, { kind: "geometry"; }>>

has()
Call Signature
has(predicate): BearQuery<N>;

Defined in: bear/engine/query.ts:423

Keep nodes whose subtree contains a match. "Walls that have a Post":

q().find(/^Wall/).has("Post")
Parameters
ParameterType
predicate(node) => boolean
Returns

BearQuery<N>

Call Signature
has(...selectors): BearQuery<N>;

Defined in: bear/engine/query.ts:424

Keep nodes whose subtree contains a match. "Walls that have a Post":

q().find(/^Wall/).has("Post")
Parameters
ParameterType
...selectorsSelector[]
Returns

BearQuery<N>

isEmpty()
isEmpty(): boolean;

Defined in: bear/engine/query.ts:700

True when nothing matched.

Returns

boolean

named()
named(...selectors): BearQuery<N>;

Defined in: bear/engine/query.ts:344

Keep the members of the current set whose label matches an exact string or a RegExp (see Selector for what "label" means). Never descends: q(wall).children().named("Sheathing") is the direct child, where .find("Sheathing") after the hop would search grandchildren and below.

Parameters
ParameterType
...selectorsreadonly (string | RegExp)[]
Returns

BearQuery<N>

nth()
nth(index): BearQuery<N>;

Defined in: bear/engine/query.ts:643

The i-th match as a query, so the chain can continue: q().find(/^Wall/).nth(2).find("Post") selects posts of the third wall. Negative indices count from the end. Out of range yields an empty query, which the strict terminals then report loudly.

Parameters
ParameterType
indexnumber
Returns

BearQuery<N>

ofType()
Call Signature
ofType<P>(partDef): BearQuery<N & object>;

Defined in: bear/engine/query.ts:372

Keep nodes whose type IS the given component (identity comparison, not name comparison). A PartDef matches the node's part type and narrows the result's props type; an assembly component matches the nodes its expansion produced. If nothing matches but a same-named component with a different identity exists in the set, the strict terminals say so; the usual cause is importing a second copy of the component's module.

Type Parameters
Type Parameter
P extends object
Parameters
ParameterType
partDefPartDef<P>
Returns

BearQuery<N & object>

Call Signature
ofType(assembly): BearQuery<N>;

Defined in: bear/engine/query.ts:376

Keep nodes whose type IS the given component (identity comparison, not name comparison). A PartDef matches the node's part type and narrows the result's props type; an assembly component matches the nodes its expansion produced. If nothing matches but a same-named component with a different identity exists in the set, the strict terminals say so; the usual cause is importing a second copy of the component's module.

Parameters
ParameterType
assemblyAssemblyFn<any>
Returns

BearQuery<N>

one()
one(): N;

Defined in: bear/engine/query.ts:655

Exactly one node. Throws when the set is empty or ambiguous, naming the chain and the extra matches. The workhorse terminal for tests and agent scripts: a typo fails loudly, and so does an accidental multi-match.

Returns

N

parent()
parent(): BearQuery<Phase<N>>;

Defined in: bear/engine/query.ts:445

Immediate parent of each node (deduplicated).

Returns

BearQuery<Phase<N>>

siblings()
siblings(): BearQuery<Phase<N>>;

Defined in: bear/engine/query.ts:475

Siblings (same parent, excluding self).

Returns

BearQuery<Phase<N>>

system()
system(name): BearQuery<N>;

Defined in: bear/engine/query.ts:615

Keep nodes inside the named system (an ancestor System assembly published the tag). A filter, like the others: it never descends.

q().find(SupplyTrace).system("DomesticCold")
Parameters
ParameterType
namestring
Returns

BearQuery<N>

trace()
trace(kind, opts?): BearQuery<TreeNode>;

Defined in: bear/engine/query.ts:556

The transitive closure of the kind's transfer semantics: every node reachable from the current set, nearest first, excluding the set itself. For a containment kind (bears), an element that declares no edge transfers to its enclosing assembly, so a load path runs member → assembly → assembly's declared supports and ends at the tree root when nothing grounds it. follow stays declared edges only.

q(joist).trace("bears")                 // the load path to the ground
q(pier).trace("bears", { dir: "in" })   // everything this pier carries
Parameters
ParameterType
kindstring
opts?{ dir?: "in" | "out"; }
opts.dir?"in" | "out"
Returns

BearQuery<TreeNode>

where()
Call Signature
where<M>(predicate): BearQuery<M>;

Defined in: bear/engine/query.ts:410

Keep nodes matching an arbitrary predicate: the escape hatch when no built-in filter fits. Type-guard predicates narrow the result, so .where(isGeometryNode) types like .geometry().

Type Parameters
Type Parameter
M extends TreeNode
Parameters
ParameterType
predicate(node) => node is M
Returns

BearQuery<M>

Call Signature
where(predicate): BearQuery<N>;

Defined in: bear/engine/query.ts:411

Keep nodes matching an arbitrary predicate: the escape hatch when no built-in filter fits. Type-guard predicates narrow the result, so .where(isGeometryNode) types like .geometry().

Parameters
ParameterType
predicate(node) => boolean
Returns

BearQuery<N>

Interfaces

QueryFn()

Defined in: bear/engine/query.ts:60

A query entry bound to one model, from queryOf: what a REPL or a test harness hands out as q. Everything q accepts, plus the forms that need a whole tree to start from: q() and the selector sugar.

Call Signature

QueryFn(): BearQuery<TreeNode>;

Defined in: bear/engine/query.ts:61

A query entry bound to one model, from queryOf: what a REPL or a test harness hands out as q. Everything q accepts, plus the forms that need a whole tree to start from: q() and the selector sugar.

Returns

BearQuery<TreeNode>

Call Signature

QueryFn<P>(selector): BearQuery<TreeNode & object>;

Defined in: bear/engine/query.ts:62

A query entry bound to one model, from queryOf: what a REPL or a test harness hands out as q. Everything q accepts, plus the forms that need a whole tree to start from: q() and the selector sugar.

Type Parameters
Type Parameter
P extends object
Parameters
ParameterType
selectorPartDef<P>
Returns

BearQuery<TreeNode & object>

Call Signature

QueryFn(selector): BearQuery<TreeNode>;

Defined in: bear/engine/query.ts:66

A query entry bound to one model, from queryOf: what a REPL or a test harness hands out as q. Everything q accepts, plus the forms that need a whole tree to start from: q() and the selector sugar.

Parameters
ParameterType
selectorAssemblyFn<any>
Returns

BearQuery<TreeNode>

Call Signature

QueryFn(selector): BearQuery<TreeNode>;

Defined in: bear/engine/query.ts:67

A query entry bound to one model, from queryOf: what a REPL or a test harness hands out as q. Everything q accepts, plus the forms that need a whole tree to start from: q() and the selector sugar.

Parameters
ParameterType
selectorstring | RegExp | ((node) => boolean)
Returns

BearQuery<TreeNode>

Call Signature

QueryFn<N>(node): BearQuery<N>;

Defined in: bear/engine/query.ts:70

A query entry bound to one model, from queryOf: what a REPL or a test harness hands out as q. Everything q accepts, plus the forms that need a whole tree to start from: q() and the selector sugar.

Type Parameters
Type Parameter
N extends TreeNode
Parameters
ParameterType
nodeN | Iterable<N, any, any> | readonly N[]
Returns

BearQuery<N>

Type Aliases

Selector

type Selector = 
  | string
  | RegExp
  | PartDef<any>
  | AssemblyFn<any>
  | ((node) => boolean);

Defined in: bear/engine/query.ts:46

What find, has, closest, and the q() entry accept. One vocabulary everywhere:

  • string: exact match on the node's label (see below)
  • RegExp: pattern match on the label
  • PartDef: part-type identity, same semantics as BearQuery.ofType
  • assembly component: identity match against the components whose expansion produced the node (q(Wall) finds every node a <Wall> returned, wrapper components included)
  • predicate: arbitrary (node) => boolean

A node's label is its name prop when present, and otherwise its FQN leaf segment (which covers label props, the part-type name, and dedup suffixes like Wall-3). Strings and RegExps match either form, so both named("W") (three walls all named "W") and find("W-3") (the third of them, by deduped segment) work. What printTree shows is what you can query for.

Components and predicates are both plain functions; they are told apart by identity, not name: every assembly the collector expands is registered, so a registered function is a component selector and is never called. (Runtime names are rewritten by minification, so a capitalized name is only a dev-time fallback for components that never materialized.) Keep predicates anonymous or camelCase, and if a predicate returns a JSX element the query throws rather than treating the truthy element as a match.

Functions

matchesAny()

function matchesAny(node, selectors): boolean;

Defined in: bear/engine/query.ts:137

Parameters

ParameterType
nodeTreeNode
selectorsreadonly Selector[]

Returns

boolean


q()

Call Signature

function q(result): BearQuery<TreeNode>;

Defined in: bear/engine/query.ts:737

Start a query.

q(result)        // a model: its whole tree
q(node)          // scope to a subtree
q(nodes)         // explicit set: an array, another query, any iterable

The engine has no notion of a current model. A host that has one (the REPL, the test harness) binds it with queryOf and hands out that function, which adds q() for the whole tree and the selector sugar (q(Drywall), q("Level-1")).

Parameters
ParameterType
resultModelView
Returns

BearQuery<TreeNode>

Call Signature

function q<N>(node): BearQuery<N>;

Defined in: bear/engine/query.ts:738

Start a query.

q(result)        // a model: its whole tree
q(node)          // scope to a subtree
q(nodes)         // explicit set: an array, another query, any iterable

The engine has no notion of a current model. A host that has one (the REPL, the test harness) binds it with queryOf and hands out that function, which adds q() for the whole tree and the selector sugar (q(Drywall), q("Level-1")).

Type Parameters
Type Parameter
N extends TreeNode
Parameters
ParameterType
nodeN | readonly N[] | Iterable<N, any, any>
Returns

BearQuery<N>


queryOf()

function queryOf(result): QueryFn;

Defined in: bear/engine/query.ts:768

Bind q to one model. The result is what a host hands out as q: with no argument it is the whole tree; with a selector it searches every node of the tree, root included (unlike .find(), which is strictly below the current set, so q(Building) selects a root-level assembly); with a node or nodes it is plain q.

Parameters

ParameterType
resultModelView

Returns

QueryFn

On this page