Blazing fast and accurate glob matcher written in JavaScript, with no dependencies and full support for standard and extended Bash glob features, including braces, extglobs, POSIX brackets, and regular expressions.
83
Build a file path matcher utility that works consistently across different operating systems (Windows and Unix-like systems). Your utility should handle path patterns and match them against file paths, regardless of whether the paths use forward slashes (/) or backslashes (\).
Create a module that exports the following functionality:
Implement a function createMatcher(pattern, options) that:
platform field that can be either 'windows' or 'posix'Implement a function matchPaths(paths, pattern, options) that:
platform fieldImplement a function normalizePath(path, targetPlatform) that:
* (matches any characters except path separators)** (matches any characters including path separators)? (matches exactly one character except path separator)/ and \\ should be treated as valid path separators/ should be treated as a path separator// Windows platform
const matcher = createMatcher('src/**/*.js', { platform: 'windows' });
matcher('src\\components\\Button.js'); // true
matcher('src/components/Button.js'); // true
// POSIX platform
const posixMatcher = createMatcher('src/**/*.js', { platform: 'posix' });
posixMatcher('src/components/Button.js'); // true
posixMatcher('src\\components\\Button.js'); // false (backslash not a separator)
// Batch matching
const paths = [
'src\\utils\\helper.js',
'src/components/App.js',
'test/unit/app.test.js',
'src/styles/main.css'
];
matchPaths(paths, 'src/**/*.js', { platform: 'windows' });
// Returns: ['src\\utils\\helper.js', 'src/components/App.js']
// Path normalization
normalizePath('src\\components\\Button.js', 'posix'); // 'src/components/Button.js'
normalizePath('src/components/Button.js', 'windows'); // 'src\\components\\Button.js'Provides fast and accurate glob matching functionality.
Create a test file named matcher.test.js with the following test cases:
const matcher = createMatcher('**/*.js', { platform: 'windows' });
assert.strictEqual(matcher('src\\app.js'), true);
assert.strictEqual(matcher('src\\components\\button.js'), true);const matcher = createMatcher('**/*.js', { platform: 'windows' });
assert.strictEqual(matcher('src/app.js'), true);
assert.strictEqual(matcher('src/components/button.js'), true);const posixMatcher = createMatcher('src/**/*.js', { platform: 'posix' });
assert.strictEqual(posixMatcher('src/components/App.js'), true);
assert.strictEqual(posixMatcher('src\\components\\App.js'), false);const paths = ['src\\utils\\helper.js', 'test\\app.test.js', 'src\\styles\\main.css'];
const result = matchPaths(paths, 'src/**/*.js', { platform: 'windows' });
assert.strictEqual(result.length, 1);
assert.strictEqual(result[0], 'src\\utils\\helper.js');assert.strictEqual(normalizePath('src\\components\\Button.js', 'posix'), 'src/components/Button.js');
assert.strictEqual(normalizePath('src/components/Button.js', 'windows'), 'src\\components\\Button.js');Install with Tessl CLI
npx tessl i tessl/npm-picomatchevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10