Skip to content

Commit

Permalink
Some small code tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed Aug 21, 2018
1 parent 3813306 commit af9b50e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
25 changes: 10 additions & 15 deletions packages/commons/lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ exports.makeArguments = function makeArguments (hook) {
// Converts different hook registration formats into the
// same internal format
exports.convertHookData = function convertHookData (obj) {
var hook = {};
let hook = {};

if (Array.isArray(obj)) {
hook = { all: obj };
Expand Down Expand Up @@ -150,11 +150,8 @@ exports.processHooks = function processHooks (hooks, initialHookObject) {

return hookObject;
};
// First step of the hook chain with the initial hook object
let promise = Promise.resolve(hookObject);

// Go through all hooks and chain them into our promise
hooks.forEach(fn => {
const promise = hooks.reduce((promise, fn) => {
const hook = fn.bind(this);

if (hook.length === 2) { // function(hook, next)
Expand All @@ -168,16 +165,14 @@ exports.processHooks = function processHooks (hooks, initialHookObject) {
}

// Use the returned hook object or the old one
promise = promise.then(updateCurrentHook);
});
return promise.then(updateCurrentHook);
}, Promise.resolve(hookObject));

return promise
.then(() => hookObject)
.catch(error => {
// Add the hook information to any errors
error.hook = hookObject;
throw error;
});
return promise.then(() => hookObject).catch(error => {
// Add the hook information to any errors
error.hook = hookObject;
throw error;
});
};

// Add `.hooks` functionality to an object
Expand All @@ -186,7 +181,7 @@ exports.enableHooks = function enableHooks (obj, methods, types) {
return obj;
}

let __hooks = {};
const __hooks = {};

types.forEach(type => {
// Initialize properties where hook functions are stored
Expand Down
8 changes: 4 additions & 4 deletions packages/commons/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ const _ = exports._ = {
},

pick (source, ...keys) {
const result = {};
keys.forEach(key => {
return keys.reduce((result, key) => {
if (source[key] !== undefined) {
result[key] = source[key];
}
});
return result;

return result;
}, {});
},

// Recursively merge the source object into the target object
Expand Down

0 comments on commit af9b50e

Please sign in to comment.