Skip to content

Commit 2bc0ba6

Browse files
committed
doc: enhance glob pattern documentation in fs.md
Updated the glob pattern section to include detailed syntax explanations, examples, and performance notes. Clarified the `pattern` parameter description and added information on case sensitivity, path separators, and symlink handling. Improved overall clarity and usability of the documentation.
1 parent 9148e96 commit 2bc0ba6

File tree

1 file changed

+87
-9
lines changed

1 file changed

+87
-9
lines changed

doc/api/fs.md

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ changes:
10901090
description: Add support for `withFileTypes` as an option.
10911091
-->
10921092
1093-
* `pattern` {string|string\[]}
1093+
* `pattern` {string|string\[]} The glob pattern(s) to match against.
10941094
* `options` {Object}
10951095
* `cwd` {string} current working directory. **Default:** `process.cwd()`
10961096
* `exclude` {Function|string\[]} Function to filter out files/directories or a
@@ -1101,11 +1101,62 @@ 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).
1105+
1106+
#### Pattern Syntax
1107+
1108+
The glob implementation supports the following pattern syntax:
1109+
1110+
**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+
1117+
**Globstar:**
1118+
* `**` - Matches zero or more directories recursively
1119+
* `**/*.js` - Matches all `.js` files in any subdirectory
1120+
1121+
**Brace Expansion:**
1122+
* `{a,b,c}` - Matches any of the comma-separated patterns
1123+
* `{1..5}` - Matches any number in the range (1, 2, 3, 4, 5)
1124+
* `*.{js,ts}` - Matches files with `.js` or `.ts` extensions
1125+
1126+
**Extended Glob Patterns:**
1127+
* `+(pattern)` - Matches one or more occurrences of the pattern
1128+
* `*(pattern)` - Matches zero or more occurrences of the pattern
1129+
* `?(pattern)` - Matches zero or one occurrence of the pattern
1130+
* `!(pattern)` - Matches anything except the pattern
1131+
1132+
#### Examples
1133+
11041134
```mjs
11051135
import { glob } from 'node:fs/promises';
11061136
1107-
for await (const entry of glob('**/*.js'))
1108-
console.log(entry);
1137+
// Basic patterns
1138+
for await (const entry of glob('*.js'))
1139+
console.log(entry); // All .js files in current directory
1140+
1141+
// Recursive search
1142+
for await (const entry of glob('**/*.{js,ts}'))
1143+
console.log(entry); // All .js and .ts files in any subdirectory
1144+
1145+
// Brace expansion
1146+
for await (const entry of glob('src/{components,utils}/**/*.js'))
1147+
console.log(entry); // .js files in src/components or src/utils
1148+
1149+
// Extended glob patterns
1150+
for await (const entry of glob('test/**/*.+(spec|test).js'))
1151+
console.log(entry); // Test files ending with .spec.js or .test.js
1152+
1153+
// Excluding patterns
1154+
for await (const entry of glob('**/*.js', { exclude: ['node_modules/**'] }))
1155+
console.log(entry); // All .js files except those in node_modules
1156+
1157+
// Multiple patterns
1158+
for await (const entry of glob(['src/**/*.js', 'lib/**/*.js']))
1159+
console.log(entry); // .js files in both src and lib directories
11091160
```
11101161
11111162
```cjs
@@ -1117,6 +1168,24 @@ const { glob } = require('node:fs/promises');
11171168
})();
11181169
```
11191170
1171+
#### Platform Considerations
1172+
1173+
* **Case sensitivity**: Glob patterns are case-insensitive on Windows and macOS,
1174+
case-sensitive on other platforms
1175+
* **Path separators**: Forward slashes (`/`) are used in patterns regardless of
1176+
platform; they are automatically converted to the appropriate separator
1177+
* **Symlinks**: Symbolic links are followed and their targets are included in results
1178+
* **Hidden files**: Files starting with `.` are included in results when explicitly
1179+
matched, but not when using `*` or `**` patterns
1180+
1181+
#### Performance Notes
1182+
1183+
* Results are streamed as an async iterator, allowing for efficient memory usage
1184+
with large result sets
1185+
* The implementation includes internal caching to avoid duplicate filesystem operations
1186+
* For better performance with large directory trees, consider using more specific
1187+
patterns to reduce the search scope
1188+
11201189
### `fsPromises.lchmod(path, mode)`
11211190
11221191
<!-- YAML
@@ -3149,20 +3218,23 @@ changes:
31493218
description: Add support for `withFileTypes` as an option.
31503219
-->
31513220
3152-
* `pattern` {string|string\[]}
3153-
3221+
* `pattern` {string|string\[]} The glob pattern(s) to match against.
31543222
* `options` {Object}
31553223
* `cwd` {string} current working directory. **Default:** `process.cwd()`
31563224
* `exclude` {Function|string\[]} Function to filter out files/directories or a
31573225
list of glob patterns to be excluded. If a function is provided, return
31583226
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
31593227
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
31603228
`false` otherwise. **Default:** `false`.
3161-
31623229
* `callback` {Function}
31633230
* `err` {Error}
3231+
* `matches` {string\[]|Dirent\[]} An array of paths or Dirent objects that
3232+
match the pattern.
3233+
3234+
Retrieves the files matching the specified pattern(s).
31643235
3165-
* Retrieves the files matching the specified pattern.
3236+
See [`fsPromises.glob()`][] for detailed information about pattern syntax,
3237+
examples, and behavior.
31663238
31673239
```mjs
31683240
import { glob } from 'node:fs';
@@ -5702,15 +5774,21 @@ changes:
57025774
description: Add support for `withFileTypes` as an option.
57035775
-->
57045776
5705-
* `pattern` {string|string\[]}
5777+
* `pattern` {string|string\[]} The glob pattern(s) to match against.
57065778
* `options` {Object}
57075779
* `cwd` {string} current working directory. **Default:** `process.cwd()`
57085780
* `exclude` {Function|string\[]} Function to filter out files/directories or a
57095781
list of glob patterns to be excluded. If a function is provided, return
57105782
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
57115783
* `withFileTypes` {boolean} `true` if the glob should return paths as Dirents,
57125784
`false` otherwise. **Default:** `false`.
5713-
* Returns: {string\[]} paths of files that match the pattern.
5785+
* Returns: {string\[]|Dirent\[]} An array of paths or Dirent objects that
5786+
match the pattern.
5787+
5788+
Retrieves the files matching the specified pattern(s).
5789+
5790+
See [`fsPromises.glob()`][] for detailed information about pattern syntax,
5791+
examples, and behavior.
57145792
57155793
```mjs
57165794
import { globSync } from 'node:fs';

0 commit comments

Comments
 (0)