Skip to content

Commit

Permalink
#17 - readability cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Trevino committed Mar 25, 2015
1 parent 445f662 commit 2d261f7
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/jefferson.js
Expand Up @@ -34,25 +34,41 @@ module.exports = (app, conf) => {
return lastProxy;
};

/**
* Resolves an alias reference
* @param aliasName
* @param middlewareChain
*/
let resolveAlias = (aliasName) => {
if (!conf.aliases) {
throw new Error(`no aliases defined, cannot find ${aliasName}`);
}
if (!conf.aliases[aliasName]) {
throw new Error(`could not find handler chain with alias ${aliasName}`);
}
return conf.aliases[aliasName];
};

/**
* Resolves alias references in a middleware cahin
* @param middleware
*/
let resolveAliases = (middleware) => {
let isAlias = (x) => typeof x === 'string';
for (let i = middleware.length -1; i >= 0; i--) {
if (isAlias(middleware[i])) {
middleware.splice(i, 1, resolveAlias(middleware[i]));
}
}
};

/**
* Configures a middleware chain
* @param middleware An array of middleware functions
* @returns {*} An array of middleware functions
*/
let configureMiddleware = (middleware) => {
for (let i = middleware.length -1; i >= 0; i--) {
let item = middleware[i];
if (typeof item === 'string') {
if (!conf.aliases) {
throw new Error(`no aliases defined, cannot find ${item}`);
}
if (!conf.aliases[item]) {
throw new Error(`could not find handler chain with alias ${item}`);
}
let subchain = conf.aliases[item];
middleware.splice(i, 1, subchain);
}
}
resolveAliases(middleware);
return middleware.map(wrapInProxies);
};

Expand Down

0 comments on commit 2d261f7

Please sign in to comment.