CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-picomatch

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

1.45x
Overview
Eval results
Files

task.mdevals/scenario-6/

Cross-Platform File Path Matcher

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 (\).

Requirements

Create a module that exports the following functionality:

1. Path Matcher Function

Implement a function createMatcher(pattern, options) that:

  • Accepts a glob pattern as a string
  • Accepts an options object with a platform field that can be either 'windows' or 'posix'
  • Returns a function that tests if a given path matches the pattern
  • The returned function should handle paths with either forward slashes or backslashes appropriately based on the platform

2. Batch Path Matching

Implement a function matchPaths(paths, pattern, options) that:

  • Accepts an array of file paths
  • Accepts a glob pattern
  • Accepts an options object with a platform field
  • Returns an array of paths that match the pattern
  • Should handle mixed slash styles in the input paths based on the platform setting

3. Path Normalization

Implement a function normalizePath(path, targetPlatform) that:

  • Converts a path to use the appropriate separator for the target platform
  • Returns a normalized path string

Technical Specifications

  • The matcher should support basic glob patterns including:
    • * (matches any characters except path separators)
    • ** (matches any characters including path separators)
    • ? (matches exactly one character except path separator)
  • On Windows mode, both / and \\ should be treated as valid path separators
  • On POSIX mode, only / should be treated as a path separator
  • Path matching should be case-insensitive on Windows and case-sensitive on POSIX by default

Example Usage

// 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'

Dependencies { .dependencies }

picomatch { .dependency }

Provides fast and accurate glob matching functionality.

Test Cases

Create a test file named matcher.test.js with the following test cases:

Test 1: Windows path matching with backslashes @test

const matcher = createMatcher('**/*.js', { platform: 'windows' });
assert.strictEqual(matcher('src\\app.js'), true);
assert.strictEqual(matcher('src\\components\\button.js'), true);

Test 2: Windows path matching with forward slashes @test

const matcher = createMatcher('**/*.js', { platform: 'windows' });
assert.strictEqual(matcher('src/app.js'), true);
assert.strictEqual(matcher('src/components/button.js'), true);

Test 3: POSIX path matching @test

const posixMatcher = createMatcher('src/**/*.js', { platform: 'posix' });
assert.strictEqual(posixMatcher('src/components/App.js'), true);
assert.strictEqual(posixMatcher('src\\components\\App.js'), false);

Test 4: Batch path matching on Windows @test

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');

Test 5: Path normalization @test

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-picomatch

tile.json