CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-glob-to-regexp

Convert globs to regular expressions

Overall
score

100%

Overview
Eval results
Files

task.mdevals/scenario-8/

File Type Validator

Build a file type validation utility that checks if file names match specific allowed extensions or naming patterns.

Requirements

Create a function called isValidFile that takes a file name (string) and a pattern (string) and returns true if the file name matches the pattern, false otherwise.

The pattern should support:

  • Multiple alternative extensions using comma-separated values in curly braces
  • Wildcards for matching any characters
  • Literal dots and other special characters

The function should be case-sensitive by default.

Examples

  • Pattern *.{js,ts} should match index.js and app.ts but not style.css
  • Pattern test-*.{spec,test}.js should match test-user.spec.js and test-auth.test.js but not main.test.js
  • Pattern {readme,license}.{md,txt} should match readme.md, license.txt, and readme.txt but not changelog.md

Test Cases

  • isValidFile("component.jsx", "*.{js,jsx}") returns true @test
  • isValidFile("style.css", "*.{js,jsx}") returns false @test
  • isValidFile("config.json", "config.{json,yaml,yml}") returns true @test
  • isValidFile("test-utils.js", "{test,spec}-*.js") returns true @test
  • isValidFile("utils-test.js", "{test,spec}-*.js") returns false @test

Implementation

@generates

API

/**
 * Validates if a file name matches a given pattern with alternative extensions.
 *
 * @param {string} fileName - The file name to validate.
 * @param {string} pattern - The pattern with alternatives (e.g., "*.{js,ts}").
 * @returns {boolean} True if the file name matches the pattern, false otherwise.
 */
function isValidFile(fileName, pattern) {
  // IMPLEMENTATION HERE
}

module.exports = { isValidFile };

Dependencies { .dependencies }

glob-to-regexp { .dependency }

Provides pattern matching with support for alternative extensions using curly brace syntax.

@satisfied-by

Install with Tessl CLI

npx tessl i tessl/npm-glob-to-regexp

tile.json