-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(enhanceConfiguration): deduplicate the hierarchicalFacets (#3966)
* feat(utils): add findIndex * fix(enhanceConfiguration): merge hierarchicalFacets
- Loading branch information
Showing
5 changed files
with
235 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import findIndex from '../findIndex'; | ||
|
||
describe('findIndex', () => { | ||
describe('with polyfill', () => { | ||
test('with empty array', () => { | ||
const items = []; | ||
const actual = findIndex(items, item => item === 'hello'); | ||
|
||
expect(actual).toEqual(-1); | ||
}); | ||
|
||
test('with unknown item in array', () => { | ||
const items = ['hey']; | ||
const actual = findIndex(items, item => item === 'hello'); | ||
|
||
expect(actual).toEqual(-1); | ||
}); | ||
|
||
test('with an array of strings', () => { | ||
const items = ['hello', 'goodbye']; | ||
const actual = findIndex(items, item => item === 'hello'); | ||
|
||
expect(actual).toEqual(0); | ||
}); | ||
|
||
test('with an array of objects', () => { | ||
const items = [{ name: 'John' }, { name: 'Jane' }]; | ||
const actual = findIndex(items, item => item.name === 'John'); | ||
|
||
expect(actual).toEqual(0); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// We aren't using the native `Array.prototype.findIndex` because the refactor away from Lodash is not | ||
// published as a major version. | ||
// Relying on the `findIndex` polyfill on user-land, which before was only required for niche use-cases, | ||
// was decided as too risky. | ||
// @MAJOR Replace with the native `Array.prototype.findIndex` method | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex | ||
function findIndex<TItem>( | ||
array: TItem[], | ||
comparator: (value: TItem) => boolean | ||
): number { | ||
if (!Array.isArray(array)) { | ||
return -1; | ||
} | ||
|
||
for (let i = 0; i < array.length; i++) { | ||
if (comparator(array[i])) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
export default findIndex; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters