From b36cdf2db6ee25f7defe6f2c02dd40e1d5cb65c4 Mon Sep 17 00:00:00 2001 From: pjmattingly Date: Thu, 28 Jul 2022 04:42:51 -0600 Subject: [PATCH] refactor: Replace `_.flatten` with `Array.prototype.flat` (#11271) --- lib/plugins/aws/deploy/lib/check-for-changes.js | 7 ++++--- lib/plugins/aws/metrics.js | 9 ++++----- .../package/compile/events/api-gateway/lib/api-keys.js | 5 ++++- .../package/compile/events/websockets/lib/deployment.js | 7 ++++--- lib/plugins/aws/utils/get-s3-objects-from-stacks.js | 4 +--- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/plugins/aws/deploy/lib/check-for-changes.js b/lib/plugins/aws/deploy/lib/check-for-changes.js index ca79e7f5f..704e85c99 100644 --- a/lib/plugins/aws/deploy/lib/check-for-changes.js +++ b/lib/plugins/aws/deploy/lib/check-for-changes.js @@ -255,8 +255,9 @@ module.exports = { const partition = account.partition; const functionNames = await this.serverless.service.getAllFunctions(); - const cloudwatchLogEvents = _.flatten( - functionNames.map((functionName) => { + + const cloudwatchLogEvents = functionNames + .map((functionName) => { const functionObj = this.serverless.service.getFunction(functionName); const FunctionName = functionObj.name; const events = functionObj.events; @@ -272,7 +273,7 @@ module.exports = { return { FunctionName, functionName, logGroupName, logSubscriptionSerialNumber }; }); }) - ); + .flat(); const cloudwatchLogEventsMap = _.groupBy(cloudwatchLogEvents, 'logGroupName'); const logGroupNames = Object.keys(cloudwatchLogEventsMap); diff --git a/lib/plugins/aws/metrics.js b/lib/plugins/aws/metrics.js index aaeed29e8..ffae2f73f 100644 --- a/lib/plugins/aws/metrics.js +++ b/lib/plugins/aws/metrics.js @@ -113,11 +113,10 @@ class AwsMetrics { if (metrics && metrics.length > 0) { const getDatapointsByLabel = (Label) => - _.flatten( - _.flatten(metrics) - .filter((metric) => metric.Label === Label) - .map((metric) => metric.Datapoints) - ); + metrics + .flat(2) + .filter((metric) => metric.Label === Label) + .map((metric) => metric.Datapoints); const invocationsCount = _.sumBy(getDatapointsByLabel('Invocations'), 'Sum'); const throttlesCount = _.sumBy(getDatapointsByLabel('Throttles'), 'Sum'); diff --git a/lib/plugins/aws/package/compile/events/api-gateway/lib/api-keys.js b/lib/plugins/aws/package/compile/events/api-gateway/lib/api-keys.js index 600e2807c..7faddd42c 100644 --- a/lib/plugins/aws/package/compile/events/api-gateway/lib/api-keys.js +++ b/lib/plugins/aws/package/compile/events/api-gateway/lib/api-keys.js @@ -43,7 +43,10 @@ module.exports = { if ( _.isObject(apiKeyDefinition) && Array.isArray(usagePlan) && - _.flatten(usagePlan.map((item) => Object.keys(item))).includes(name) + usagePlan + .map((item) => Object.keys(item)) + .flat() + .includes(name) ) { keyNumber = 0; apiKeyDefinition[name].forEach((key) => { diff --git a/lib/plugins/aws/package/compile/events/websockets/lib/deployment.js b/lib/plugins/aws/package/compile/events/websockets/lib/deployment.js index 60cf98780..c47dde97c 100644 --- a/lib/plugins/aws/package/compile/events/websockets/lib/deployment.js +++ b/lib/plugins/aws/package/compile/events/websockets/lib/deployment.js @@ -6,8 +6,8 @@ const pickWebsocketsTemplatePart = require('./pick-websockets-template-part'); module.exports = { compileDeployment() { - const dependentResourceIds = _.flatten( - this.validated.events.map((event) => { + const dependentResourceIds = this.validated.events + .map((event) => { const result = []; if (event.routeResponseSelectionExpression) { result.push(this.provider.naming.getWebsocketsRouteResponseLogicalId(event.route)); @@ -15,7 +15,8 @@ module.exports = { result.push(this.provider.naming.getWebsocketsRouteLogicalId(event.route)); return result; }) - ); + .flat(); + const websocketsTemplatePart = pickWebsocketsTemplatePart( this.serverless.service.provider.compiledCloudFormationTemplate, this.provider.naming.getWebsocketsApiLogicalId() diff --git a/lib/plugins/aws/utils/get-s3-objects-from-stacks.js b/lib/plugins/aws/utils/get-s3-objects-from-stacks.js index 82ad62131..ad8316818 100644 --- a/lib/plugins/aws/utils/get-s3-objects-from-stacks.js +++ b/lib/plugins/aws/utils/get-s3-objects-from-stacks.js @@ -1,8 +1,6 @@ 'use strict'; -const _ = require('lodash'); - module.exports = (stacks, prefix, service, stage) => - _.flatten(stacks).map((entry) => ({ + stacks.flat().map((entry) => ({ Key: `${prefix}/${service}/${stage}/${entry.directory}/${entry.file}`, }));