@push.rocks/smartmatch

A minimal RegExp-based library for case-insensitive wildcard string matching.

Issue Reporting and Security

For reporting bugs, issues, or security vulnerabilities, please visit community.foss.global/. This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a code.foss.global/ account to submit Pull Requests directly.

Install

Install @push.rocks/smartmatch with pnpm:

pnpm add @push.rocks/smartmatch

This will add @push.rocks/smartmatch to your project's dependencies and you're ready to start using it in your application.

Usage

SmartMatch matches an entire string case-insensitively. * matches zero or more characters, including newlines. A leading ! negates the result. All other characters, including ?, brackets, braces and backslashes, are literal; a backslash does not escape a wildcard.

Getting Started with @push.rocks/smartmatch

Firstly, ensure that you import @push.rocks/smartmatch into your project:

import { SmartMatch } from '@push.rocks/smartmatch';

Creating a Matcher Instance

The SmartMatch class is the entry point to the library's functionality. You create an instance of SmartMatch by providing a wildcard pattern to its constructor. This pattern will be used for matching against the strings.

Here’s an example:

const myMatcher = new SmartMatch('*.ts');

This matcher will be used to determine if a given string matches the wildcard pattern *.ts, meaning it should end with .ts.

Matching Strings

To match a string against the pattern, use the match method on your SmartMatch instance. This method takes a string as an argument and returns a boolean indicating whether the string matches the pattern.

Example usage:

// Assuming myMatcher is instantiated with '*.ts' as shown above.

const fileName = 'example.ts';

if (myMatcher.match(fileName)) {
  console.log(`${fileName} matches the pattern!`);
} else {
  console.log(`${fileName} does not match the pattern.`);
}

Advanced Use Cases

@push.rocks/smartmatch is designed to be minimal, but you can create multiple instances of SmartMatch with different patterns to support more complex matching logic in your application. This is particularly useful for applications that deal with various types of files, names, or identifiers that follow different naming conventions.

For example:

const scriptMatcher = new SmartMatch('*.js');
const typeScriptMatcher = new SmartMatch('*.ts');

const checkFileType = (fileName: string) => {
  if (scriptMatcher.match(fileName)) {
    console.log(`${fileName} is a JavaScript file.`);
  } else if (typeScriptMatcher.match(fileName)) {
    console.log(`${fileName} is a TypeScript file.`);
  } else {
    console.log(`${fileName} is of an unknown type.`);
  }
};

// Test the function
checkFileType('test.js');  // Output: test.js is a JavaScript file.
checkFileType('module.ts');  // Output: module.ts is a TypeScript file.
checkFileType('image.png');  // Output: image.png is of an unknown type.

This makes @push.rocks/smartmatch a versatile library suitable for various applications that require simple yet efficient matching logic.

Conclusion

@push.rocks/smartmatch offers an elegant solution for matching strings against wildcard patterns. Whether you are developing a file utility tool, a project management app, or any software that needs to filter or categorize data based on naming patterns, @push.rocks/smartmatch could be the library you need. Its simplicity and effectiveness make it a valuable tool for developers looking for a minimal matching library.

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in license.md.

Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

Company Information

Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.

S
Description
A minimal matching library using picomatch for string matching based on wildcards.
Readme
458 KiB
Languages
TypeScript 100%