CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-nanomatch

Fast, minimal glob matcher for node.js with complete Bash 4.3 wildcard support

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Nanomatch

Nanomatch is a fast and minimal glob matcher for Node.js that provides complete Bash 4.3 wildcard support. It offers efficient pattern matching for file paths and strings using standard glob patterns like *, **, ?, and [...], with optimized performance and comprehensive caching.

Package Information

  • Package Name: nanomatch
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install nanomatch

Core Imports

const nanomatch = require('nanomatch');

For ES modules:

import nanomatch from 'nanomatch';

Basic Usage

const nanomatch = require('nanomatch');

// Match files against patterns
const files = ['a.js', 'b.txt', 'c.js', 'd.md'];
console.log(nanomatch(files, '*.js'));
//=> ['a.js', 'c.js']

// Check if a string matches a pattern
console.log(nanomatch.isMatch('foo.js', '*.js'));
//=> true

// Filter with multiple patterns including negation
console.log(nanomatch(files, ['*.js', '!a.js']));
//=> ['c.js']

// Match with glob stars for directory traversal
const paths = ['src/index.js', 'lib/utils.js', 'test/spec.js'];
console.log(nanomatch(paths, 'src/**'));
//=> ['src/index.js']

Architecture

Nanomatch is built around several key components:

  • Parser/Compiler Pipeline: Uses snapdragon for parsing glob patterns into AST and compiling to optimized regex
  • Caching System: Aggressive memoization of compiled patterns for performance
  • Bash Compatibility: Full support for Bash 4.3 wildcard semantics with cross-platform normalization
  • Negation Support: Built-in support for negation patterns using ! prefix
  • Options System: Comprehensive configuration for behavior customization

Capabilities

Core Pattern Matching

Primary glob pattern matching functionality for filtering arrays of strings against one or more patterns.

/**
 * Match array of strings against glob patterns
 * @param {Array} list - Array of strings to match
 * @param {String|Array} patterns - One or more glob patterns
 * @param {Object} options - Optional matching configuration
 * @returns {Array} Array of matching strings
 */
function nanomatch(list, patterns, options);

/**
 * Match array against single pattern
 * @param {Array} list - Array of strings to match  
 * @param {String} pattern - Single glob pattern
 * @param {Object} options - Optional matching configuration
 * @returns {Array} Array of matching strings
 */
nanomatch.match(list, pattern, options);

/**
 * Test if string matches pattern
 * @param {String} string - String to test
 * @param {String} pattern - Glob pattern to test against
 * @param {Object} options - Optional matching configuration  
 * @returns {Boolean} True if string matches pattern
 */
nanomatch.isMatch(string, pattern, options);

Core Pattern Matching

Collection Operations

Advanced matching operations for working with arrays and performing logical operations across multiple patterns.

/**
 * Test if some items match any patterns
 * @param {String|Array} list - String or array to test
 * @param {String|Array} patterns - Patterns to match against
 * @param {Object} options - Optional matching configuration
 * @returns {Boolean} True if any items match any patterns  
 */
nanomatch.some(list, patterns, options);

/**
 * Test if every item matches at least one pattern
 * @param {String|Array} list - String or array to test
 * @param {String|Array} patterns - Patterns to match against
 * @param {Object} options - Optional matching configuration
 * @returns {Boolean} True if all items match at least one pattern
 */
nanomatch.every(list, patterns, options);

/**
 * Test if any patterns match the string
 * @param {String} str - String to test
 * @param {String|Array} patterns - Patterns to test against
 * @param {Object} options - Optional matching configuration
 * @returns {Boolean} True if any patterns match
 */
nanomatch.any(str, patterns, options);

/**
 * Test if all patterns match the string  
 * @param {String} str - String to test
 * @param {String|Array} patterns - Patterns to test against
 * @param {Object} options - Optional matching configuration
 * @returns {Boolean} True if all patterns match
 */
nanomatch.all(str, patterns, options);

/**
 * Return strings that do not match any patterns
 * @param {Array} list - Array of strings to filter
 * @param {String|Array} patterns - Patterns to match against
 * @param {Object} options - Optional matching configuration
 * @returns {Array} Array of non-matching strings
 */
nanomatch.not(list, patterns, options);

Collection Operations

Content and Object Matching

String containment matching and object key filtering capabilities.

/**
 * Test if pattern matches any part of string
 * @param {String} str - String to search within
 * @param {String|Array} patterns - Patterns to search for
 * @param {Object} options - Optional matching configuration
 * @returns {Boolean} True if patterns match any part of string
 */
nanomatch.contains(str, patterns, options);

/**
 * Filter object keys using glob patterns
 * @param {Object} object - Object with keys to filter
 * @param {String|Array} patterns - Patterns to match keys against
 * @param {Object} options - Optional matching configuration
 * @returns {Object} New object with only matching keys
 */
nanomatch.matchKeys(object, patterns, options);

Content and Object Matching

Matcher Creation and Capture

Factory functions for creating reusable matchers and extracting pattern captures.

/**
 * Create reusable matcher function from pattern
 * @param {String} pattern - Glob pattern to create matcher for
 * @param {Object} options - Optional matching configuration
 * @returns {Function} Matcher function that takes string and returns boolean
 */
nanomatch.matcher(pattern, options);

/**
 * Extract captures from pattern match
 * @param {String} pattern - Glob pattern with capture groups
 * @param {String} string - String to match and extract from
 * @param {Object} options - Optional matching configuration
 * @returns {Array|null} Array of captured groups or null if no match
 */
nanomatch.capture(pattern, string, options);

Matcher Creation and Capture

Regex and Compilation

Low-level pattern parsing, compilation, and regex generation for advanced use cases.

/**
 * Create regular expression from glob pattern
 * @param {String} pattern - Glob pattern to convert
 * @param {Object} options - Optional compilation configuration
 * @returns {RegExp} Compiled regular expression
 */
nanomatch.makeRe(pattern, options);

/**
 * Parse and compile pattern with full AST and metadata
 * @param {String} pattern - Glob pattern to parse and compile
 * @param {Object} options - Optional parsing/compilation configuration
 * @returns {Object} Object with compiled output, AST, and source map
 */
nanomatch.create(pattern, options);

/**
 * Parse pattern string into AST
 * @param {String} pattern - Glob pattern to parse
 * @param {Object} options - Optional parsing configuration
 * @returns {Object} Abstract syntax tree representation
 */
nanomatch.parse(pattern, options);

/**
 * Compile AST or pattern string to output
 * @param {Object|String} ast - AST object or pattern string to compile
 * @param {Object} options - Optional compilation configuration
 * @returns {Object} Object with compiled output and metadata
 */
nanomatch.compile(ast, options);

Regex and Compilation

Cache Management

Control over internal pattern caching for performance optimization.

/**
 * Clear internal pattern cache
 * @returns {void}
 */
nanomatch.clearCache();

Utility Functions

Helper functions for advanced pattern analysis and option determination.

/**
 * Determine if matchBase option should be enabled for pattern
 * @param {String} pattern - Glob pattern to analyze
 * @param {Object} options - Options object to check
 * @returns {Boolean} True if matchBase should be enabled
 */
nanomatch.matchBase(pattern, options);

Exposed Internal Components

Access to nanomatch's internal parsing and compilation components for advanced customization.

/**
 * Exposed compiler functions for advanced pattern compilation
 * @type {Object}
 */
nanomatch.compilers;

/**
 * Exposed parser functions for advanced pattern parsing
 * @type {Object}
 */
nanomatch.parsers;

/**
 * Exposed cache instance for direct cache manipulation
 * @type {Object}
 */
nanomatch.cache;

Common Options

interface NanomatchOptions {
  /** Match basenames only, ignoring directory paths */
  basename?: boolean;
  /** Alias for basename */  
  matchBase?: boolean;
  /** Enable bash-like bracket behavior (default: true) */
  bash?: boolean;
  /** Disable regex and function memoization */
  cache?: boolean;
  /** Match dotfiles (files starting with .) */
  dot?: boolean;
  /** Throw error when no matches found */
  failglob?: boolean;
  /** Patterns to ignore during matching */
  ignore?: string | string[];
  /** Case-insensitive matching */
  nocase?: boolean;
  /** Remove duplicate results (default: true) */
  nodupes?: boolean;
  /** Disable globstar (**) matching */
  noglobstar?: boolean;
  /** Disable negation (!) patterns */
  nonegate?: boolean;
  /** Return pattern if no matches found */
  nonull?: boolean;
  /** Alias for nonull */
  nullglob?: boolean;
  /** Custom slash character(s) for path separators */
  slash?: string | Function;
  /** Custom star character pattern */
  star?: string | Function;
  /** Custom snapdragon instance */
  snapdragon?: Object;
  /** Generate source maps during compilation */
  sourcemap?: boolean;
  /** Remove backslashes from returned matches */
  unescape?: boolean;
  /** Convert paths to POSIX format (default: true) */
  unixify?: boolean;
}

Types

/** Function signature for matcher functions returned by nanomatch.matcher */
type MatcherFunction = (str: string) => boolean;

/** Compilation result from nanomatch.create */
interface CompilationResult {
  /** Compiled regex pattern string */
  output: string;
  /** Parsed abstract syntax tree */
  ast: Object;
  /** Source map (if sourcemap option enabled) */
  map?: Object;
  /** Parser options used */
  options: Object;
  /** Parser state information */  
  state: Object;
  /** Available compilers */
  compilers: Object;
}

/** Parse result from nanomatch.parse */
interface ParseResult {
  /** AST node type */
  type: string;
  /** Input pattern string */
  input: string;
  /** Child nodes */
  nodes: Object[];
  /** Parsing errors */
  errors: any[];
}
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/nanomatch@1.2.x
Publish Source
CLI
Badge
tessl/npm-nanomatch badge