@dlenroc/tag-expression
v1.0.0
Published
Parser and evaluator for boolean tag expressions
Maintainers
Readme
@dlenroc/tag-expression
Parser and evaluator for boolean tag expressions.
Installation
npm install @dlenroc/tag-expressionUsage
Parse once, evaluate against many items:
import { parse } from "@dlenroc/tag-expression";
const result = parse("catalog & !end-to-end");
if (!result.ok) {
throw new Error(result.error);
}
const items = [
{ name: "A", tags: new Set(["catalog", "smoke"]) },
{ name: "B", tags: new Set(["catalog", "end-to-end"]) },
{ name: "C", tags: new Set(["shipping"]) },
];
const matched = items.filter((item) => result.value.evaluate(item.tags));
// [{ name: 'A', tags: Set { 'catalog', 'smoke' } }]Syntax
Tag expressions are boolean expressions with the operators !, & and |.
Parentheses ( ) can be used to adjust for operator precedence.
Two special expressions are supported: any() matches when any tags are present,
and none() matches when no tags are present. Both can be combined with other
expressions just like regular tags.
Operators
| Operator | Meaning | Associativity |
| -------- | ------- | ------------- |
| ! | not | right |
| & | and | left |
| \| | or | left |
Tag rules
A tag must not be blank and must not contain whitespace or any of the
following reserved characters: ( ) & | !
Examples
| Expression | Selection |
| ------------------------------------------------ | ----------------------------------------------------------- |
| product | product |
| catalog \| shipping | catalog or shipping |
| catalog & shipping | catalog and shipping |
| product & !end-to-end | product, excluding end-to-end |
| (micro \| integration) & (product \| shipping) | micro or integration within product or shipping |
