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
Parameter | Type | Description |
---|---|---|
tagName | string | |
subtree | TreeNode | (Optional) |
Returns:
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);
}