Skip to content

createGlobMatcher

createMatcher with every string read as a glob (*, ?): no string is ever taken for a regular expression, so it never throws. The right choice for paths and file names.

Available since v3.1.5

import { createGlobMatcher } from '@helpers4/string';
// or, from the all-in-one package (same code, one install):
import { createGlobMatcher } from 'helpers4/string';
createGlobMatcher(pattern: MatcherPattern | readonly MatcherPattern[], options: Omit<MatcherOptions, "syntax">): function
ParameterTypeDescription
patternMatcherPattern | readonly MatcherPattern[]A pattern, or an array of patterns and `!` exclusions
optionsOmit<MatcherOptions, "syntax">Case sensitivity

function — A function telling whether a string matches

Strings are always globs, so a path is never taken for a regular expression.

const isSource = createGlobMatcher(['/src/*.ts', '!*.test.ts']);
isSource('/src/index.ts')      // => true
isSource('/src/index.test.ts') // => false

caseSensitive: false matches letters of any case.

createGlobMatcher('*.FIG', { caseSensitive: false })('a.fig') // => true

Options for createMatcher and createSafeMatcher.

interface MatcherOptions {
  caseSensitive?: boolean;
  extended?: boolean;
  syntax?: "auto" | "regex" | "glob";
}

One pattern accepted by createMatcher: a string (read according to MatcherOptions.syntax), a RegExp, or a predicate of your own.

type MatcherPattern = string | RegExp | ((text: string) => boolean)

View source on GitHub