1.4.0 • Published 7 months ago
@dressed/matcher v1.4.0
@dressed/matcher
A tiny utility for creating regexes using expressive, customizable patterns.
🧩 Pattern Syntax
| Syntax | Description |
|---|---|
:<argname> | Defines a named argument |
{...} | Marks content inside as optional |
(...) | Content inside is a custom regex |
| And more! | Easily plug in your own tokens |
You can pair arguments with custom regexes to make sure the argument value matches the regex.
Input: i-love-:animal(dogs|cats)\
Output: /^i-love-(?\<animal>(?:dogs|cats))$/\
Will match:
i-love-dogsi-love-cats
Examples:
button-:variantmatchesbutton-primary,button-secondary, etc.wait{-:length}matcheswait,wait-100,wait-200, etc.- For better verboseness, you could use
wait{-:length(\d)}
- For better verboseness, you could use
ticket-(open|close)matchesticket-open,ticket-closeticket-action:(open|close)is the same as 3, except the action is captured
🧮 scorePattern()
Score the pattern based on how dynamic it is (higher is less dynamic)
"my-button"; // Granular
"my-:component"; // Less specific
":name"; // Open-ended🔮 Params<pattern>
Generates the type that a regex returned options would be.
The returned type is not completely exhaustive, but it provides a pretty good representation.
Params<"i love :animal(dogs|cats)">; // { animal: "dogs" | "cats" }