# All files / utils filters.ts

28.57% Statements 36/126
100% Branches 0/0
0% Functions 0/4
28.57% Lines 36/126

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
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    1271x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

     import type { TextExtractor } from "../types";
    const identity =
      <T>(name: string) =>
      (item: T): string => {
        if (typeof item === "string") {
          return item;
        }

        if (process.env.NODE_ENV !== "production") {
          throw new Error(
            `A \`TextExtractor\` must be provided to \`${name}\` for lists that do not contain strings`
          );
        }

        return "";
      };

    export interface BaseFilterOptions<T> {
      list: readonly T[];
      query: string;
      extractor?: TextExtractor<T>;
      whitespace?: "ignore" | "trim" | "keep";
    }

    export interface CaseInsensitiveOptions<T> extends BaseFilterOptions<T> {
      startsWith?: boolean;
    }

    export interface FilterOptions<T> extends BaseFilterOptions<T> {
      filter(query: string, value: string): boolean;
      extractor: TextExtractor<T>;
    }

    function filter<T>(options: FilterOptions<T>): readonly T[] {
      const { list, query, filter, extractor, whitespace = "keep" } = options;
      if (!list.length) {
        return list;
      }

      let q = query.toLowerCase();
      if (whitespace === "ignore") {
        q = q.replace(/\s/g, "");
      } else if (whitespace === "trim") {
        q = q.trim();
      }

      if (!q) {
        return list;
      }

      return list.filter((item) => filter(q, extractor(item)));
    }

    export function caseInsensitiveFilter<T extends string>(
      options: Omit<CaseInsensitiveOptions<T>, "extractor">
    ): readonly T[];
    export function caseInsensitiveFilter<T>(
      options: CaseInsensitiveOptions<T> & { extractor: TextExtractor<T> }
    ): readonly T[];
    export function caseInsensitiveFilter<T>(
      options: CaseInsensitiveOptions<T>
    ): readonly T[] {
      const {
        list,
        query,
        extractor = identity("caseInsensitiveFilter"),
        startsWith,
        whitespace,
      } = options;

      return filter({
        list,
        query,
        extractor,
        whitespace,
        filter(query, value) {
          const matchIndex = value.indexOf(query);
          if (startsWith) {
            return matchIndex === 0;
          }

          return matchIndex !== -1;
        },
      });
    }

    export function fuzzyFilter<T extends string>(
      options: FilterOptions<T>
    ): readonly T[];
    export function fuzzyFilter<T>(
      option: FilterOptions<T> & { extractor: TextExtractor<T> }
    ): readonly T[];
    export function fuzzyFilter<T>(options: FilterOptions<T>): readonly T[] {
      const {
        list,
        query,
        extractor = identity("fuzzyFilter"),
        whitespace,
      } = options;

      let regexp: RegExp;
      return filter({
        list,
        query,
        extractor,
        whitespace,
        filter(query, value) {
          if (!regexp) {
            regexp = new RegExp(
              `${query}`
                .split("")
                .join("\\w*")
                .replace(
                  /(\(|\||\)|\\(?!w\*)|\[|\|-|\.|\^|\+|\$|\?|^(?!w)\*)/g,
                  "\\$1"
                )
                // Couldn't get the matching of two '*' working, so replace them here..
                .replace(/\*\*/g, "*\\*"),
              "i"
            );
          }

          return value.length > 0 && regexp.test(value);
        },
      });
    }