Commit 069b4d16 authored by mfluharty's avatar mfluharty

Use lodash's differenceBy instead of filterByKey

Replace old custom utility function with standard lodash utility
parent 091f27bd
/**
* Compares two arrays by the given key and returns the difference
*
* @param {Array} firstArray
* @param {Array} secondArray
* @param {String} key
* @returns {Array}
*/
const filterByKey = (firstArray = [], secondArray = [], key = '') =>
firstArray.filter(item => !secondArray.find(el => el[key] === item[key]));
export default filterByKey;
import filterByKey from '../store/utils/filter_by_key';
import { differenceBy } from 'lodash';
const KEY_TO_FILTER_BY = 'fingerprint';
......@@ -19,8 +19,8 @@ self.addEventListener('message', e => {
// eslint-disable-next-line no-restricted-globals
self.postMessage({
newIssues: filterByKey(headIssues, baseIssues, KEY_TO_FILTER_BY),
resolvedIssues: filterByKey(baseIssues, headIssues, KEY_TO_FILTER_BY),
newIssues: differenceBy(headIssues, baseIssues, KEY_TO_FILTER_BY),
resolvedIssues: differenceBy(baseIssues, headIssues, KEY_TO_FILTER_BY),
});
// eslint-disable-next-line no-restricted-globals
......
......@@ -2,7 +2,6 @@ import {
parseCodeclimateMetrics,
doCodeClimateComparison,
} from '~/reports/codequality_report/store/utils/codequality_comparison';
import mockFilterByKey from '~/reports/codequality_report/store/utils/filter_by_key';
import { baseIssues, mockParsedHeadIssues, mockParsedBaseIssues } from '../../mock_data';
jest.mock('~/reports/codequality_report/workers/codequality_comparison_worker', () => {
......@@ -15,10 +14,15 @@ jest.mock('~/reports/codequality_report/workers/codequality_comparison_worker',
postMessage: data => {
if (!data.headIssues) return mockPostMessageCallback({ data: {} });
if (!data.baseIssues) throw new Error();
const key = 'fingerprint';
return mockPostMessageCallback({
data: {
newIssues: mockFilterByKey(data.headIssues, data.baseIssues, 'fingerprint'),
resolvedIssues: mockFilterByKey(data.baseIssues, data.headIssues, 'fingerprint'),
newIssues: data.headIssues.filter(
item => !data.baseIssues.find(el => el[key] === item[key]),
),
resolvedIssues: data.baseIssues.filter(
item => !data.headIssues.find(el => el[key] === item[key]),
),
},
});
},
......
import filterByKey from '~/reports/codequality_report/store/utils/filter_by_key';
import { mockParsedHeadIssues, mockParsedBaseIssues, issueDiff } from '../../mock_data';
describe('filterByKey', () => {
it('should return a diff of the arrays based on the given key', () => {
const result = filterByKey(mockParsedHeadIssues, mockParsedBaseIssues, 'fingerprint');
expect(result).toEqual(issueDiff);
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment