Skip to content

Commit 991cf38

Browse files
committed
feat(context): Allow patterns to be RegExp
1 parent 437682a commit 991cf38

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

packages/context/src/context.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,21 @@ export class Context {
6565

6666
/**
6767
* Find bindings using the key pattern
68-
* @param pattern Key pattern with optional `*` wildcards
68+
* @param pattern Key regexp or pattern with optional `*` wildcards
6969
*/
70-
find(pattern?: string): Binding[] {
70+
find(pattern?: string | RegExp): Binding[] {
7171
let bindings: Binding[] = [];
72-
if (pattern) {
72+
let glob: RegExp | undefined = undefined;
73+
if (typeof pattern === 'string') {
7374
// TODO(@superkhau): swap with production grade glob to regex lib
7475
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) {
7681
this.registry.forEach(binding => {
77-
const isMatch = glob.test(binding.key);
82+
const isMatch = glob!.test(binding.key);
7883
if (isMatch) bindings.push(binding);
7984
});
8085
} else {
@@ -87,12 +92,15 @@ export class Context {
8792

8893
/**
8994
* 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
9196
*/
92-
findByTag(pattern: string): Binding[] {
97+
findByTag(pattern: string | RegExp): Binding[] {
9398
const bindings: Binding[] = [];
9499
// 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;
96104
this.registry.forEach(binding => {
97105
const isMatch = Array.from(binding.tags).some(tag => glob.test(tag));
98106
if (isMatch) bindings.push(binding);

packages/context/test/unit/context.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ describe('Context', () => {
9292
result = ctx.find('ba*');
9393
expect(result).to.be.eql([b2, b3]);
9494
});
95+
96+
it('returns matching binding with regexp', () => {
97+
const b1 = ctx.bind('foo');
98+
const b2 = ctx.bind('bar');
99+
const b3 = ctx.bind('baz');
100+
let result = ctx.find(/\w+/);
101+
expect(result).to.be.eql([b1, b2, b3]);
102+
result = ctx.find(/ba/);
103+
expect(result).to.be.eql([b2, b3]);
104+
});
95105
});
96106

97107
describe('findByTag', () => {
@@ -108,6 +118,15 @@ describe('Context', () => {
108118
const result = ctx.findByTag('t*');
109119
expect(result).to.be.eql([b1, b2]);
110120
});
121+
122+
it('returns matching binding with regexp', () => {
123+
const b1 = ctx.bind('foo').tag('t1');
124+
const b2 = ctx.bind('bar').tag('t2');
125+
let result = ctx.findByTag(/t/);
126+
expect(result).to.be.eql([b1, b2]);
127+
result = ctx.findByTag(/t1/);
128+
expect(result).to.be.eql([b1]);
129+
});
111130
});
112131

113132
describe('getBinding', () => {

0 commit comments

Comments
 (0)