From c4ad6f4a5310d1f8e4b24eb5b029a778ef51e3ac Mon Sep 17 00:00:00 2001 From: Stephen Young Date: Thu, 7 Dec 2017 12:14:32 -0500 Subject: [PATCH 1/2] Using a common string delimiter. --- src/Bottle/constant.js | 2 +- src/Bottle/decorator.js | 4 ++-- src/Bottle/middleware.js | 4 ++-- src/Bottle/provider.js | 4 ++-- src/Bottle/reset-providers.js | 2 +- src/Bottle/value.js | 2 +- src/globals.js | 6 +++++- 7 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/Bottle/constant.js b/src/Bottle/constant.js index e7aa1b2..4f4bd30 100644 --- a/src/Bottle/constant.js +++ b/src/Bottle/constant.js @@ -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; diff --git a/src/Bottle/decorator.js b/src/Bottle/decorator.js index 066dd7f..11d0a12 100644 --- a/src/Bottle/decorator.js +++ b/src/Bottle/decorator.js @@ -12,10 +12,10 @@ var decorator = function decorator(fullname, func) { fullname = '__global__'; } - 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] = []; diff --git a/src/Bottle/middleware.js b/src/Bottle/middleware.js index 3f64926..7c10904 100644 --- a/src/Bottle/middleware.js +++ b/src/Bottle/middleware.js @@ -50,10 +50,10 @@ var middleware = function middleware(fullname, func) { fullname = '__global__'; } - 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] = []; diff --git a/src/Bottle/provider.js b/src/Bottle/provider.js index aebd909..b85cfe4 100644 --- a/src/Bottle/provider.js +++ b/src/Bottle/provider.js @@ -18,7 +18,7 @@ var reducer = function reducer(instance, func) { */ var provider = function provider(fullname, Provider) { var parts, name; - parts = fullname.split('.'); + parts = fullname.split(DELIMITER); if (this.providerMap[fullname] && parts.length === 1 && !this.container[fullname + 'Provider']) { return console.error(fullname + ' provider already instantiated.'); } @@ -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); diff --git a/src/Bottle/reset-providers.js b/src/Bottle/reset-providers.js index d6382e5..40bfd60 100644 --- a/src/Bottle/reset-providers.js +++ b/src/Bottle/reset-providers.js @@ -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])); } diff --git a/src/Bottle/value.js b/src/Bottle/value.js index df1b7a0..19b2139 100644 --- a/src/Bottle/value.js +++ b/src/Bottle/value.js @@ -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; diff --git a/src/globals.js b/src/globals.js index 26cb003..103e829 100644 --- a/src/globals.js +++ b/src/globals.js @@ -1,3 +1,7 @@ +/** + * String constants + */ +var DELIMITER = '.'; /** * Unique id counter; @@ -57,5 +61,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); }; From f311b6b43df95dc1cf7e20fd6344d67120c96555 Mon Sep 17 00:00:00 2001 From: Stephen Young Date: Thu, 7 Dec 2017 12:33:40 -0500 Subject: [PATCH 2/2] Moved common, repeated strings into globals.js --- src/Bottle/decorator.js | 4 ++-- src/Bottle/middleware.js | 4 ++-- src/Bottle/pop.js | 4 ++-- src/Bottle/provider.js | 4 ++-- src/Bottle/reset-providers.js | 2 +- src/export.js | 2 +- src/globals.js | 4 ++++ 7 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/Bottle/decorator.js b/src/Bottle/decorator.js index 11d0a12..4fd4508 100644 --- a/src/Bottle/decorator.js +++ b/src/Bottle/decorator.js @@ -7,9 +7,9 @@ */ 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(DELIMITER); diff --git a/src/Bottle/middleware.js b/src/Bottle/middleware.js index 7c10904..56c75de 100644 --- a/src/Bottle/middleware.js +++ b/src/Bottle/middleware.js @@ -45,9 +45,9 @@ 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(DELIMITER); diff --git a/src/Bottle/pop.js b/src/Bottle/pop.js index a63593d..f736508 100644 --- a/src/Bottle/pop.js +++ b/src/Bottle/pop.js @@ -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(); @@ -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 = {}; diff --git a/src/Bottle/provider.js b/src/Bottle/provider.js index b85cfe4..595b1e3 100644 --- a/src/Bottle/provider.js +++ b/src/Bottle/provider.js @@ -19,7 +19,7 @@ var reducer = function reducer(instance, func) { var provider = function provider(fullname, Provider) { var parts, name; parts = fullname.split(DELIMITER); - if (this.providerMap[fullname] && parts.length === 1 && !this.container[fullname + 'Provider']) { + if (this.providerMap[fullname] && parts.length === 1 && !this.container[fullname + PROVIDER_SUFFIX]) { return console.error(fullname + ' provider already instantiated.'); } this.originalProviders[fullname] = Provider; @@ -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] = { diff --git a/src/Bottle/reset-providers.js b/src/Bottle/reset-providers.js index 40bfd60..fe34a55 100644 --- a/src/Bottle/reset-providers.js +++ b/src/Bottle/reset-providers.js @@ -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]; }; /** diff --git a/src/export.js b/src/export.js index 3e9117b..8f18cf8 100644 --- a/src/export.js +++ b/src/export.js @@ -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) { diff --git a/src/globals.js b/src/globals.js index 103e829..3fd0a3a 100644 --- a/src/globals.js +++ b/src/globals.js @@ -2,6 +2,10 @@ * String constants */ var DELIMITER = '.'; +var FUNCTION_TYPE = 'function'; +var STRING_TYPE = 'string'; +var GLOBAL_NAME = '__global__'; +var PROVIDER_SUFFIX = 'Provider'; /** * Unique id counter;