Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
refactor(lodash): sumBy (#688)
Browse files Browse the repository at this point in the history
* refactor(lodash): sumBy

I added a new test file, since this didn't seem to be tested anywhere.

* fix: guard for undefined values and ignore those
  • Loading branch information
Haroenv authored and samouss committed May 9, 2019
1 parent bef0ce9 commit 9aa04b0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/SearchResults/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -286,7 +285,11 @@ 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 result.processingTimeMS === undefined
? sum
: sum + result.processingTimeMS;
}, 0);
/**
* The position if the position was guessed by IP.
* @member {string}
Expand Down
30 changes: 30 additions & 0 deletions test/spec/SearchResults/initialization.js
Original file line number Diff line number Diff line change
@@ -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 ignore undefined', function() {
var result = new SearchResults(new SearchParameters(), [
{
processingTimeMS: undefined
},
{
processingTimeMS: 1
}
]);

expect(result.processingTimeMS).toBe(1);
});

0 comments on commit 9aa04b0

Please sign in to comment.