Comprehensive Guide to Nanomatch A Powerful Matching Library for JavaScript

Introduction to Nanomatch

Nanomatch is a powerful and lightweight library for matching file paths and patterns in JavaScript. It provides an extensive set of features, including support for wildcards, extglobs, posix brackets, brace expansion, and more. In this guide, we’ll explore the various APIs offered by Nanomatch and provide practical examples to help you get started.

Installation

npm install nanomatch --save

APIs and Examples

nm()

The nm function is the core API for matching strings against patterns. It returns an array of matched strings.


const nm = require('nanomatch');
console.log(nm(['a.js', 'a.txt', 'b.js'], '*.js'));
// Output: ['a.js', 'b.js']

isMatch()

The isMatch function checks if a single string matches a pattern. It returns a boolean value.


console.log(nm.isMatch('foo.js', '*.js'));
// Output: true

console.log(nm.isMatch('bar.txt', '*.js'));
// Output: false

matcher()

The matcher function creates a matcher function that can be reused for multiple strings.


const match = nm.matcher('*.js');
console.log(match('foo.js'));
// Output: true

console.log(match('bar.txt'));
// Output: false

matchKeys()

The matchKeys function filters the keys of an object based on a pattern.


const obj = { 'a.js': 1, 'a.txt': 2, 'b.js': 3 };
console.log(nm.matchKeys(obj, '*.js'));
// Output: { 'a.js': 1, 'b.js': 3 }

contains()

The contains function checks if a pattern exists in a given string.


console.log(nm.contains('foo/bar.baz', 'bar.*'));
// Output: true

console.log(nm.contains('foo/baz.qux', 'bar.*'));
// Output: false

some()

The some function checks if any string in an array matches a pattern.


console.log(nm.some(['a.js', 'b.txt'], '*.js'));
// Output: true

console.log(nm.some(['a.txt', 'b.txt'], '*.js'));
// Output: false

every()

The every function checks if all strings in an array match a pattern.


console.log(nm.every(['a.js', 'b.js'], '*.js'));
// Output: true

console.log(nm.every(['a.js', 'b.txt'], '*.js'));
// Output: false

not()

The not function returns an array of strings that do not match a pattern.


console.log(nm.not(['a.js', 'b.js', 'c.txt'], '*.js'));
// Output: ['c.txt']

App Example

Here is an example application that demonstrates how to use Nanomatch APIs to filter a list of file paths based on user input:


const nm = require('nanomatch');
const files = ['index.html', 'style.css', 'app.js', 'README.md'];

function filterFiles(pattern) {
  return nm(files, pattern);
}

console.log(filterFiles('*.js'));
// Output: ['app.js']

console.log(filterFiles('*.md'));
// Output: ['README.md']

In this example, we define a filterFiles function that takes a pattern as input and returns the files that match the pattern using the nm function.

These examples showcase just a fraction of the capabilities of Nanomatch. You can explore more by delving into the documentation and experimenting with different patterns and matching techniques.

Hash: 4f97b74c69a469bb68429675b8419c778532d7e4a8ea5af0e558138ed86540a5

Leave a Reply

Your email address will not be published. Required fields are marked *