Skip to content

Commit

Permalink
Merge pull request #101 from young-steveo/string-constants
Browse files Browse the repository at this point in the history
Moved common, repeated strings into globals.js
  • Loading branch information
young-steveo authored Dec 8, 2017
2 parents 97ef4e3 + f311b6b commit d7e7157
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Bottle/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @return Bottle
*/
var constant = function constant(name, value) {
var parts = name.split('.');
var parts = name.split(DELIMITER);
name = parts.pop();
defineConstant.call(parts.reduce(setValueObject, this.container), name, value);
return this;
Expand Down
8 changes: 4 additions & 4 deletions src/Bottle/decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
*/
var decorator = function decorator(fullname, func) {
var parts, name;
if (typeof fullname === 'function') {
if (typeof fullname === FUNCTION_TYPE) {
func = fullname;
fullname = '__global__';
fullname = GLOBAL_NAME;
}

parts = fullname.split('.');
parts = fullname.split(DELIMITER);
name = parts.shift();
if (parts.length) {
getNestedBottle.call(this, name).decorator(parts.join('.'), func);
getNestedBottle.call(this, name).decorator(parts.join(DELIMITER), func);
} else {
if (!this.decorators[name]) {
this.decorators[name] = [];
Expand Down
8 changes: 4 additions & 4 deletions src/Bottle/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ var applyMiddleware = function applyMiddleware(middleware, name, instance, conta
*/
var middleware = function middleware(fullname, func) {
var parts, name;
if (typeof fullname === 'function') {
if (typeof fullname === FUNCTION_TYPE) {
func = fullname;
fullname = '__global__';
fullname = GLOBAL_NAME;
}

parts = fullname.split('.');
parts = fullname.split(DELIMITER);
name = parts.shift();
if (parts.length) {
getNestedBottle.call(this, name).middleware(parts.join('.'), func);
getNestedBottle.call(this, name).middleware(parts.join(DELIMITER), func);
} else {
if (!this.middlewares[name]) {
this.middlewares[name] = [];
Expand Down
4 changes: 2 additions & 2 deletions src/Bottle/pop.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var bottles = {};
*/
var pop = function pop(name) {
var instance;
if (typeof name === 'string') {
if (typeof name === STRING_TYPE) {
instance = bottles[name];
if (!instance) {
bottles[name] = instance = new Bottle();
Expand All @@ -31,7 +31,7 @@ var pop = function pop(name) {
* Clear all named bottles.
*/
var clear = function clear(name) {
if (typeof name === 'string') {
if (typeof name === STRING_TYPE) {
delete bottles[name];
} else {
bottles = {};
Expand Down
8 changes: 4 additions & 4 deletions src/Bottle/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var reducer = function reducer(instance, func) {
*/
var provider = function provider(fullname, Provider) {
var parts, name;
parts = fullname.split('.');
if (this.providerMap[fullname] && parts.length === 1 && !this.container[fullname + 'Provider']) {
parts = fullname.split(DELIMITER);
if (this.providerMap[fullname] && parts.length === 1 && !this.container[fullname + PROVIDER_SUFFIX]) {
return console.error(fullname + ' provider already instantiated.');
}
this.originalProviders[fullname] = Provider;
Expand All @@ -28,7 +28,7 @@ var provider = function provider(fullname, Provider) {
name = parts.shift();

if (parts.length) {
getNestedBottle.call(this, name).provider(parts.join('.'), Provider);
getNestedBottle.call(this, name).provider(parts.join(DELIMITER), Provider);
return this;
}
return createProvider.call(this, name, Provider);
Expand Down Expand Up @@ -57,7 +57,7 @@ var createProvider = function createProvider(name, Provider) {
container = this.container;
decorators = this.decorators;
middlewares = this.middlewares;
providerName = name + 'Provider';
providerName = name + PROVIDER_SUFFIX;

properties = Object.create(null);
properties[providerName] = {
Expand Down
4 changes: 2 additions & 2 deletions src/Bottle/reset-providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
var removeProviderMap = function resetProvider(name) {
delete this.providerMap[name];
delete this.container[name];
delete this.container[name + 'Provider'];
delete this.container[name + PROVIDER_SUFFIX];
};

/**
Expand All @@ -18,7 +18,7 @@ var removeProviderMap = function resetProvider(name) {
var resetProviders = function resetProviders() {
var providers = this.originalProviders;
Object.keys(this.originalProviders).forEach(function resetPrvider(provider) {
var parts = provider.split('.');
var parts = provider.split(DELIMITER);
if (parts.length > 1) {
parts.forEach(removeProviderMap, getNestedBottle.call(this, parts[0]));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bottle/value.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
var value = function value(name, val) {
var parts;
parts = name.split('.');
parts = name.split(DELIMITER);
name = parts.pop();
defineValue.call(parts.reduce(setValueObject, this.container), name, val);
return this;
Expand Down
2 changes: 1 addition & 1 deletion src/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var objectTypes = {
/**
* Export
*/
if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {
if (typeof define === FUNCTION_TYPE && typeof define.amd === 'object' && define.amd) {
root.Bottle = Bottle;
define(function() { return Bottle; });
} else if (freeExports && freeModule) {
Expand Down
10 changes: 9 additions & 1 deletion src/globals.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* String constants
*/
var DELIMITER = '.';
var FUNCTION_TYPE = 'function';
var STRING_TYPE = 'string';
var GLOBAL_NAME = '__global__';
var PROVIDER_SUFFIX = 'Provider';

/**
* Unique id counter;
Expand Down Expand Up @@ -57,5 +65,5 @@ var getNestedBottle = function getNestedBottle(name) {
* @return Service
*/
var getNestedService = function getNestedService(fullname) {
return fullname.split('.').reduce(getNested, this);
return fullname.split(DELIMITER).reduce(getNested, this);
};

0 comments on commit d7e7157

Please sign in to comment.