new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Bun · Runtime · all subjects

bun apis/glob

16 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Bun.Glob for file globbing

Bun provides Bun.Glob for pattern-based file matching and globbing operations.

Glob.scan() async iteration

The scan() method accepts a root parameter (string or ScanOptions object) and returns an AsyncIterable<string> of matching file paths. It scans the directory and all sub-directories recursively. Example: for await (const file of glob.scan('.')) { console.log(file); }

Glob.scanSync() synchronous iteration

The scanSync() method accepts a root parameter (string or ScanOptions object) and returns an Iterable<string> of matching file paths, scanning synchronously.

Glob.match() pattern matching

The match() method takes a path string and returns a boolean indicating whether the path matches the glob pattern.

ScanOptions interface

ScanOptions has the following fields: cwd (string, optional, defaults to process.cwd()), dot (boolean, optional, defaults to false - allow patterns to match entries beginning with '.'), absolute (boolean, optional, defaults to false - return absolute paths), followSymlinks (boolean, optional, defaults to false - traverse descendants of symbolic link directories), throwErrorOnBrokenSymlink (boolean, optional, defaults to false - throw error when symbolic link is broken), onlyFiles (boolean, optional, defaults to true - return only files).

Glob pattern: ? single character

The ? pattern matches any single character. Example: '???.ts' matches 'foo.ts' but not 'foobar.ts'.

Glob pattern: * zero or more characters

The * pattern matches zero or more characters, except for path separators (/ or \). Example: '*.ts' matches 'index.ts' but not 'src/index.ts'.

Glob pattern: ** any characters including slashes

The ** pattern matches any number of characters including path separators (/ or \). Example: '**/*.ts' matches both 'index.ts' and 'src/index.ts'.

Glob class import and basic usage

The Glob class is imported from 'bun'. Create a new Glob instance with a pattern string, then use scan() or scanSync() methods to find matching files in a directory, or use match() to test a string against the pattern.

Glob pattern: {a,b,c} alternation

The {a,b,c} pattern matches any of the given patterns separated by commas. These patterns can be nested up to 10 levels deep and contain any other wildcards. Example: '{a,b,c}.ts' matches 'a.ts', 'b.ts', and 'c.ts' but not 'd.ts'.

Glob pattern: ! negation

The ! pattern at the start of a pattern negates the result. Example: '!index.ts' returns true for 'foo.ts' but false for 'index.ts'.

Glob pattern: \ escape character

The \ pattern escapes any special characters. Example: '\!index.ts' matches a file literally named '!index.ts'.

Node.js fs.glob() compatibility

Bun implements Node.js fs.glob(), fs.globSync(), and fs.promises.glob() functions from the 'node:fs' module. All three functions support array of patterns as the first argument and an exclude option to filter results. Example: await promises.glob(['**/*.ts', '**/*.js']) or await promises.glob('**/*', { exclude: ['node_modules/**', '*.test.*'] }).

Glob basic example

import { Glob } from 'bun'; const glob = new Glob('**/*.ts'); for await (const file of glob.scan('.')) { console.log(file); } This example demonstrates scanning the current directory and subdirectories for all TypeScript files.

Glob match example

import { Glob } from 'bun'; const glob = new Glob('*.ts'); glob.match('index.ts'); glob.match('index.js'); The first returns true, the second returns false.

Glob pattern: [ab] character class

The [ab] pattern matches one of the characters contained in the brackets. Character ranges like [0-9] or [a-z] are supported. Negation operators ^ or ! exclude characters, like [^ab] or [!a-z]. Example: 'ba[rz].ts' matches 'bar.ts' and 'baz.ts' but not 'bat.ts'.

Give your agent this brain