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

TreePattern.oneOf() method

Used to specify alternative possible subtrees in the search pattern.

Signature:

static oneOf(possibleSubtrees: TreeNode[]): TreeNode;

Parameters

Parameter Type Description
possibleSubtrees TreeNode[]  

Returns:

TreeNode

Remarks

Used to build the pattern tree for TreePattern.match(). Allows several alternative patterns to be matched for a given subtree.

Example:

const myPattern: TreePattern = new TreePattern({
  animal: TreePattern.oneOf([
    { kind: 'dog', bark: 'loud' },
    { kind: 'cat', meow: 'quiet' }
  ])
});
if (myPattern.match({ animal: { kind: 'dog', bark: 'loud' } })) {
  console.log('I can match dog.');
}
if (myPattern.match({ animal: { kind: 'cat', meow: 'quiet' } })) {
  console.log('I can match cat, too.');
}