From 5ac071455b8ad02a0fab954842bdf02c95557818 Mon Sep 17 00:00:00 2001 From: Haroen Viaene Date: Mon, 6 May 2019 17:02:29 +0200 Subject: [PATCH 1/2] refactor(lodash): sumBy I added a new test file, since this didn't seem to be tested anywhere. --- src/SearchResults/index.js | 5 ++-- test/spec/SearchResults/initialization.js | 30 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 test/spec/SearchResults/initialization.js diff --git a/src/SearchResults/index.js b/src/SearchResults/index.js index 3b585b84e..b0947a540 100644 --- a/src/SearchResults/index.js +++ b/src/SearchResults/index.js @@ -3,7 +3,6 @@ var compact = require('lodash/compact'); var findIndex = require('lodash/findIndex'); -var sumBy = require('lodash/sumBy'); var find = require('lodash/find'); var orderBy = require('lodash/orderBy'); @@ -286,7 +285,9 @@ function SearchResults(state, results) { * sum of the processing time of all the queries * @member {number} */ - this.processingTimeMS = sumBy(results, 'processingTimeMS'); + this.processingTimeMS = results.reduce(function(sum, result) { + return sum + result.processingTimeMS; + }, 0); /** * The position if the position was guessed by IP. * @member {string} diff --git a/test/spec/SearchResults/initialization.js b/test/spec/SearchResults/initialization.js new file mode 100644 index 000000000..96dc4594e --- /dev/null +++ b/test/spec/SearchResults/initialization.js @@ -0,0 +1,30 @@ +'use strict'; + +var SearchParameters = require('../../../src/SearchParameters'); +var SearchResults = require('../../../src/SearchResults'); + +test('processingTime should be the sum of all individual times', function() { + var result = new SearchResults(new SearchParameters(), [ + { + processingTimeMS: 1 + }, + { + processingTimeMS: 1 + } + ]); + + expect(result.processingTimeMS).toBe(2); +}); + +test('processingTime should be NaN for one value undefined', function() { + var result = new SearchResults(new SearchParameters(), [ + { + processingTimeMS: undefined + }, + { + processingTimeMS: 1 + } + ]); + + expect(result.processingTimeMS).toBe(NaN); +}); From 2895bca8c845fcb93ee8306df326c3abd780b321 Mon Sep 17 00:00:00 2001 From: Haroen Viaene Date: Mon, 6 May 2019 17:27:22 +0200 Subject: [PATCH 2/2] fix: guard for undefined values and ignore those --- src/SearchResults/index.js | 4 +++- test/spec/SearchResults/initialization.js | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/SearchResults/index.js b/src/SearchResults/index.js index b0947a540..ef516bf13 100644 --- a/src/SearchResults/index.js +++ b/src/SearchResults/index.js @@ -286,7 +286,9 @@ function SearchResults(state, results) { * @member {number} */ this.processingTimeMS = results.reduce(function(sum, result) { - return sum + result.processingTimeMS; + return result.processingTimeMS === undefined + ? sum + : sum + result.processingTimeMS; }, 0); /** * The position if the position was guessed by IP. diff --git a/test/spec/SearchResults/initialization.js b/test/spec/SearchResults/initialization.js index 96dc4594e..982779ee5 100644 --- a/test/spec/SearchResults/initialization.js +++ b/test/spec/SearchResults/initialization.js @@ -16,7 +16,7 @@ test('processingTime should be the sum of all individual times', function() { expect(result.processingTimeMS).toBe(2); }); -test('processingTime should be NaN for one value undefined', function() { +test('processingTime should ignore undefined', function() { var result = new SearchResults(new SearchParameters(), [ { processingTimeMS: undefined @@ -26,5 +26,5 @@ test('processingTime should be NaN for one value undefined', function() { } ]); - expect(result.processingTimeMS).toBe(NaN); + expect(result.processingTimeMS).toBe(1); });