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

Moved common, repeated strings into globals.js #101

Merged
merged 2 commits into from
Dec 8, 2017
Merged
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
2 changes: 1 addition & 1 deletion src/Bottle/constant.js
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 4 additions & 4 deletions src/Bottle/decorator.js
Original file line number Diff line number Diff line change
@@ -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] = [];
8 changes: 4 additions & 4 deletions src/Bottle/middleware.js
Original file line number Diff line number Diff line change
@@ -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] = [];
4 changes: 2 additions & 2 deletions src/Bottle/pop.js
Original file line number Diff line number Diff line change
@@ -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 = {};
8 changes: 4 additions & 4 deletions src/Bottle/provider.js
Original file line number Diff line number Diff line change
@@ -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;
@@ -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);
@@ -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] = {
4 changes: 2 additions & 2 deletions src/Bottle/reset-providers.js
Original file line number Diff line number Diff line change
@@ -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];
};

/**
@@ -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]));
}
2 changes: 1 addition & 1 deletion src/Bottle/value.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion src/export.js
Original file line number Diff line number Diff line change
@@ -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) {
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;
@@ -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);
};