From d103a22fa0d93b777a595cccf13724989d64522c Mon Sep 17 00:00:00 2001 From: Joel McKinnon Date: Thu, 23 Apr 2020 10:38:44 -0700 Subject: [PATCH] Lodash upgrade (#2885) * upgraded lodash, changed method names * native implementations as requested --- .eslintrc.js | 14 +++++++++++++- package.json | 3 ++- platform/telemetry/src/TelemetryCapability.js | 9 +++++---- src/api/composition/DefaultCompositionProvider.js | 4 ++-- src/api/telemetry/DefaultMetadataProvider.js | 6 +++--- src/api/telemetry/TelemetryAPI.js | 4 ++-- src/api/telemetry/TelemetryMetadataManager.js | 8 ++++---- .../imagery/components/ImageryViewLayout.vue | 2 +- src/plugins/plot/src/chart/MCTChartController.js | 2 +- .../plot/src/telemetry/StackedPlotController.js | 3 ++- .../src/telemetry/SummaryWidgetEvaluator.js | 2 +- .../telemetryTable/TelemetryTableConfiguration.js | 2 +- .../collections/SortedTableRowCollection.js | 4 ++-- 13 files changed, 39 insertions(+), 24 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 3cf7ec2e20..f7ffebb956 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -8,9 +8,11 @@ module.exports = { "globals": { "_": "readonly" }, + "plugins": ["lodash"], "extends": [ "eslint:recommended", - "plugin:vue/recommended" + "plugin:vue/recommended", + "plugin:lodash/recommended" ], "parser": "vue-eslint-parser", "parserOptions": { @@ -22,6 +24,16 @@ module.exports = { } }, "rules": { + "lodash/prefer-lodash-method": "off", + "lodash/prefer-lodash-typecheck": "off", + "lodash/prefer-constant": "off", + "lodash/prefer-noop": "off", + "lodash/prefer-matches": "off", + "lodash/prefer-includes": "off", + "lodash/prefer-startswith": "off", + "lodash/prefer-get": "off", + "lodash/prefer-is-nil": "off", + "lodash/import-scope": "off", "no-bitwise": "error", "curly": "error", "eqeqeq": "error", diff --git a/package.json b/package.json index 9a68e1fb3f..870d17ef05 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "d3-time": "1.0.x", "d3-time-format": "2.1.x", "eslint": "5.2.0", + "eslint-plugin-lodash": "^6.0.0", "eslint-plugin-vue": "^6.0.0", "eventemitter3": "^1.2.0", "exports-loader": "^0.7.0", @@ -47,7 +48,7 @@ "karma-sourcemap-loader": "^0.3.7", "karma-webpack": "^3.0.0", "location-bar": "^3.0.1", - "lodash": "^3.10.1", + "lodash": "^4.17.12", "markdown-toc": "^0.11.7", "marked": "^0.3.5", "mini-css-extract-plugin": "^0.4.1", diff --git a/platform/telemetry/src/TelemetryCapability.js b/platform/telemetry/src/TelemetryCapability.js index c5ac36cd3d..c2bbe5cadb 100644 --- a/platform/telemetry/src/TelemetryCapability.js +++ b/platform/telemetry/src/TelemetryCapability.js @@ -25,8 +25,7 @@ */ define( [ - '../../../src/api/objects/object-utils', - 'lodash' + '../../../src/api/objects/object-utils' ], function ( objectUtils, @@ -235,7 +234,8 @@ define( var defaultRange = metadata.valuesForHints(['range'])[0]; defaultRange = defaultRange ? defaultRange.key : undefined; - var sourceMap = _.indexBy(metadata.values(), 'key'); + const keyBy = (array, k) => (array || []).reduce((r, x) => ({ ...r, [k ? x[k] : x]: x }), {}); + var sourceMap = keyBy(metadata.values(), 'key'); var isLegacyProvider = telemetryAPI.findRequestProvider(domainObject) === telemetryAPI.legacyProvider; @@ -300,7 +300,8 @@ define( var defaultRange = metadata.valuesForHints(['range'])[0]; defaultRange = defaultRange ? defaultRange.key : undefined; - var sourceMap = _.indexBy(metadata.values(), 'key'); + const keyBy = (array, k) => (array || []).reduce((r, x) => ({ ...r, [k ? x[k] : x]: x }), {}); + var sourceMap = keyBy(metadata.values(), 'key'); var isLegacyProvider = telemetryAPI.findSubscriptionProvider(domainObject) === telemetryAPI.legacyProvider; diff --git a/src/api/composition/DefaultCompositionProvider.js b/src/api/composition/DefaultCompositionProvider.js index ca10ca64b0..566427a1ae 100644 --- a/src/api/composition/DefaultCompositionProvider.js +++ b/src/api/composition/DefaultCompositionProvider.js @@ -196,8 +196,8 @@ define([ * @private */ DefaultCompositionProvider.prototype.includes = function (parent, childId) { - return parent.composition.findIndex(composee => - this.publicAPI.objects.areIdsEqual(composee, childId)) !== -1; + return parent.composition.some(composee => + this.publicAPI.objects.areIdsEqual(composee, childId)); }; DefaultCompositionProvider.prototype.reorder = function (domainObject, oldIndex, newIndex) { diff --git a/src/api/telemetry/DefaultMetadataProvider.js b/src/api/telemetry/DefaultMetadataProvider.js index 2f6aa94ed0..8c9a4787a5 100644 --- a/src/api/telemetry/DefaultMetadataProvider.js +++ b/src/api/telemetry/DefaultMetadataProvider.js @@ -85,9 +85,9 @@ define([ value: +e.value }; }), 'e.value'); - valueMetadata.values = _.pluck(valueMetadata.enumerations, 'value'); - valueMetadata.max = _.max(valueMetadata.values); - valueMetadata.min = _.min(valueMetadata.values); + valueMetadata.values = Object.entries(valueMetadata.enumerations).reduce((a, [key, {value}]) => {a [key] = value; return a}, []); + valueMetadata.max = Math.max(...valueMetadata.values); + valueMetadata.min = Math.min(...valueMetadata.values); } valueMetadatas.push(valueMetadata); diff --git a/src/api/telemetry/TelemetryAPI.js b/src/api/telemetry/TelemetryAPI.js index 9de8638eb3..d45eb742cd 100644 --- a/src/api/telemetry/TelemetryAPI.js +++ b/src/api/telemetry/TelemetryAPI.js @@ -370,7 +370,7 @@ define([ TelemetryAPI.prototype.commonValuesForHints = function (metadatas, hints) { var options = metadatas.map(function (metadata) { var values = metadata.valuesForHints(hints); - return _.indexBy(values, 'key'); + return _.keyBy(values, 'key'); }).reduce(function (a, b) { var results = {}; Object.keys(a).forEach(function (key) { @@ -383,7 +383,7 @@ define([ var sortKeys = hints.map(function (h) { return 'hints.' + h; }); - return _.sortByAll(options, sortKeys); + return _.sortBy(options, sortKeys); }; /** diff --git a/src/api/telemetry/TelemetryMetadataManager.js b/src/api/telemetry/TelemetryMetadataManager.js index 5b93eee6d6..049d1712aa 100644 --- a/src/api/telemetry/TelemetryMetadataManager.js +++ b/src/api/telemetry/TelemetryMetadataManager.js @@ -57,13 +57,13 @@ define([ if (valueMetadata.format === 'enum') { if (!valueMetadata.values) { - valueMetadata.values = _.pluck(valueMetadata.enumerations, 'value'); + valueMetadata.values = Object.entries(valueMetadata.enumerations).reduce((a, [key, {value}]) => {a [key] = value; return a}, []); } if (!valueMetadata.hasOwnProperty('max')) { - valueMetadata.max = _.max(valueMetadata.values) + 1; + valueMetadata.max = Math.max(...valueMetadata.values) + 1; } if (!valueMetadata.hasOwnProperty('min')) { - valueMetadata.min = _.min(valueMetadata.values) - 1; + valueMetadata.min = Math.min(...valueMetadata.values) - 1; } } @@ -121,7 +121,7 @@ define([ return metadata.hints[hint]; } }); - return _.sortByAll(matchingMetadata, ...iteratees); + return _.sortBy(matchingMetadata, ...iteratees); }; TelemetryMetadataManager.prototype.getFilterableValues = function () { diff --git a/src/plugins/imagery/components/ImageryViewLayout.vue b/src/plugins/imagery/components/ImageryViewLayout.vue index c308e4c366..0821eeabd9 100644 --- a/src/plugins/imagery/components/ImageryViewLayout.vue +++ b/src/plugins/imagery/components/ImageryViewLayout.vue @@ -220,7 +220,7 @@ export default { return; } - const index = _.sortedIndex(this.imageHistory, datum, this.timeFormat.format.bind(this.timeFormat)); + const index = _.sortedIndexBy(this.imageHistory, datum, this.timeFormat.format.bind(this.timeFormat)); this.imageHistory.splice(index, 0, datum); }, updateValues(datum) { diff --git a/src/plugins/plot/src/chart/MCTChartController.js b/src/plugins/plot/src/chart/MCTChartController.js index aa086d412d..65224ad504 100644 --- a/src/plugins/plot/src/chart/MCTChartController.js +++ b/src/plugins/plot/src/chart/MCTChartController.js @@ -152,7 +152,7 @@ function ( MCTChartController.prototype.destroy = function () { this.isDestroyed = true; this.stopListening(); - _.invoke(this.lines, 'destroy'); + this.lines.map(line => line.destroy()); DrawLoader.releaseDrawAPI(this.drawAPI); }; diff --git a/src/plugins/plot/src/telemetry/StackedPlotController.js b/src/plugins/plot/src/telemetry/StackedPlotController.js index f2be95448a..2ae348ef7a 100644 --- a/src/plugins/plot/src/telemetry/StackedPlotController.js +++ b/src/plugins/plot/src/telemetry/StackedPlotController.js @@ -76,7 +76,7 @@ define([ if (childObj) { var index = telemetryObjects.indexOf(childObj); telemetryObjects.splice(index, 1); - $scope.$broadcast('plot:tickWidth', _.max(tickWidthMap)); + $scope.$broadcast('plot:tickWidth', Math.max(...Object.values(tickWidthMap))); } } @@ -131,6 +131,7 @@ define([ } tickWidthMap[plotId] = Math.max(width, tickWidthMap[plotId]); var newTickWidth = _.max(tickWidthMap); + if (newTickWidth !== tickWidth || width !== tickWidth) { tickWidth = newTickWidth; $scope.$broadcast('plot:tickWidth', tickWidth); diff --git a/src/plugins/summaryWidget/src/telemetry/SummaryWidgetEvaluator.js b/src/plugins/summaryWidget/src/telemetry/SummaryWidgetEvaluator.js index 7ae94d3e42..ceb03f26c6 100644 --- a/src/plugins/summaryWidget/src/telemetry/SummaryWidgetEvaluator.js +++ b/src/plugins/summaryWidget/src/telemetry/SummaryWidgetEvaluator.js @@ -154,7 +154,7 @@ define([ return _(this.baseState) .values() .map(_.clone) - .indexBy('id') + .keyBy('id') .value(); }.bind(this)); }; diff --git a/src/plugins/telemetryTable/TelemetryTableConfiguration.js b/src/plugins/telemetryTable/TelemetryTableConfiguration.js index 5946779c4d..a423b51910 100644 --- a/src/plugins/telemetryTable/TelemetryTableConfiguration.js +++ b/src/plugins/telemetryTable/TelemetryTableConfiguration.js @@ -100,7 +100,7 @@ define([ hasColumnWithKey(columnKey) { return _.flatten(Object.values(this.columns)) - .findIndex(column => column.getKey() === columnKey) !== -1; + .some(column => column.getKey() === columnKey); } getColumns() { diff --git a/src/plugins/telemetryTable/collections/SortedTableRowCollection.js b/src/plugins/telemetryTable/collections/SortedTableRowCollection.js index 043ac29032..3cf99c20b5 100644 --- a/src/plugins/telemetryTable/collections/SortedTableRowCollection.js +++ b/src/plugins/telemetryTable/collections/SortedTableRowCollection.js @@ -93,7 +93,7 @@ define( // same time stamp let potentialDupes = this.rows.slice(startIx, endIx + 1); // Search potential dupes for exact dupe - isDuplicate = _.findIndex(potentialDupes, _.isEqual.bind(undefined, row)) > -1; + isDuplicate = potentialDupes.some(_.isEqual.bind(undefined, row)); } if (!isDuplicate) { @@ -201,7 +201,7 @@ define( sortBy(sortOptions) { if (arguments.length > 0) { this.sortOptions = sortOptions; - this.rows = _.sortByOrder(this.rows, (row) => row.getParsedValue(sortOptions.key) , sortOptions.direction); + this.rows = _.orderBy(this.rows, (row) => row.getParsedValue(sortOptions.key) , sortOptions.direction); this.emit('sort'); } // Return duplicate to avoid direct modification of underlying object