Skip to content

Commit e2d9fb9

Browse files
committed
doc(fs): improve glob documentation formatting per review feedback
1 parent 3bf6d2c commit e2d9fb9

File tree

1 file changed

+117
-49
lines changed

1 file changed

+117
-49
lines changed

doc/api/fs.md

Lines changed: 117 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,99 +1101,167 @@ changes:
11011101
* Returns: {AsyncIterator} An AsyncIterator that yields the paths of files
11021102
that match the pattern.
11031103
1104-
Retrieves the files matching the specified pattern(s).
1104+
Retrieves the file paths matching the specified pattern(s).
11051105
11061106
#### Pattern Syntax
11071107
11081108
The glob implementation supports the following pattern syntax:
11091109
11101110
**Basic Wildcards:**
1111-
* `*` - Matches any number of characters within a single path segment
1112-
* `?` - Matches exactly one character
1113-
* `[abc]` - Matches any character in the brackets
1114-
* `[a-z]` - Matches any character in the range
1115-
* `[!abc]` or `[^abc]` - Matches any character not in the brackets
1116-
* `[!a-z]` or `[^a-z]` - Matches any character not in the range
1111+
1112+
| Pattern | Description |
1113+
| ------- | ----------- |
1114+
| `*` | Matches any number of characters within a single path segment. |
1115+
| `?` | Matches exactly one character. |
1116+
| `[abc]` | Matches any character in the brackets. |
1117+
| `[a-z]` | Matches any character in the range. |
1118+
| `[!abc]` or `[^abc]` | Matches any character not in the brackets. |
1119+
| `[!a-z]` or `[^a-z]` | Matches any character not in the range. |
11171120
11181121
**Globstar:**
1119-
* `**` - Matches zero or more directories recursively
1120-
* `**/*.js` - Matches all `.js` files in any subdirectory
1122+
1123+
| Pattern | Description |
1124+
| ------- | ----------- |
1125+
| `**` | Matches zero or more directories recursively. |
1126+
| `**/*.js` | Matches all `.js` files in any subdirectory. |
11211127
11221128
**Brace Expansion:**
1123-
* `{a,b,c}` - Matches any of the comma-separated patterns
1124-
* `{1..5}` - Matches any number in the range (1, 2, 3, 4, 5)
1125-
* `*.{js,ts}` - Matches files with `.js` or `.ts` extensions
1129+
1130+
| Pattern | Description |
1131+
| ------- | ----------- |
1132+
| `{a,b,c}` | Matches any of the comma-separated patterns. |
1133+
| `{1..5}` | Matches any number in the range (1, 2, 3, 4, 5). |
1134+
| `*.{js,ts}` | Matches files with `.js` or `.ts` extensions. |
11261135
11271136
**Extended Glob Patterns:**
1128-
* `+(pattern)` - Matches one or more occurrences of the pattern
1129-
* `*(pattern)` - Matches zero or more occurrences of the pattern
1130-
* `?(pattern)` - Matches zero or one occurrence of the pattern
1131-
* `!(pattern)` - Matches anything except the pattern
1137+
1138+
| Pattern | Description |
1139+
| ------- | ----------- |
1140+
| `+(pattern)` | Matches one or more occurrences of the pattern. |
1141+
| `*(pattern)` | Matches zero or more occurrences of the pattern. |
1142+
| `?(pattern)` | Matches zero or one occurrence of the pattern. |
1143+
| `!(pattern)` | Matches anything except the pattern. |
11321144
11331145
#### Examples
11341146
1147+
The following examples demonstrate common glob usage patterns.
1148+
1149+
**Basic pattern matching:**
1150+
1151+
Match all `.js` files in the current directory.
1152+
11351153
```mjs
11361154
import { glob } from 'node:fs/promises';
11371155
1138-
// Basic patterns
11391156
for await (const entry of glob('*.js'))
1140-
console.log(entry); // All .js files in current directory
1157+
console.log(entry);
1158+
```
1159+
1160+
```cjs
1161+
const { glob } = require('node:fs/promises');
1162+
1163+
(async () => {
1164+
for await (const entry of glob('*.js'))
1165+
console.log(entry);
1166+
})();
1167+
```
1168+
1169+
**Recursive search with brace expansion:**
1170+
1171+
Match all `.js` and `.ts` files in any subdirectory.
1172+
1173+
```mjs
1174+
import { glob } from 'node:fs/promises';
11411175
1142-
// Recursive search
11431176
for await (const entry of glob('**/*.{js,ts}'))
1144-
console.log(entry); // All .js and .ts files in any subdirectory
1177+
console.log(entry);
1178+
```
1179+
1180+
**Using `Array.fromAsync` to collect results:**
1181+
1182+
Convert the async iterator to an array for easier manipulation.
1183+
1184+
```mjs
1185+
import { glob } from 'node:fs/promises';
1186+
1187+
const files = await Array.fromAsync(glob('src/**/*.js'));
1188+
console.log(files);
1189+
```
1190+
1191+
**Brace expansion for multiple directories:**
1192+
1193+
Match `.js` files in `src/components` or `src/utils` directories.
1194+
1195+
```mjs
1196+
import { glob } from 'node:fs/promises';
11451197
1146-
// Brace expansion
11471198
for await (const entry of glob('src/{components,utils}/**/*.js'))
1148-
console.log(entry); // .js files in src/components or src/utils
1199+
console.log(entry);
1200+
```
1201+
1202+
**Extended glob patterns:**
1203+
1204+
Match test files ending with `.spec.js` or `.test.js`. Note that the `+(spec|test)`
1205+
pattern also matches files like `foo.spectest.js`.
1206+
1207+
```mjs
1208+
import { glob } from 'node:fs/promises';
11491209
1150-
// Extended glob patterns
11511210
for await (const entry of glob('test/**/*.+(spec|test).js'))
1152-
console.log(entry); // Test files ending with .spec.js or .test.js
1153-
// Note: This also matches files like 'foo.spectest.js'
1211+
console.log(entry);
1212+
```
1213+
1214+
For more precise matching, use brace expansion instead:
1215+
1216+
```mjs
1217+
import { glob } from 'node:fs/promises';
11541218
1155-
// More precise pattern using brace expansion
11561219
for await (const entry of glob('test/**/*.{spec,test}.js'))
1157-
console.log(entry); // Only matches .spec.js or .test.js files
1220+
console.log(entry);
1221+
```
11581222
1159-
// Excluding patterns
1160-
for await (const entry of glob('**/*.js', { exclude: ['node_modules/**'] }))
1161-
console.log(entry); // All .js files except those in node_modules
1223+
**Excluding patterns:**
11621224
1163-
// Multiple patterns
1164-
for await (const entry of glob(['src/**/*.js', 'lib/**/*.js']))
1165-
console.log(entry); // .js files in both src and lib directories
1225+
Match all `.js` files except those in `node_modules`.
1226+
1227+
```mjs
1228+
import { glob } from 'node:fs/promises';
1229+
1230+
for await (const entry of glob('**/*.js', { exclude: ['node_modules/**'] }))
1231+
console.log(entry);
11661232
```
11671233
1168-
```cjs
1169-
const { glob } = require('node:fs/promises');
1234+
**Multiple patterns:**
11701235
1171-
(async () => {
1172-
for await (const entry of glob('**/*.js'))
1173-
console.log(entry);
1174-
})();
1236+
Match `.js` files in both `src` and `lib` directories.
1237+
1238+
```mjs
1239+
import { glob } from 'node:fs/promises';
1240+
1241+
for await (const entry of glob(['src/**/*.js', 'lib/**/*.js']))
1242+
console.log(entry);
11751243
```
11761244
11771245
#### Platform Considerations
11781246
11791247
* **Case sensitivity**: Glob patterns are case-insensitive on Windows and macOS,
1180-
case-sensitive on other platforms
1248+
case-sensitive on other platforms.
11811249
* **Path separators**: Forward slashes (`/`) are used in patterns regardless of
1182-
platform; they are automatically converted to the appropriate separator
1250+
platform; they are automatically converted to the appropriate separator.
11831251
* **Returned paths**: Results use platform-specific path separators (`\` on Windows,
11841252
`/` on POSIX). Paths are relative to `options.cwd` if provided, otherwise
1185-
relative to the current working directory
1186-
* **Symlinks**: Symbolic links are followed and their targets are included in results
1253+
relative to the current working directory.
1254+
* **Symlinks**: Symbolic links are followed and their targets are included in results.
11871255
* **Hidden files**: Files starting with `.` are included in results when explicitly
1188-
matched, but not when using `*` or `**` patterns
1256+
matched, but not when using `*` or `**` patterns.
11891257
11901258
#### Performance Notes
11911259
11921260
* Results are streamed as an async iterator, allowing for efficient memory usage
1193-
with large result sets
1194-
* The implementation includes internal caching to avoid duplicate file system operations
1261+
with large result sets.
1262+
* The implementation includes internal caching to avoid duplicate file system operations.
11951263
* For better performance with large directory trees, consider using more specific
1196-
patterns to reduce the search scope
1264+
patterns to reduce the search scope.
11971265
11981266
[`path.matchesGlob()`][] supports the same basic glob pattern syntax for
11991267
in-memory path matching, but does not include the `exclude` option.
@@ -3245,7 +3313,7 @@ changes:
32453313
Windows, `/` on POSIX) and are relative to `options.cwd` if provided,
32463314
otherwise relative to the current working directory.
32473315
3248-
Retrieves the files matching the specified pattern(s).
3316+
Retrieves the file paths matching the specified pattern(s).
32493317
32503318
See `fsPromises.glob()` for detailed information about pattern syntax,
32513319
examples, and behavior.
@@ -5801,7 +5869,7 @@ changes:
58015869
Windows, `/` on POSIX) and are relative to `options.cwd` if provided,
58025870
otherwise relative to the current working directory.
58035871
5804-
Retrieves the files matching the specified pattern(s).
5872+
Retrieves the file paths matching the specified pattern(s).
58055873
58065874
See `fsPromises.glob()` for detailed information about pattern syntax,
58075875
examples, and behavior.

0 commit comments

Comments
 (0)