Skip to content

Commit

Permalink
refactor: Replace _.reduce with array.reduce (#7883)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvdai2401 committed Jun 26, 2020
1 parent bfa3a61 commit 297f7d8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 85 deletions.
3 changes: 1 addition & 2 deletions lib/classes/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,7 @@ functionalities related to given service or current environment.`
// Get all the commands using getCommands() with filtered entrypoint
// commands and reduce to the required command.
const allCommands = this.serverless.pluginManager.getCommands();
const command = _.reduce(
commandsArray,
const command = commandsArray.reduce(
(currentCmd, cmd) => {
if (currentCmd.commands && cmd in currentCmd.commands) {
return currentCmd.commands[cmd];
Expand Down
53 changes: 20 additions & 33 deletions lib/classes/PluginManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,13 @@ class PluginManager {
}

const splitAlias = alias.split(':');
const aliasTarget = _.reduce(
splitAlias,
(__, aliasPath) => {
const currentAlias = __;
if (!currentAlias[aliasPath]) {
currentAlias[aliasPath] = {};
}
return currentAlias[aliasPath];
},
this.aliases
);
const aliasTarget = splitAlias.reduce((__, aliasPath) => {
const currentAlias = __;
if (!currentAlias[aliasPath]) {
currentAlias[aliasPath] = {};
}
return currentAlias[aliasPath];
}, this.aliases);
// Check if the alias is already defined
if (aliasTarget.command) {
throw new this.serverless.classes.Error(
Expand All @@ -205,16 +201,12 @@ class PluginManager {
}
// Check if the alias would overwrite an exiting command
if (
_.reduce(
splitAlias,
(__, aliasPath) => {
if (!__ || !__.commands || !__.commands[aliasPath]) {
return null;
}
return __.commands[aliasPath];
},
this
)
splitAlias.reduce((__, aliasPath) => {
if (!__ || !__.commands || !__.commands[aliasPath]) {
return null;
}
return __.commands[aliasPath];
}, this)
) {
throw new this.serverless.classes.Error(`Command "${alias}" cannot be overriden by an alias`);
}
Expand Down Expand Up @@ -380,16 +372,12 @@ class PluginManager {

getAliasCommandTarget(aliasArray) {
// Check if the command references an alias
const aliasCommand = _.reduce(
aliasArray,
(__, commandPath) => {
if (!__ || !__[commandPath]) {
return null;
}
return __[commandPath];
},
this.aliases
);
const aliasCommand = aliasArray.reduce((__, commandPath) => {
if (!__ || !__[commandPath]) {
return null;
}
return __[commandPath];
}, this.aliases);

return _.get(aliasCommand, 'command');
}
Expand All @@ -407,8 +395,7 @@ class PluginManager {
const aliasCommandTarget = this.getAliasCommandTarget(commandsArray);
const commandOrAlias = aliasCommandTarget ? aliasCommandTarget.split(':') : commandsArray;

return _.reduce(
commandOrAlias,
return commandOrAlias.reduce(
(current, name, index) => {
const commandExists = name in current.commands;
const isNotContainer = commandExists && current.commands[name].type !== 'container';
Expand Down
26 changes: 11 additions & 15 deletions lib/classes/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,17 @@ class Utils {
}

isEventUsed(functions, eventName) {
return _.reduce(
functions,
(accum, func) => {
const events = func.events || [];
if (events.length) {
events.forEach(event => {
if (Object.keys(event)[0] === eventName) {
accum = true; // eslint-disable-line no-param-reassign
}
});
}
return accum;
},
false
);
return Object.keys(functions).reduce((accum, key) => {
const events = functions[key].events || [];
if (events.length) {
events.forEach(event => {
if (Object.keys(event)[0] === eventName) {
accum = true; // eslint-disable-line no-param-reassign
}
});
}
return accum;
}, false);
}
}

Expand Down
34 changes: 13 additions & 21 deletions lib/plugins/aws/package/compile/events/apiGateway/lib/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,10 @@ module.exports = {
node.children.forEach(child => getNodePaths(result, child));
}

return _.reduce(
trees,
(result, tree) => {
getNodePaths(result, tree);
return result;
},
{}
);
return trees.reduce((result, tree) => {
getNodePaths(result, tree);
return result;
}, {});
},

getResourcePaths() {
Expand Down Expand Up @@ -166,19 +162,15 @@ module.exports = {
_.some(predefinedResourceNodes, node => node.path === event.http.path)
)
) {
return _.reduce(
predefinedResources,
(resourceMap, resource) => {
const r = resourceMap;
r[resource.path] = resource;

if (!resource.name) {
r[resource.path].name = this.provider.naming.normalizePath(resource.path);
}
return r;
},
{}
);
return predefinedResources.reduce((resourceMap, resource) => {
const r = resourceMap;
r[resource.path] = resource;

if (!resource.name) {
r[resource.path].name = this.provider.naming.normalizePath(resource.path);
}
return r;
}, {});
}

// cut resource branches from trees
Expand Down
24 changes: 10 additions & 14 deletions lib/plugins/aws/package/compile/events/cognitoUserPool/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,20 +308,16 @@ class AwsCompileCognitoUserPoolEvents {
}

generateTemplateForPool(poolName, currentPoolTriggerFunctions) {
const lambdaConfig = _.reduce(
currentPoolTriggerFunctions,
(result, value) => {
const lambdaLogicalId = this.provider.naming.getLambdaLogicalId(value.functionName);

// Return a new object to avoid lint errors
return Object.assign({}, result, {
[value.triggerSource]: {
'Fn::GetAtt': [lambdaLogicalId, 'Arn'],
},
});
},
{}
);
const lambdaConfig = currentPoolTriggerFunctions.reduce((result, value) => {
const lambdaLogicalId = this.provider.naming.getLambdaLogicalId(value.functionName);

// Return a new object to avoid lint errors
return Object.assign({}, result, {
[value.triggerSource]: {
'Fn::GetAtt': [lambdaLogicalId, 'Arn'],
},
});
}, {});

const userPoolLogicalId = this.provider.naming.getCognitoUserPoolLogicalId(poolName);

Expand Down

0 comments on commit 297f7d8

Please sign in to comment.