https://github.com/edjcase/motoko_glob
A library for matching glob patterns
https://github.com/edjcase/motoko_glob
Last synced: 5 months ago
JSON representation
A library for matching glob patterns
- Host: GitHub
- URL: https://github.com/edjcase/motoko_glob
- Owner: edjCase
- License: mit
- Created: 2025-02-13T16:02:02.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-09-06T20:58:01.000Z (10 months ago)
- Last Synced: 2025-09-06T22:26:23.245Z (10 months ago)
- Language: Motoko
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Glob: Path Pattern Matching for Motoko
A glob pattern matching library for Motoko that supports a wide range of pattern matching features.
## Installation
```bash
mops install glob
```
To set up MOPS package manager, follow the instructions from the
[MOPS Site](https://j4mwm-bqaaa-aaaam-qajbq-cai.ic0.app/)
## Quick Start
```motoko
import Glob "mo:glob";
// Basic matching
Glob.match("file.txt", "*.txt"); // true
Glob.match("dir/file.txt", "**/*.txt"); // true
Glob.match("script.js", "*.txt"); // false
// Directory matching
Glob.match("/foo/bar/baz", "/foo/*/baz"); // true
Glob.match("/foo/bar/qux/baz", "/foo/**/baz"); // true
```
## Features
### Basic Wildcards
- `*` matches any number of characters within a path segment
- `?` matches exactly one character
```motoko
Glob.match("hello.txt", "h*.txt"); // true
Glob.match("hello.txt", "h????.txt"); // true
```
### Directory Wildcards
- `*/` matches a single directory level
- `**/` matches zero or more directory levels
```motoko
Glob.match("/a/b/c", "/a/*/c"); // true
Glob.match("/a/b/c/d", "/a/**/d"); // true
```
### Character Classes
- `[abc]` matches any one character listed
- `[a-z]` matches any one character in the range
- `[!abc]` matches any one character not listed
```motoko
Glob.match("file1.txt", "file[1-3].txt"); // true
Glob.match("fileA.txt", "file[ABC].txt"); // true
Glob.match("fileD.txt", "file[!ABC].txt"); // true
```
### Pattern Negation
- `!pattern` matches paths that don't match the pattern
```motoko
Glob.match("/static/public/file.js", "!/static/private/**"); // true
Glob.match("/static/private/file.js", "!/static/private/**"); // false
```
### Special Notes
- Dot files (hidden files) are included in wildcard matches by default
- Path separator normalization is handled automatically
- Supports both absolute and relative paths
- Escaping special characters with backslash
## API Reference
### `match(path : Text, pattern : Text) : Bool`
Matches a path against a glob pattern and returns true if it matches.
Parameters:
- `path`: The path to test
- `pattern`: The glob pattern to match against
Returns:
- `Bool`: True if the path matches the pattern, false otherwise
## Examples
### File Extensions
```motoko
// Match specific file types
Glob.match("/static/file.js", "/static/*.js"); // true
Glob.match("/static/file.min.js", "/static/*.js"); // true
```
### Nested Directories
```motoko
// Match files in nested directories
Glob.match("/static/css/file.css", "/static/**/*.css"); // true
Glob.match("/static/css/nested/file.css", "/static/**/*.css"); // true
```
### Complex Patterns
```motoko
// Version directories with multiple extensions
Glob.match("/static/v1.2.3/app.js", "/static/v[0-9]*\\.[0-9]*\\.[0-9]*/**/*.js"); // true
// Multiple file extensions
Glob.match("/static/styles.min.css", "/static/**/*.min.{js,css}"); // true
```