{"version":3,"file":"matches-CCxvPv8Q.mjs","names":[],"sources":["../src/matches.ts"],"sourcesContent":["import stripAnsiFn from \"strip-ansi\";\nimport type { TestInstance } from \"./types\";\n\nexport type MatcherFunction = (\n  content: string,\n  element: TestInstance | null,\n) => boolean;\n\nexport type Matcher = MatcherFunction | RegExp | number | string;\n\nexport type NormalizerFn = (text: string) => string;\n\nexport interface NormalizerOptions extends DefaultNormalizerOptions {\n  normalizer?: NormalizerFn;\n}\n\nexport interface MatcherOptions {\n  exact?: boolean;\n  /** Use normalizer with getDefaultNormalizer instead */\n  trim?: boolean;\n  /** Use normalizer with getDefaultNormalizer instead */\n  stripAnsi?: boolean;\n  /** Use normalizer with getDefaultNormalizer instead */\n  collapseWhitespace?: boolean;\n  normalizer?: NormalizerFn;\n  /** suppress suggestions for a specific query */\n  suggest?: boolean;\n}\n\nexport type Match = (\n  textToMatch: string,\n  node: TestInstance | null,\n  matcher: Matcher,\n  options?: MatcherOptions,\n) => boolean;\n\nexport interface DefaultNormalizerOptions {\n  trim?: boolean;\n  collapseWhitespace?: boolean;\n  stripAnsi?: boolean;\n}\n\nfunction assertNotNullOrUndefined(matcher: Matcher) {\n  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n  if (matcher === null || matcher === undefined) {\n    throw new Error(\n      `It looks like ${matcher} was passed instead of a matcher. Did you do something like getByText(${matcher})?`,\n    );\n  }\n}\n\n/**\n * @private\n */\nfunction fuzzyMatches(\n  textToMatch: string,\n  node: TestInstance | null,\n  matcher: Matcher,\n  normalizer: NormalizerFn,\n) {\n  if (typeof textToMatch !== \"string\") {\n    return false;\n  }\n  assertNotNullOrUndefined(matcher);\n\n  const normalizedText = normalizer(textToMatch);\n\n  if (typeof matcher === \"string\" || typeof matcher === \"number\") {\n    return normalizedText\n      .toLowerCase()\n      .includes(matcher.toString().toLowerCase());\n  } else if (typeof matcher === \"function\") {\n    return matcher(normalizedText, node);\n  } else {\n    return matcher.test(normalizedText);\n  }\n}\n\n/**\n * @private\n */\nfunction matches(\n  textToMatch: string,\n  node: TestInstance | null,\n  matcher: Matcher,\n  normalizer: NormalizerFn,\n): boolean {\n  if (typeof textToMatch !== \"string\") {\n    return false;\n  }\n\n  assertNotNullOrUndefined(matcher);\n\n  const normalizedText = normalizer(textToMatch);\n  if (matcher instanceof Function) {\n    return matcher(normalizedText, node);\n  } else if (matcher instanceof RegExp) {\n    return matcher.test(normalizedText);\n  } else {\n    return normalizedText === String(matcher);\n  }\n}\n\nfunction getDefaultNormalizer({\n  trim = true,\n  collapseWhitespace = true,\n  stripAnsi = true,\n}: DefaultNormalizerOptions = {}): NormalizerFn {\n  return (text: string) => {\n    let normalizedText = text;\n    normalizedText = trim ? normalizedText.trim() : normalizedText;\n    normalizedText = collapseWhitespace\n      ? normalizedText.replace(/\\s+/g, \" \")\n      : normalizedText;\n    normalizedText = stripAnsi ? stripAnsiFn(normalizedText) : normalizedText;\n    return normalizedText;\n  };\n}\n\n/**\n * @param {Object} props\n * Constructs a normalizer to pass to functions in matches.js\n * @param {boolean|undefined} props.trim The user-specified value for `trim`, without\n * any defaulting having been applied\n * @param {boolean|undefined} props.stripAnsi The user-specified value for `stripAnsi`, without\n * any defaulting having been applied\n * @param {boolean|undefined} props.collapseWhitespace The user-specified value for\n * `collapseWhitespace`, without any defaulting having been applied\n * @param {Function|undefined} props.normalizer The user-specified normalizer\n * @returns {Function} A normalizer\n */\nfunction makeNormalizer({\n  trim,\n  stripAnsi,\n  collapseWhitespace,\n  normalizer,\n}: NormalizerOptions): NormalizerFn {\n  if (normalizer) {\n    // User has specified a custom normalizer\n    if (\n      typeof trim !== \"undefined\" ||\n      typeof collapseWhitespace !== \"undefined\" ||\n      typeof stripAnsi !== \"undefined\"\n    ) {\n      // They've also specified a value for trim or collapseWhitespace\n      throw new Error(\n        \"trim and collapseWhitespace are not supported with a normalizer. \" +\n          \"If you want to use the default trim and collapseWhitespace logic in your normalizer, \" +\n          'use \"getDefaultNormalizer({trim, collapseWhitespace})\" and compose that into your normalizer',\n      );\n    }\n\n    return normalizer;\n  } else {\n    // No custom normalizer specified. Just use default.\n    return getDefaultNormalizer({ trim, collapseWhitespace, stripAnsi });\n  }\n}\n\nexport { fuzzyMatches, matches, getDefaultNormalizer, makeNormalizer };\n"],"mappings":";;AA0CA,SAAS,yBAAyB,SAAkB;CAElD,IAAI,YAAY,QAAQ,YAAY,KAAA,GAClC,MAAM,IAAI,MACR,iBAAiB,QAAQ,wEAAwE,QAAQ,GAC3G;AAEJ;;;;AAKA,SAAS,aACP,aACA,MACA,SACA,YACA;CACA,IAAI,OAAO,gBAAgB,UACzB,OAAO;CAET,yBAAyB,OAAO;CAEhC,MAAM,iBAAiB,WAAW,WAAW;CAE7C,IAAI,OAAO,YAAY,YAAY,OAAO,YAAY,UACpD,OAAO,eACJ,YAAY,CAAC,CACb,SAAS,QAAQ,SAAS,CAAC,CAAC,YAAY,CAAC;MACvC,IAAI,OAAO,YAAY,YAC5B,OAAO,QAAQ,gBAAgB,IAAI;MAEnC,OAAO,QAAQ,KAAK,cAAc;AAEtC;;;;AAKA,SAAS,QACP,aACA,MACA,SACA,YACS;CACT,IAAI,OAAO,gBAAgB,UACzB,OAAO;CAGT,yBAAyB,OAAO;CAEhC,MAAM,iBAAiB,WAAW,WAAW;CAC7C,IAAI,mBAAmB,UACrB,OAAO,QAAQ,gBAAgB,IAAI;MAC9B,IAAI,mBAAmB,QAC5B,OAAO,QAAQ,KAAK,cAAc;MAElC,OAAO,mBAAmB,OAAO,OAAO;AAE5C;AAEA,SAAS,qBAAqB,EAC5B,OAAO,MACP,qBAAqB,MACrB,YAAY,SACgB,CAAC,GAAiB;CAC9C,QAAQ,SAAiB;EACvB,IAAI,iBAAiB;EACrB,iBAAiB,OAAO,eAAe,KAAK,IAAI;EAChD,iBAAiB,qBACb,eAAe,QAAQ,QAAQ,GAAG,IAClC;EACJ,iBAAiB,YAAY,YAAY,cAAc,IAAI;EAC3D,OAAO;CACT;AACF;;;;;;;;;;;;;AAcA,SAAS,eAAe,EACtB,MACA,WACA,oBACA,cACkC;CAClC,IAAI,YAAY;EAEd,IACE,OAAO,SAAS,eAChB,OAAO,uBAAuB,eAC9B,OAAO,cAAc,aAGrB,MAAM,IAAI,MACR,sPAGF;EAGF,OAAO;CACT,OAEE,OAAO,qBAAqB;EAAE;EAAM;EAAoB;CAAU,CAAC;AAEvE"}