@@ -65,16 +65,21 @@ export class Context {
65
65
66
66
/**
67
67
* Find bindings using the key pattern
68
- * @param pattern Key pattern with optional `*` wildcards
68
+ * @param pattern Key regexp or pattern with optional `*` wildcards
69
69
*/
70
- find ( pattern ?: string ) : Binding [ ] {
70
+ find ( pattern ?: string | RegExp ) : Binding [ ] {
71
71
let bindings : Binding [ ] = [ ] ;
72
- if ( pattern ) {
72
+ let glob : RegExp | undefined = undefined ;
73
+ if ( typeof pattern === 'string' ) {
73
74
// TODO(@superkhau): swap with production grade glob to regex lib
74
75
Binding . validateKey ( pattern ) ;
75
- const glob = new RegExp ( '^' + pattern . split ( '*' ) . join ( '.*' ) + '$' ) ;
76
+ glob = new RegExp ( '^' + pattern . split ( '*' ) . join ( '.*' ) + '$' ) ;
77
+ } else if ( pattern instanceof RegExp ) {
78
+ glob = pattern ;
79
+ }
80
+ if ( glob ) {
76
81
this . registry . forEach ( binding => {
77
- const isMatch = glob . test ( binding . key ) ;
82
+ const isMatch = glob ! . test ( binding . key ) ;
78
83
if ( isMatch ) bindings . push ( binding ) ;
79
84
} ) ;
80
85
} else {
@@ -87,12 +92,15 @@ export class Context {
87
92
88
93
/**
89
94
* Find bindings using the tag pattern
90
- * @param pattern Tag pattern with optional `*` wildcards
95
+ * @param pattern Tag name regexp or pattern with optional `*` wildcards
91
96
*/
92
- findByTag ( pattern : string ) : Binding [ ] {
97
+ findByTag ( pattern : string | RegExp ) : Binding [ ] {
93
98
const bindings : Binding [ ] = [ ] ;
94
99
// TODO(@superkhau): swap with production grade glob to regex lib
95
- const glob = new RegExp ( '^' + pattern . split ( '*' ) . join ( '.*' ) + '$' ) ;
100
+ const glob =
101
+ typeof pattern === 'string'
102
+ ? new RegExp ( '^' + pattern . split ( '*' ) . join ( '.*' ) + '$' )
103
+ : pattern ;
96
104
this . registry . forEach ( binding => {
97
105
const isMatch = Array . from ( binding . tags ) . some ( tag => glob . test ( tag ) ) ;
98
106
if ( isMatch ) bindings . push ( binding ) ;
0 commit comments