Skip to content

Commit e1e2279

Browse files
committed
refactor(util): allow search depth to be 0
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 2666be1 commit e1e2279

File tree

6 files changed

+28
-18
lines changed

6 files changed

+28
-18
lines changed

.commitlintrc.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import { scopes } from '@flex-development/commitlint-config'
1515
const config: UserConfig = {
1616
extends: ['@flex-development'],
1717
rules: {
18-
'scope-enum': [RuleConfigSeverity.Error, 'always', scopes(['chore'])]
18+
'scope-enum': [RuleConfigSeverity.Error, 'always', scopes([
19+
'chore',
20+
'util'
21+
])]
1922
}
2023
}
2124

README.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,23 @@ Options for creating a file system tree (TypeScript interface).
9999

100100
#### Properties
101101

102-
- `content` (`boolean`, optional) — include file content (populates the `value` field of each [`file` node][fst-file])
103-
- `depth` (`number`, optional) — maximum search depth (inclusive)
104-
- `extensions` ([`Extensions`](#extensions), optional) — list of file extensions to filter matched files by
105-
- `filters` ([`Filters`](#filters), optional) — path filters to determine if nodes should be added to the tree
106-
- `fs` ([`Partial<FileSystem>`](#filesystem), optional) — file system adapter
107-
- `handles` ([`Handles`](#handles), optional) — node handlers
108-
- `root` (`URL | string`, optional) — module id of root directory
102+
- `content` (`boolean`, optional) —
103+
include file content (populates the `value` field of each [`file` node][fst-file])
104+
- `depth` (`number`, optional) —
105+
maximum search depth (inclusive). a search depth less than `0` will produce an empty tree
106+
- `extensions` ([`Extensions`](#extensions), optional) —
107+
list of file extensions to filter matched files by
108+
- `filters` ([`Filters`](#filters), optional) —
109+
path filters to determine if nodes should be added to the tree
110+
- `fs` ([`Partial<FileSystem>`](#filesystem), optional) —
111+
file system adapter
112+
- `handles` ([`Handles`](#handles), optional) —
113+
node handlers
114+
- `root` (`URL | string`, optional) —
115+
module id of root directory
109116
- **default**: [`pathe.cwd() + pathe.sep`][pathe]
110-
- `sort` ([`Sort`](#sort), optional) — function used to sort child nodes
117+
- `sort` ([`Sort`](#sort), optional) —
118+
function used to sort child nodes
111119

112120
### `Dirent`
113121

__tests__/utils/readdir.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function readdir(
4949
if (
5050
depth === null ||
5151
depth === undefined ||
52-
typeof depth === 'number' && depth > 0
52+
typeof depth === 'number' && depth >= 0
5353
) {
5454
dir = toPath(dir)
5555

src/__tests__/util.spec.mts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,9 @@ describe('unit:fromFileSystem', () => {
8484
}
8585
})
8686

87-
it.each<Parameters<typeof testSubject>>([
88-
[{ depth: -13 }],
89-
[{ depth: 0 }]
90-
])('should return empty tree (%#)', options => {
87+
it('should return empty tree', () => {
9188
// Act
92-
const result = testSubject(options)
89+
const result = testSubject({ depth: -13 })
9390

9491
// Expect
9592
expect(result).to.have.property('children').be.an('array')
@@ -104,14 +101,14 @@ describe('unit:fromFileSystem', () => {
104101
]>([
105102
[
106103
{
107-
depth: 1
104+
depth: 0
108105
},
109106
function assertion(
110107
this: void,
111108
options: Options | null | undefined
112109
): Fn<[Root], undefined> {
113110
ok(options, 'expected `options`')
114-
ok(options.depth, 'expected `options.depth`')
111+
ok(typeof options.depth === 'number', 'expected `options.depth`')
115112

116113
/**
117114
* Read directory result.

src/interfaces/options.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ interface Options {
2424

2525
/**
2626
* Maximum search depth (inclusive).
27+
*
28+
* > 👉 **Note**: A search depth less than `0` will produce an empty tree.
2729
*/
2830
depth?: number | null | undefined
2931

src/util.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function fromFileSystem(
135135
if (
136136
depth === null ||
137137
depth === undefined ||
138-
typeof depth === 'number' && depth > 0
138+
typeof depth === 'number' && depth >= 0
139139
) {
140140
for (const dirent of fs.readdirSync(path, { withFileTypes: true })) {
141141
/**

0 commit comments

Comments
 (0)