Skip to content

Commit

Permalink
feat(context): Allow patterns to be RegExp
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Nov 21, 2017
1 parent 437682a commit 991cf38
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/context/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,21 @@ export class Context {

/**
* Find bindings using the key pattern
* @param pattern Key pattern with optional `*` wildcards
* @param pattern Key regexp or pattern with optional `*` wildcards
*/
find(pattern?: string): Binding[] {
find(pattern?: string | RegExp): Binding[] {
let bindings: Binding[] = [];
if (pattern) {
let glob: RegExp | undefined = undefined;
if (typeof pattern === 'string') {
// TODO(@superkhau): swap with production grade glob to regex lib
Binding.validateKey(pattern);
const glob = new RegExp('^' + pattern.split('*').join('.*') + '$');
glob = new RegExp('^' + pattern.split('*').join('.*') + '$');
} else if (pattern instanceof RegExp) {
glob = pattern;
}
if (glob) {
this.registry.forEach(binding => {
const isMatch = glob.test(binding.key);
const isMatch = glob!.test(binding.key);
if (isMatch) bindings.push(binding);
});
} else {
Expand All @@ -87,12 +92,15 @@ export class Context {

/**
* Find bindings using the tag pattern
* @param pattern Tag pattern with optional `*` wildcards
* @param pattern Tag name regexp or pattern with optional `*` wildcards
*/
findByTag(pattern: string): Binding[] {
findByTag(pattern: string | RegExp): Binding[] {
const bindings: Binding[] = [];
// TODO(@superkhau): swap with production grade glob to regex lib
const glob = new RegExp('^' + pattern.split('*').join('.*') + '$');
const glob =
typeof pattern === 'string'
? new RegExp('^' + pattern.split('*').join('.*') + '$')
: pattern;
this.registry.forEach(binding => {
const isMatch = Array.from(binding.tags).some(tag => glob.test(tag));
if (isMatch) bindings.push(binding);
Expand Down
19 changes: 19 additions & 0 deletions packages/context/test/unit/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ describe('Context', () => {
result = ctx.find('ba*');
expect(result).to.be.eql([b2, b3]);
});

it('returns matching binding with regexp', () => {
const b1 = ctx.bind('foo');
const b2 = ctx.bind('bar');
const b3 = ctx.bind('baz');
let result = ctx.find(/\w+/);
expect(result).to.be.eql([b1, b2, b3]);
result = ctx.find(/ba/);
expect(result).to.be.eql([b2, b3]);
});
});

describe('findByTag', () => {
Expand All @@ -108,6 +118,15 @@ describe('Context', () => {
const result = ctx.findByTag('t*');
expect(result).to.be.eql([b1, b2]);
});

it('returns matching binding with regexp', () => {
const b1 = ctx.bind('foo').tag('t1');
const b2 = ctx.bind('bar').tag('t2');
let result = ctx.findByTag(/t/);
expect(result).to.be.eql([b1, b2]);
result = ctx.findByTag(/t1/);
expect(result).to.be.eql([b1]);
});
});

describe('getBinding', () => {
Expand Down

0 comments on commit 991cf38

Please sign in to comment.