Skip to content

Commit

Permalink
Merge pull request #4 from catberry/develop
Browse files Browse the repository at this point in the history
Release 2.2.0
  • Loading branch information
Denis Rechkunov authored Aug 23, 2016
2 parents a702f05 + 1aa12de commit 33ffad3
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 5 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
charset = utf-8

[**.{js,json}]
indent_style = tab
indent_size = 2
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
notifications:
webhooks:
urls:
- https://webhooks.gitter.im/e/c862b3dde2aeef762073
- https://webhooks.gitter.im/e/5763fef442c720162f9a
on_success: always # options: [always|never|change] default: always
on_failure: always # options: [always|never|change] default: always
on_start: true # default: false
language: node_js
sudo: false
node_js:
- "4"
- "5"
- "6"
14 changes: 14 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
comment:
layout: header, changes, diff, sunburst
coverage:
notify:
gitter:
default:
threshold: '0'
url: https://webhooks.gitter.im/e/e0fbec98c76c1614706a
webhook:
default:
threshold: '0'
url: https://webhooks.gitter.im/e/4d9a9af137bfbc20d8f2
status:
changes: false
16 changes: 14 additions & 2 deletions lib/ServiceLocator.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ServiceLocator {
*/
registerInstance(type, instance) {
this._throwIfNotString(type);
this._initializeRegistration(type, this);
this._initializeRegistration(type);

this._registrations[type].unshift({
Implementation: instance.constructor,
Expand Down Expand Up @@ -89,6 +89,18 @@ class ServiceLocator {
this._registrations[type] = [];
}

/**
* The function checks whether a type exists and if there is at least one instance
*
* @param {string} type The type name
* @returns {boolean}
*/
has(type) {
this._throwIfNotString(type);

return (type in this._registrations && this._registrations[type].length > 0);
}

/**
* Creates an instance for the specified registration descriptor.
* @param {Object} registration The registration descriptor object.
Expand All @@ -100,7 +112,7 @@ class ServiceLocator {
return registration.singleInstance;
}

// inject Service Locator as the only argument of the costructor.
// inject Service Locator as the only argument of the constructor.
const instance = new registration.Implementation(this);

if (registration.isSingleton) {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "catberry-locator",
"version": "2.1.0",
"version": "2.2.0",
"author": "Denis Rechkunov <[email protected]>",
"description": "Service Locator for Catberry Framework",
"homepage": "https://github.com/catberry/catberry-locator",
Expand All @@ -24,8 +24,8 @@
"devDependencies": {
"istanbul": "~0.4.0",
"codecov": "^1.0.1",
"mocha": "^2.4.5",
"eslint": "^2.0.0"
"mocha": "^3.0.2",
"eslint": "^3.3.1"
},
"engines": {
"node": ">=4"
Expand Down
36 changes: 36 additions & 0 deletions test/lib/ServiceLocator.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,40 @@ describe('ServiceLocator', function() {
});
});
});

describe('#has', function() {
it('should return true when it has the registered singleton implementation type by the name', function() {
class Implementation1 { }

locator.register('type', Implementation1, true);

const has = locator.has('type');
assert.strictEqual(has, true);
});

it('should return true when it has the registered type by the name', function() {
class Implementation1 { }

locator.register('type', Implementation1);

const has = locator.has('type');
assert.strictEqual(has, true);
});

it('should return true when it has multiple the registered implementations of the type name', function() {
class Implementation1 { }
function Implementation2() { }

locator.register('type', Implementation1);
locator.register('type', Implementation2);

const has = locator.has('type');
assert.strictEqual(has, true);
});

it('should return false when it does not have the registered type by the name', function() {
const has = locator.has('not exists');
assert.strictEqual(has, false);
});
});
});

0 comments on commit 33ffad3

Please sign in to comment.