Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Context bindings - Finding bindings" #61

Merged
merged 1 commit into from
Feb 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/loopback/lib/context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import {Binding} from './binding';

export class Context {
private registry: Map<string, any>;
private registry: Map<string, Binding>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should support string|Symbol ...can do in another PR

Copy link
Contributor Author

@superkhau superkhau Feb 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created follow up issue at #67, we can discuss priority during planning.


constructor() {
this.registry = new Map();
Expand All @@ -27,6 +27,23 @@ export class Context {
return this.registry.has(key);
}

find(pattern: string): Binding[] {
let bindings = [];
if (pattern) {
// TODO: swap with production grade glob to regex lib
const glob = new RegExp('^' + pattern.split('*').join('.*') + '$');
this.registry.forEach(binding => {
const isMatch = glob.test(binding.key);
if (isMatch)
bindings.push(binding);
});
} else {
bindings = Array.from(this.registry.values());
}

return bindings;
}

get(key: string) {
const binding = this.registry.get(key);
return binding.value;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Feature: Context Binding - Finding bindings

## Scenario: Finding all bindings

- Given a context
- And a binding with key `foo` bound to the value `bar`
- And another binding with key `bat` bound to the value `baz`
- When I find all bindings
- Then I get a list of all bindings

```ts
// create a container for bindings
const ctx = new Context();

// creating two simple bindings
ctx.bind('foo').to('bar');
ctx.bind('baz').to('qux');

// find all bindings
const bindings = ctx.find();

const keys = bindings.map(binding => {
return binding.key;
});
console.log(keys); // => ['foo', 'baz']
```

## Scenario: Finding bindings by pattern

- Given a context
- And a binding with key `my.foo` bound to the value `bar`
- And another binding with key `my.baz` bound to the value `qux`
- When I find all bindings using a pattern
- Then I get a list of all bindings matching the pattern

```ts
// create a container for bindings
const ctx = new Context({
delimiter: '.' // default
});

// creating three simple bindings
ctx.bind('my.foo').to('bar');
ctx.bind('my.baz').to('qux');
ctx.bind('ur.quux').to('quuz');

// find all bindings
const bindings = ctx.find('my.*');

const keys = bindings.map(binding => binding.key)
console.log(keys); // => ['my.foo', 'my.baz']
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright IBM Corp. 2013,2017. All Rights Reserved.
// Node module: loopback
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {expect} from '@loopback/testlab';
import * as util from 'loopback/test/support/util';

describe('Context bindings - Finding bindings', () => {
let ctx;

describe('Finding all binding', () => {
before('given a context', createContext);
before('with two simple bindings', () => {
createBinding('foo', 'bar');
createBinding('baz', 'qux');
});

describe('when I find all bindings', () => {
it('returns all bindings', () => {
const bindings = ctx.find();
const keys = bindings.map(binding => {
return binding.key;
});
expect(keys).to.include('foo', 'baz');
});
});

});

describe('Finding bindings by pattern', () => {
before('given a context', createContext);
before('with namespaced bindings', () => {
createBinding('my.foo', 'bar');
createBinding('my.baz', 'qux');
createBinding('ur.quux', 'quuz');
});

describe('when I find all bindings using a pattern', () => {
it('returns all bindings matching the pattern', () => {
const bindings = ctx.find('my.*');
const keys = bindings.map(binding => binding.key);
expect(keys).to.include('my.foo', 'my.baz');
expect(keys).to.not.include('ur.quux');
});
});
});

function createContext() {
ctx = util.getContext();
}
function createBinding(key, value) {
ctx.bind(key).to(value);
}
});