Rush StackShopBlogEvents
Skip to main content

Home > @rushstack/tree-pattern > TreePattern > tag

TreePattern.tag() method

Labels a subtree within the search pattern, so that the matching object can be retrieved.

Signature:

static tag(tagName: string, subtree?: TreeNode): TreeNode;

Parameters

ParameterTypeDescription
tagNamestring
subtreeTreeNode(Optional)

Returns:

TreeNode

Remarks

Used to build the pattern tree for TreePattern.match(). For the given subtree of the pattern, if it is matched, that node will be assigned to the captures object using tagName as the key.

Example:

const myCaptures: { personName?: string } = {};
const myPattern = {
name: TreePattern.tag('personName')
};
if (myPattern.match({ name: 'Bob' }, myCaptures)) {
console.log(myCaptures.personName);
}