or run

npx @tessl/cli init
Log in

Version

Files

tile.json

task.mdevals/scenario-4/

File Path Validator

A utility for validating file paths against flexible naming patterns with optional components.

Requirements

Create a function that validates file paths based on patterns where certain parts may be optional. The validator should handle paths where specific segments or characters can either be present or absent.

Core Functionality

Implement a path validator that:

  • Accepts a pattern string and returns a validation function
  • The validation function takes a file path string and returns true if it matches the pattern, false otherwise
  • Supports patterns where certain components are optional (may appear zero or one time)
  • Handles typical file path patterns including extensions, prefixes, and naming conventions

Expected Behavior

The validator should correctly identify matches for patterns with optional components:

  • Optional file extensions (e.g., files that may or may not have .txt extension)
  • Optional version suffixes (e.g., files with optional version numbers)
  • Optional prefixes (e.g., files with optional naming prefixes)
  • Optional character sequences within filenames

Test Cases

  • Given pattern with optional extension, validates "readme" matches and "readme.md" matches @test
  • Given pattern with optional version suffix, validates "config" matches and "config-v2" matches @test
  • Given pattern with optional prefix, validates "report.pdf" matches and "draft-report.pdf" matches @test
  • Given pattern with optional character, validates "file1.js" matches and "file.js" matches @test

Implementation

@generates

API

/**
 * Creates a path validator function from a pattern with optional components.
 *
 * @param {string} pattern - The pattern string with optional components
 * @param {object} options - Configuration options for the validator
 * @returns {function(string): boolean} A function that validates paths against the pattern
 */
function createValidator(pattern, options) {
  // IMPLEMENTATION HERE
}

module.exports = { createValidator };

Dependencies { .dependencies }

picomatch { .dependency }

Provides glob pattern matching support with extglob features.