Skip to content

Commit

Permalink
favor .find to destructuring + .filter (spinnaker#2793)
Browse files Browse the repository at this point in the history
  • Loading branch information
anotherchrisberry authored Oct 4, 2016
1 parent ce22d78 commit f1d15b0
Show file tree
Hide file tree
Showing 28 changed files with 41 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = angular.module('spinnaker.loadBalancer.aws.details.controller',
$scope.InsightFilterStateModel = InsightFilterStateModel;

function extractLoadBalancer() {
let [appLoadBalancer] = app.loadBalancers.data.filter(function (test) {
let appLoadBalancer = app.loadBalancers.data.find(function (test) {
return test.name === loadBalancer.name && test.region === loadBalancer.region && test.account === loadBalancer.accountId;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ module.exports = angular
regions.forEach(region => {
var regionalVpcId = null;
if (vpcId) {
var [baseVpc] = this.vpcs.filter(vpc => vpc.id === vpcId),
[regionalVpc] = this.vpcs.filter(vpc => vpc.account === account && vpc.region === region && vpc.name === baseVpc.name);
let baseVpc = this.vpcs.find(vpc => vpc.id === vpcId),
regionalVpc = this.vpcs.find(vpc => vpc.account === account && vpc.region === region && vpc.name === baseVpc.name);
regionalVpcId = regionalVpc ? regionalVpc.id : undefined;
}

Expand Down Expand Up @@ -87,8 +87,8 @@ module.exports = angular
this.rule.name = null;
return;
}
let [baseVpc] = filtered.filter(vpc => vpc.id === this.rule.vpcId),
[regionalVpc] = filtered.filter(vpc => vpc.account === this.rule.accountName && vpc.name === baseVpc.name);
let baseVpc = filtered.find(vpc => vpc.id === this.rule.vpcId),
regionalVpc = filtered.find(vpc => vpc.account === this.rule.accountName && vpc.name === baseVpc.name);
if (regionalVpc) {
this.rule.vpcId = regionalVpc.id;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ module.exports = angular.module('spinnaker.aws.serverGroup.configure.service', [
ami: image.amis ? image.amis[command.region][0] : null
};
});
var [match] = regionalImages.filter((image) => image.imageName === command.amiName);
let match = regionalImages.find((image) => image.imageName === command.amiName);
if (command.amiName && !match) {
result.dirty.amiName = true;
command.amiName = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = angular.module('spinnaker.aws.cloneServerGroup.controller', [
if ($scope.$$destroyed) {
return;
}
let [cloneStage] = $scope.taskMonitor.task.execution.stages.filter((stage) => stage.type === 'cloneServerGroup');
let cloneStage = $scope.taskMonitor.task.execution.stages.find((stage) => stage.type === 'cloneServerGroup');
if (cloneStage && cloneStage.context['deploy.server.groups']) {
let newServerGroupName = cloneStage.context['deploy.server.groups'][$scope.command.region];
if (newServerGroupName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ module.exports = angular
results.forEach(transformAvailableMetric);
this.metrics = results.sort((a, b) => a.label.localeCompare(b.label));
let currentDimensions = alarm.dimensions.sort(dimensionSorter).map(d => d.value).join(', ');
let [selected] = this.metrics.filter(metric =>
let selected = this.metrics.find(metric =>
metric.name === alarm.metricName && metric.namespace === alarm.namespace &&
metric.dimensionValues === currentDimensions
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports = angular.module('spinnaker.serverGroup.details.aws.autoscaling.p
function getDisabledDate(serverGroup) {
if (serverGroup.isDisabled) {
let processes = normalizeScalingProcesses(serverGroup);
let [disabledProcess] = processes.filter((process) => process.name === 'AddToLoadBalancer' && !process.enabled);
let disabledProcess = processes.find((process) => process.name === 'AddToLoadBalancer' && !process.enabled);
if (disabledProcess) {
return disabledProcess.suspensionDate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = angular.module('spinnaker.azure.cloneServerGroup.controller', [
if ($scope.$$destroyed) {
return;
}
let [cloneStage] = $scope.taskMonitor.task.execution.stages.filter((stage) => stage.type === 'cloneServerGroup');
let cloneStage = $scope.taskMonitor.task.execution.stages.find((stage) => stage.type === 'cloneServerGroup');
if (cloneStage && cloneStage.context['deploy.server.groups']) {
let newServerGroupName = cloneStage.context['deploy.server.groups'][$scope.command.region];
if (newServerGroupName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = angular.module('spinnaker.serverGroup.configure.cf.cloneServerG
if ($scope.$$destroyed) {
return;
}
let [cloneStage] = $scope.taskMonitor.task.execution.stages.filter((stage) => stage.type === 'cloneServerGroup');
let cloneStage = $scope.taskMonitor.task.execution.stages.find((stage) => stage.type === 'cloneServerGroup');
if (cloneStage && cloneStage.context['deploy.server.groups']) {
let newServerGroupName = cloneStage.context['deploy.server.groups'][$scope.command.region];
if (newServerGroupName) {
Expand Down
3 changes: 1 addition & 2 deletions app/scripts/modules/core/application/application.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ export class Application {
* @param key
*/
public getDataSource(key: string): ApplicationDataSource {
let [dataSource] = this.dataSources.filter(ds => ds.key === key);
return dataSource;
return this.dataSources.find(ds => ds.key === key);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ module.exports = angular
if (ClusterFilterModel.sortFilter.listInstances && ClusterFilterModel.sortFilter.multiselect) {
let instancesSelected = 0;
MultiselectModel.instanceGroups.forEach((instanceGroup) => {
let [match] = serverGroups.filter((serverGroup) => {
let match = serverGroups.find((serverGroup) => {
return serverGroup.name === instanceGroup.serverGroup &&
serverGroup.region === instanceGroup.region &&
serverGroup.account === instanceGroup.account &&
Expand Down Expand Up @@ -291,14 +291,13 @@ module.exports = angular
}, 25);

function getCluster(application, clusterName, account, category) {
let [match] = (application.clusters || []).filter(c => c.account === account && c.name === clusterName && c.category === category);
return match;
return (application.clusters || []).find(c => c.account === account && c.name === clusterName && c.category === category);
}

function diffSubgroups(oldGroups, newGroups) {
var groupsToRemove = [];
oldGroups.forEach(function(oldGroup, idx) {
var [newGroup] = (newGroups || []).filter(group =>
let newGroup = (newGroups || []).find(group =>
group.heading === oldGroup.heading &&
group.category === oldGroup.category);
if (!newGroup) {
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/modules/core/cluster/filter/multiselect.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module.exports = angular
account = serverGroup.account,
region = serverGroup.region,
cloudProvider = serverGroup.type;
let [result] = this.instanceGroups.filter((instanceGroup) => {
let result = this.instanceGroups.find((instanceGroup) => {
return instanceGroup.serverGroup === serverGroupName &&
instanceGroup.account === account &&
instanceGroup.region === region &&
Expand Down Expand Up @@ -138,7 +138,7 @@ module.exports = angular
}
this.deselectAllInstances();
let key = this.makeServerGroupKey(serverGroup),
[selected] = this.serverGroups.filter((sg) => sg.key === key);
selected = this.serverGroups.find((sg) => sg.key === key);
if (selected) {
this.serverGroups.splice(this.serverGroups.indexOf(selected), 1);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ module.exports = angular.module('spinnaker.core.delivery.executions.service', [
function waitUntilNewTriggeredPipelineAppears(application, pipelineName, triggeredPipelineId) {

return getRunningExecutions(application.name).then(function(executions) {
var [match] = executions.filter(function(execution) {
let match = executions.find(function(execution) {
return execution.id === triggeredPipelineId;
});
var deferred = $q.defer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,8 @@ module.exports = angular.module('spinnaker.core.instance.details.multipleInstanc
*/

function getServerGroup(group) {
let [serverGroup] = app.serverGroups.data.filter((serverGroup) => serverGroup.name === group.serverGroup &&
return app.serverGroups.data.find((serverGroup) => serverGroup.name === group.serverGroup &&
serverGroup.account === group.account && serverGroup.region === group.region);

return serverGroup;
}

function getInstanceDetails(group, instanceId) {
Expand All @@ -187,8 +185,7 @@ module.exports = angular.module('spinnaker.core.instance.details.multipleInstanc
return null;
}

let [instance] = serverGroup.instances.filter((instance) => instance.id === instanceId);
return instance || {};
return serverGroup.instances.find((instance) => instance.id === instanceId) || {};
}

let makeInstanceModel = (group, instanceId) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export class LoadBalancersTagController implements ng.IComponentController {
this.application.getDataSource('loadBalancers').ready().then(() => {
let serverGroup: ServerGroup = this.serverGroup;
this.loadBalancers = serverGroup.loadBalancers.map( (lbName: string) => {
let [match] = this.application.getDataSource('loadBalancers')
let match = this.application.getDataSource('loadBalancers')
.data
.filter((lb: LoadBalancer): boolean => {
.find((lb: LoadBalancer): boolean => {
return lb.name === lbName
&& lb.account === serverGroup.account
&& lb.region === serverGroup.region
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = angular.module('spinnaker.core.pipeline.stage.executionWindows.
this.finishWaiting = () => {
let stage = $scope.stage;
let matcher = (execution) => {
let [match] = execution.stages.filter((test) => test.id === stage.id);
let match = execution.stages.find((test) => test.id === stage.id);
return match.status !== 'RUNNING';
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = angular

let provideJudgment = (execution, stage, judgment, input) => {
let matcher = (execution) => {
let [match] = execution.stages.filter((test) => test.id === stage.id);
let match = execution.stages.find((test) => test.id === stage.id);
return match && match.status !== 'RUNNING';
};
let data = {judgmentStatus: judgment, judgmentInput: input};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = angular.module('spinnaker.core.pipeline.stage.wait.executionDet
this.finishWaiting = () => {
let stage = $scope.stage;
let matcher = (execution) => {
let [match] = execution.stages.filter((test) => test.id === stage.id);
let match = execution.stages.find((test) => test.id === stage.id);
return match.status !== 'RUNNING';
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = angular.module('spinnaker.core.pipeline.config.trigger.pipeline
formatLabel: (trigger) => {

let loadSuccess = (pipelines) => {
let [pipeline] = pipelines.filter((config) => config.id === trigger.pipeline);
let pipeline = pipelines.find((config) => config.id === trigger.pipeline);
return pipeline ? `(Pipeline) ${trigger.application}: ${pipeline.name}` : '[pipeline not found]';
};

Expand Down Expand Up @@ -98,7 +98,7 @@ module.exports = angular.module('spinnaker.core.pipeline.config.trigger.pipeline
$scope.userSuppliedParameters = {};

this.updateParam = function(parameter) {
if($scope.useDefaultParameters[parameter] === true) {
if ($scope.useDefaultParameters[parameter] === true) {
delete $scope.userSuppliedParameters[parameter];
delete $scope.trigger.parameters[parameter];
} else if($scope.userSuppliedParameters[parameter]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ module.exports = angular.module('spinnaker.core.serverGroup.details.multipleServ
let retrieveServerGroups = () => {
this.serverGroups = MultiselectModel.serverGroups.map(multiselectGroup => {
let group = _.cloneDeep(multiselectGroup);
let [match] = app.serverGroups.data.filter(check => check.name === group.name && check.account === group.account && check.region === group.region);
let match = app.serverGroups.data.find(check => check.name === group.name && check.account === group.account && check.region === group.region);
if (match) {
group.instanceCounts = _.cloneDeep(match.instanceCounts);
group.disabled = match.isDisabled;
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/modules/core/subnet/subnet.read.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = angular

function getSubnetPurpose(id) {
return listSubnets().then(subnets => {
let [match] = subnets.filter(test => test.id === id);
let match = subnets.find(test => test.id === id);
return match ? match.purpose : null;
});
}
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/modules/core/task/taskProgressBar.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ module.exports = angular.module('spinnaker.core.task.progressBar.directive', [])
scope.progressStyle = { width: stepsComplete.length / task.steps.length * 100 + '%' };

if (task.isRunning) {
let [currentStep] = task.steps.filter(step => step.hasNotStarted || step.isRunning);
let currentStep = task.steps.find(step => step.hasNotStarted || step.isRunning);
if (currentStep) {
var currentStepIndex = task.steps.indexOf(currentStep) + 1;
scope.tooltip = $sce.trustAsHtml('Step ' + currentStepIndex + ' of ' + task.steps.length + ': ' + $filter('robotToHuman')(currentStep.name));
}
}

if (task.isFailed) {
var [failedStep] = task.steps.filter(step => step.isFailed || step.isSuspended);
let failedStep = task.steps.find(step => step.isFailed || step.isSuspended);

if (failedStep && task.failureMessage) {
var failedStepIndex = task.steps.indexOf(failedStep) + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = angular.module('spinnaker.serverGroup.configure.gce.cloneServer
if ($scope.$$destroyed) {
return;
}
let [cloneStage] = $scope.taskMonitor.task.execution.stages.filter((stage) => stage.type === 'cloneServerGroup');
let cloneStage = $scope.taskMonitor.task.execution.stages.find((stage) => stage.type === 'cloneServerGroup');
if (cloneStage && cloneStage.context['deploy.server.groups']) {
let newServerGroupName = cloneStage.context['deploy.server.groups'][$scope.command.region];
if (newServerGroupName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ module.exports = angular.module('spinnaker.kubernetes.clusterCommandBuilder.serv
let result = [];
containers.forEach((container) => {
if (container.imageDescription.fromContext) {
let [matchingImage] = upstreamImages.filter((image) => container.imageDescription.stageId === image.stageId);
let matchingImage = upstreamImages.find((image) => container.imageDescription.stageId === image.stageId);
if (matchingImage) {
container.imageDescription.cluster = matchingImage.cluster;
container.imageDescription.pattern = matchingImage.pattern;
container.imageDescription.repository = matchingImage.repository;
result.push(container);
}
} else if (container.imageDescription.fromTrigger) {
let [matchingImage] = upstreamImages.filter((image) => {
let matchingImage = upstreamImages.find((image) => {
return container.imageDescription.registry === image.registry
&& container.imageDescription.repository === image.repository
&& container.imageDescription.tag === image.tag;
Expand Down Expand Up @@ -176,7 +176,7 @@ module.exports = angular.module('spinnaker.kubernetes.clusterCommandBuilder.serv
});
}
current.requisiteStageRefIds.forEach(function(id) {
let [next] = all.filter((stage) => stage.refId === id);
let next = all.find((stage) => stage.refId === id);
if (next) {
result = result.concat(findUpstreamImages(next, all, visited));
}
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/modules/netflix/migrator/migrator.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ module.exports = angular
let addAccountNames = (results) => {
accountService.getAllAccountDetailsForProvider('aws').then(accounts => {
results.securityGroups.forEach(group => {
let [match] = accounts.filter(a => a.accountId === group.accountId);
let match = accounts.find(a => a.accountId === group.accountId);
group.accountName = match ? match.name : group.accountId;
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = angular
}

if (settings.feature.vpcMigrator) {
let [migrated] = $scope.application.pipelineConfigs.data.filter(test => test.name === $scope.pipeline.name + ' - vpc0');
let migrated = $scope.application.pipelineConfigs.data.find(test => test.name === $scope.pipeline.name + ' - vpc0');
if (migrated) {
$scope.migrated = migrated;
}
Expand Down Expand Up @@ -244,7 +244,7 @@ module.exports = angular
};

this.close = () => {
var [newPipeline] = application.pipelineConfigs.data.filter(test => {
let newPipeline = application.pipelineConfigs.data.find(test => {
return test.name.indexOf(this.viewState.targetName) === 0;
});
$state.go('^.pipelineConfig', {pipelineId: newPipeline.id});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = angular.module('spinnaker.loadBalancer.openstack.details.contro
$scope.InsightFilterStateModel = InsightFilterStateModel;

function extractLoadBalancer() {
let [appLoadBalancer] = app.loadBalancers.data.filter(function (test) {
let appLoadBalancer = app.loadBalancers.data.find(function (test) {
return test.name === loadBalancer.name &&
test.region === loadBalancer.region &&
test.account === loadBalancer.accountId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let angular = require('angular');
module.exports = angular.module('spinnaker.openstack.serverGroupCommandBuilder.service', [
require('../../image/image.reader.js'),
])
.factory('openstackServerGroupCommandBuilder', function ($q, openstackImageReader, subnetReader, loadBalancerReader, settings, namingService, applicationReader, openstackServerGroupTransformer) {
.factory('openstackServerGroupCommandBuilder', function ($q, openstackImageReader, subnetReader, loadBalancerReader, settings, namingService, applicationReader) {

function buildNewServerGroupCommand(application, defaults) {
defaults = defaults || {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = angular.module('spinnaker.serverGroup.configure.titus.cloneServ
if ($scope.$$destroyed) {
return;
}
let [cloneStage] = $scope.taskMonitor.task.execution.stages.filter((stage) => stage.type === 'cloneServerGroup');
let cloneStage = $scope.taskMonitor.task.execution.stages.find((stage) => stage.type === 'cloneServerGroup');
if (cloneStage && cloneStage.context['deploy.server.groups']) {
let newServerGroupName = cloneStage.context['deploy.server.groups'][$scope.command.region];
if (newServerGroupName) {
Expand Down

0 comments on commit f1d15b0

Please sign in to comment.