or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-4/

File Filter System

Build a file filtering system that matches files against include patterns while excluding specific patterns using ignore rules.

Background

You're building a build tool that needs to process files in a project directory. The system should match files based on inclusion patterns but also respect exclusion rules for files that should be ignored (like dependencies, build artifacts, and configuration files).

Requirements

Implement a file filtering system with the following features:

  1. Basic Filtering: Accept an array of file paths and one or more glob patterns to match against
  2. Ignore Patterns: Support an ignore list that excludes files from results even if they match the inclusion patterns
  3. Multiple Ignore Rules: Handle multiple ignore patterns (e.g., excluding both node_modules and .git directories)
  4. Pattern Priority: Ensure ignore patterns take precedence over inclusion patterns

Specifications

Create a module src/file-filter.js that exports a filterFiles function with the following signature:

/**
 * Filters an array of file paths based on include patterns and ignore rules
 * @param {string[]} files - Array of file paths to filter
 * @param {string|string[]} patterns - Glob pattern(s) to match
 * @param {string|string[]} ignorePatterns - Pattern(s) to exclude from results
 * @returns {string[]} - Filtered array of matching files
 */
function filterFiles(files, patterns, ignorePatterns) {
  // Implementation
}

Test Cases

Create src/file-filter.test.js with the following test cases:

Test 1: Basic ignore pattern @test

const files = [
  'src/app.js',
  'src/utils.js',
  'node_modules/lodash/index.js',
  'node_modules/react/index.js'
];
const result = filterFiles(files, '**/*.js', 'node_modules/**');
// Expected: ['src/app.js', 'src/utils.js']

Test 2: Multiple ignore patterns @test

const files = [
  'src/index.js',
  'test/test.js',
  'build/bundle.js',
  'dist/output.js',
  '.git/config'
];
const result = filterFiles(files, '**/*', ['build/**', 'dist/**', '.git/**']);
// Expected: ['src/index.js', 'test/test.js']

Test 3: Ignore takes precedence @test

const files = [
  'src/app.js',
  'src/app.test.js',
  'src/utils.js',
  'src/utils.test.js'
];
const result = filterFiles(files, 'src/**/*.js', '**/*.test.js');
// Expected: ['src/app.js', 'src/utils.js']

Dependencies { .dependencies }

micromatch { .dependency }

Provides glob pattern matching and filtering capabilities for file paths.

Constraints

  • Use only the standard library and the specified dependency
  • Keep the implementation simple and focused on the core requirement
  • Ensure ignore patterns consistently override inclusion patterns