swimlane/ngx-ui

View on GitHub
projects/swimlane/ngx-ui/src/lib/components/select/contains-filter.util.ts

Summary

Maintainability
A
1 hr
Test Coverage
import { escapeRegExp } from '../../utils/regex/escape.util';

export function containsFilter(
  value: any,
  keyword: string,
  options: { filterCaseSensitive?: boolean },
  depth = 0
): boolean {
  if (value === undefined || value === null || depth > 2) {
    return false;
  }

  if (typeof value === 'string') {
    if (!isNaN(+value)) {
      return value === keyword;
    }

    const escapedKeyword = escapeRegExp(keyword);
    // eslint-disable-next-line
    const idx = options.filterCaseSensitive ? value.indexOf(keyword) : value.search(new RegExp(escapedKeyword, 'i'));
    return idx > -1;
  } else if (typeof value === 'object') {
    const keys = Object.getOwnPropertyNames(value);

    for (const k of keys) {
      if (containsFilter(value[k], keyword, options, depth + 1)) {
        return true;
      }
    }
  }
}