Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
refactor(lodash): replace find (#2446)
Browse files Browse the repository at this point in the history
Chosen to duplicate the funciton in core & dom, because we have no good way to share functionality otherwise

IFW-740
  • Loading branch information
Haroenv committed Aug 19, 2019
1 parent 6bd5858 commit 060980d
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { find, isEmpty } from 'lodash';
import { isEmpty } from 'lodash';
import PropTypes from 'prop-types';
import createConnector from '../core/createConnector';
import { find } from '../core/utils';
import {
cleanUpValue,
refineValue,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { find } from 'lodash';
import PropTypes from 'prop-types';
import { find } from '../core/utils';
import createConnector from '../core/createConnector';
import {
cleanUpValue,
Expand Down
38 changes: 38 additions & 0 deletions packages/react-instantsearch-core/src/core/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,42 @@ describe('utils', () => {
]);
});
});

describe('find', () => {
test('returns the first match based on the comparator', () => {
expect(
utils.find([1], function() {
return true;
})
).toBe(1);
expect(
utils.find([1, 2], function() {
return true;
})
).toBe(1);

expect(
utils.find([{ nice: false }, { nice: true }], function(el) {
return el.nice;
})
).toEqual({ nice: true });
});

test('returns undefined in non-found cases', () => {
expect(
utils.find([], function() {
return false;
})
).toBeUndefined();
expect(
utils.find(undefined, function() {
return false;
})
).toBeUndefined();

expect(function() {
utils.find([1, 2, 3], undefined);
}).toThrow();
});
});
});
17 changes: 16 additions & 1 deletion packages/react-instantsearch-core/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const shallowEqual = (objA, objB) => {

// Test for A's keys different from B.
const hasOwn = Object.prototype.hasOwnProperty;
// tslint:disable-next-line: prefer-for-of
for (let i = 0; i < keysA.length; i++) {
if (!hasOwn.call(objB, keysA[i]) || objA[keysA[i]] !== objB[keysA[i]]) {
return false;
Expand Down Expand Up @@ -70,3 +69,19 @@ export function addQueryID(hits, queryID) {
__queryID: queryID,
}));
}

export function find<T = any>(
array: T[],
comparator: (item: T) => boolean
): T | undefined {
if (!Array.isArray(array)) {
return undefined;
}

for (let i = 0; i < array.length; i++) {
if (comparator(array[i])) {
return array[i];
}
}
return undefined;
}
8 changes: 5 additions & 3 deletions packages/react-instantsearch-dom/src/components/MenuSelect.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { find } from 'lodash';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { translatable } from 'react-instantsearch-core';
import { createClassNames } from '../core/utils';
import { createClassNames, find } from '../core/utils';

const cx = createClassNames('MenuSelect');

Expand Down Expand Up @@ -31,7 +30,10 @@ class MenuSelect extends Component {
};

get selectedValue() {
const { value } = find(this.props.items, { isRefined: true }) || {
const { value } = find(
this.props.items,
item => item.isRefined === true
) || {
value: 'ais__see__all__option',
};
return value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { find } from 'lodash';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { translatable } from 'react-instantsearch-core';
import { createClassNames, range } from '../core/utils';
import { createClassNames, find, range } from '../core/utils';

const cx = createClassNames('RatingMenu');

Expand Down
38 changes: 38 additions & 0 deletions packages/react-instantsearch-dom/src/core/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,42 @@ describe('utils', () => {
]);
});
});

describe('find', () => {
test('returns the first match based on the comparator', () => {
expect(
utils.find([1], function() {
return true;
})
).toBe(1);
expect(
utils.find([1, 2], function() {
return true;
})
).toBe(1);

expect(
utils.find([{ nice: false }, { nice: true }], function(el) {
return el.nice;
})
).toEqual({ nice: true });
});

test('returns undefined in non-found cases', () => {
expect(
utils.find([], function() {
return false;
})
).toBeUndefined();
expect(
utils.find(undefined, function() {
return false;
})
).toBeUndefined();

expect(function() {
utils.find([1, 2, 3], undefined);
}).toThrow();
});
});
});
16 changes: 16 additions & 0 deletions packages/react-instantsearch-dom/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,19 @@ export function range({ start = 0, end, step = 1 }: RangeOptions): number[] {
(_, current) => (start + current) * limitStep
);
}

export function find<T = any>(
array: T[],
comparator: (item: T) => boolean
): T | undefined {
if (!Array.isArray(array)) {
return undefined;
}

for (let i = 0; i < array.length; i++) {
if (comparator(array[i])) {
return array[i];
}
}
return undefined;
}

0 comments on commit 060980d

Please sign in to comment.