All files / utils/filterArrayByQueryMatch index.ts

100% Statements 7/7
100% Branches 4/4
100% Functions 3/3
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1823x             23x   23x 132x   36x          
const filterArrayByQueryMatch = <
  T extends Record<string, unknown> | string | any
>(
  query: string,
  arr: T[] = [],
  fields: (keyof T)[] = []
) => {
  const regex = new RegExp(query, 'ig');
 
  return arr.filter(item => {
    if (typeof item === 'string') return item.match(regex);
 
    return fields.some(field => String(item[field]).match(regex));
  });
};
 
export { filterArrayByQueryMatch };