CtrlK
CommunityDocumentationLog inGet started
Tessl Logo

tessl/npm-glob-to-regexp

tessl install tessl/npm-glob-to-regexp@0.4.0

Convert globs to regular expressions

Agent Success

Agent success rate when using this tile

100%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.15x

Baseline

Agent success rate without this tile

87%

task.mdevals/scenario-2/

File Validator with Pattern Matching

Overview

Build a file validator that uses pattern matching to determine if filenames are valid according to predefined naming conventions. The validator should support character ranges and character classes for flexible matching.

Requirements

Create a module that exports a validateFilename function with the following behavior:

Function Signature

validateFilename(filename, pattern)

Parameters:

  • filename (string): The filename to validate
  • pattern (string): A pattern string that supports character ranges

Returns:

  • true if the filename matches the pattern
  • false otherwise

Pattern Syntax

Your validator must support the following pattern syntax:

  1. Literal characters: Match exactly as written (e.g., file matches "file")

  2. Asterisk wildcard (*): Matches any sequence of characters (e.g., *.txt matches any file ending in .txt)

  3. Character classes ([]): Match exactly one character from a set

    • Single characters: [abc] matches 'a', 'b', or 'c'
    • Ranges: [a-z] matches any lowercase letter
    • Multiple ranges: [a-zA-Z] matches any letter
    • Numbers: [0-9] matches any digit
    • Combined: [a-z0-9] matches any lowercase letter or digit

Examples

  • validateFilename("report1.txt", "report[0-9].txt")true
  • validateFilename("reportA.txt", "report[0-9].txt")false
  • validateFilename("test-a.js", "test-[a-z].js")true
  • validateFilename("Test-A.js", "test-[a-z].js")false
  • validateFilename("config.prod.json", "config.[a-z]*.json")true
  • validateFilename("file123.log", "file[0-9][0-9][0-9].log")true

Implementation Notes

  • Patterns should match the entire filename (anchored matching)
  • Character classes should support standard range syntax like [a-z], [0-9], [A-Z]
  • Special characters in patterns (like dots) should be treated literally

Dependencies { .dependencies }

glob-to-regexp { .dependency }

Provides glob pattern to regular expression conversion support.

Test Cases

Test Case 1: Single digit range { .test-case @test }

Test file: validator.test.js

const { validateFilename } = require('./validator');

test('matches single digit in range', () => {
  expect(validateFilename('file0.txt', 'file[0-9].txt')).toBe(true);
  expect(validateFilename('file5.txt', 'file[0-9].txt')).toBe(true);
  expect(validateFilename('file9.txt', 'file[0-9].txt')).toBe(true);
});

test('rejects characters outside digit range', () => {
  expect(validateFilename('filea.txt', 'file[0-9].txt')).toBe(false);
  expect(validateFilename('fileA.txt', 'file[0-9].txt')).toBe(false);
});

Test Case 2: Letter ranges { .test-case @test }

Test file: validator.test.js

test('matches lowercase letter range', () => {
  expect(validateFilename('test-a.js', 'test-[a-z].js')).toBe(true);
  expect(validateFilename('test-m.js', 'test-[a-z].js')).toBe(true);
  expect(validateFilename('test-z.js', 'test-[a-z].js')).toBe(true);
});

test('matches uppercase letter range', () => {
  expect(validateFilename('LOG-A.txt', 'LOG-[A-Z].txt')).toBe(true);
  expect(validateFilename('LOG-Z.txt', 'LOG-[A-Z].txt')).toBe(true);
});

Test Case 3: Combined ranges with wildcards { .test-case @test }

Test file: validator.test.js

test('matches combined patterns with wildcards', () => {
  expect(validateFilename('config.dev.json', 'config.[a-z]*.json')).toBe(true);
  expect(validateFilename('config.production.json', 'config.[a-z]*.json')).toBe(true);
});

test('rejects when first character not in range', () => {
  expect(validateFilename('config.1dev.json', 'config.[a-z]*.json')).toBe(false);
});

Test Case 4: Multiple character classes { .test-case @test }

Test file: validator.test.js

test('matches multiple character classes', () => {
  expect(validateFilename('v1.2.txt', 'v[0-9].[0-9].txt')).toBe(true);
  expect(validateFilename('v9.0.txt', 'v[0-9].[0-9].txt')).toBe(true);
});

test('rejects when any position mismatches', () => {
  expect(validateFilename('va.2.txt', 'v[0-9].[0-9].txt')).toBe(false);
  expect(validateFilename('v1.b.txt', 'v[0-9].[0-9].txt')).toBe(false);
});

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/glob-to-regexp@0.4.x
tile.json