All files matches.ts

100% Statements 17/17
100% Branches 12/12
100% Functions 1/1
100% Lines 17/17

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 421x 1x                                           1x 42x   42x 1x 1x 42x 9x 9x   36x 101x 18x 18x 101x   14x 14x  
import { isRegExp } from './is-reg-exp.ts';
import { isString } from './is-string.ts';
 
/**
 * Determines if the given `text` matches the provided `match` criteria.
 *
 * The `match` parameter can be:
 * - A string: returns true if the trimmed, lowercased `text` is equal to the lowercased `match` string.
 * - A RegExp: returns true if the regular expression matches the trimmed, lowercased `text`.
 * - An iterable of strings or RegExps: returns true if any of the elements match the `text` as described above.
 * @param text - The input string to test against the match criteria.
 * @param match - A string, RegExp, or iterable of strings/RegExps to match against the input text.
 * @returns `true` if the text matches the criteria; otherwise, `false`.
 * @group RegExp
 * @category Operations
 * @example
 * ```typescript
 * matches('Hello', 'hello'); // true
 * matches('Hello', /he.*\/ui); // true
 * matches('Hello', ['hi', /he.*\/ui]); // true
 * matches('Hello', ['hi', 'hey']); // false
 * ```
 */
export function matches(text: string, match: string | RegExp | Iterable<string | RegExp>): boolean {
  const str = text.trim().toLocaleLowerCase();
 
  if (isRegExp(match)) {
    return match.test(str);
  }
  if (isString(match)) {
    return match.toLocaleLowerCase() === str;
  }
 
  for (const m of match) {
    if ((isRegExp(m) && m.test(str)) || (isString(m) && m.toLocaleLowerCase() === str)) {
      return true;
    }
  }
 
  return false;
}