Comment

Ankan Adhikari [firehawk895]

here is a modified version that takes an additional parameter attribute_list which allows for matching deep within the object using dot notation

```
export function filterList(q, list, attribute_list) {
  /*
  q query string to search for
  list is the list of objects to search for
  attribute_list is the list of attributes of the object to match on, can use dot object notation
  */

  function escapeRegExp(s) {
    return s.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&";;
  }
  const words = q
    .split(/\s+/g)
    .map(s => s.trim())
    .filter(s => !!s);
  const hasTrailingSpace = q.endsWith(" ");
  const searchRegex = new RegExp(
    words
      .map((word, i) => {
        if (i + 1 === words.length && !hasTrailingSpace) {
          // The last word - ok with the word being "startswith"-like
          return `(?=.*\\b${escapeRegExp(word)})`;
        } else {
          // Not the last word - expect the whole word exactly
          return `(?=.*\\b${escapeRegExp(word)}\\b)`;
        }
      })
      .join("") + ".+",
    "gi"
  );
  return list.filter(item => {
    let result = false;
    attribute_list.forEach(attr => {
      // This beautiful stackoverflow answer converts dot notation into an object reference
      // https://stackoverflow.com/a/6394168/1881812
      // 'a.b.etc'.split('.').reduce((o,i)=>o[i], obj)
      result =
        result ||
        searchRegex.test(attr.split(".").reduce((o, i) => o[i], item));
    });
    return result;
  });
}
```