2019-08-31 20:51:14 -07:00
|
|
|
import { helper } from '@ember/component/helper';
|
|
|
|
import { isArray } from '@ember/array';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recreates the Array.find() function where the first value in the array will be returned that strictly
|
|
|
|
* matches the given value or passes the predicate function's boolean test
|
|
|
|
* @param {Array<T>} list - the list to be traversed
|
|
|
|
* @param {primitive value or (item: T) => boolean} valueOrPredicate - the item to test against to find
|
|
|
|
* @returns {T}
|
|
|
|
*/
|
|
|
|
// TODO: This helper should be moved to nacho utils whenever we can work on importing the open source version
|
|
|
|
// back into wherehows
|
|
|
|
export function findInArray<T>([list, valueOrPredicate]: [
|
|
|
|
Array<T>,
|
|
|
|
number | string | boolean | ((item: T) => boolean)
|
|
|
|
]): T | undefined {
|
2020-08-26 15:44:50 -07:00
|
|
|
if (!isArray(list)) {
|
|
|
|
throw new Error('expected first parameter to find-in-array helper to be an array');
|
|
|
|
}
|
|
|
|
|
2019-08-31 20:51:14 -07:00
|
|
|
const predicate =
|
2020-08-26 15:44:50 -07:00
|
|
|
typeof valueOrPredicate === 'function' ? valueOrPredicate : (item: unknown): boolean => item === valueOrPredicate;
|
2019-08-31 20:51:14 -07:00
|
|
|
|
|
|
|
return list.find(predicate);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default helper(findInArray);
|