All files / tao-utils/src trigram-filter.js

100% Statements 13/13
100% Branches 10/10
100% Functions 4/4
100% Lines 11/11

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                            30x 7x   25x 25x 6x   25x 7x   25x 28x 3x   26x      
import { AppCtx } from '@tao.js/core';
 
/**
 * Builds a predicate that tests whether a value is an `AppCtx` matching any
 * of the given trigrams (wildcard-aware via `AppCtx.isMatch`, which reads
 * short `t`/`a`/`o` keys only).
 *
 * @param {...*} trigrams - optional leading `exact` boolean (require exact
 *        key equality — wildcards only match wildcards), then trigram
 *        objects or a single array of trigrams; with none (or a leading
 *        `null`) the predicate matches any `AppCtx`
 * @returns {function(*): boolean} predicate over candidate AppCons
 */
export default function trigramFilter(...trigrams) {
  if (!trigrams.length || trigrams[0] == null) {
    return (ac) => ac instanceof AppCtx;
  }
  let exact = false;
  if (typeof trigrams[0] === 'boolean') {
    exact = trigrams.shift();
  }
  if (Array.isArray(trigrams[0])) {
    trigrams = trigrams[0];
  }
  return (ac) => {
    if (!(ac instanceof AppCtx)) {
      return false;
    }
    return trigrams.some((trigram) => ac.isMatch(trigram, exact));
  };
}