tessl install tessl/npm-glob-to-regexp@0.4.0Convert 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%
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.
Create a module that exports a validateFilename function with the following behavior:
validateFilename(filename, pattern)Parameters:
filename (string): The filename to validatepattern (string): A pattern string that supports character rangesReturns:
true if the filename matches the patternfalse otherwiseYour validator must support the following pattern syntax:
Literal characters: Match exactly as written (e.g., file matches "file")
Asterisk wildcard (*): Matches any sequence of characters (e.g., *.txt matches any file ending in .txt)
Character classes ([]): Match exactly one character from a set
[abc] matches 'a', 'b', or 'c'[a-z] matches any lowercase letter[a-zA-Z] matches any letter[0-9] matches any digit[a-z0-9] matches any lowercase letter or digitvalidateFilename("report1.txt", "report[0-9].txt") → truevalidateFilename("reportA.txt", "report[0-9].txt") → falsevalidateFilename("test-a.js", "test-[a-z].js") → truevalidateFilename("Test-A.js", "test-[a-z].js") → falsevalidateFilename("config.prod.json", "config.[a-z]*.json") → truevalidateFilename("file123.log", "file[0-9][0-9][0-9].log") → true[a-z], [0-9], [A-Z]Provides glob pattern to regular expression conversion support.
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 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 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 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);
});