Skip to content

Commit

Permalink
refactor(util): allow search depth to be 0
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Jan 19, 2025
1 parent 2666be1 commit e1e2279
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
5 changes: 4 additions & 1 deletion .commitlintrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import { scopes } from '@flex-development/commitlint-config'
const config: UserConfig = {
extends: ['@flex-development'],
rules: {
'scope-enum': [RuleConfigSeverity.Error, 'always', scopes(['chore'])]
'scope-enum': [RuleConfigSeverity.Error, 'always', scopes([
'chore',
'util'
])]
}
}

Expand Down
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,23 @@ Options for creating a file system tree (TypeScript interface).

#### Properties

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

### `Dirent`

Expand Down
2 changes: 1 addition & 1 deletion __tests__/utils/readdir.mts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function readdir(
if (
depth === null ||
depth === undefined ||
typeof depth === 'number' && depth > 0
typeof depth === 'number' && depth >= 0
) {
dir = toPath(dir)

Expand Down
11 changes: 4 additions & 7 deletions src/__tests__/util.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,9 @@ describe('unit:fromFileSystem', () => {
}
})

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

// Expect
expect(result).to.have.property('children').be.an('array')
Expand All @@ -104,14 +101,14 @@ describe('unit:fromFileSystem', () => {
]>([
[
{
depth: 1
depth: 0
},
function assertion(
this: void,
options: Options | null | undefined
): Fn<[Root], undefined> {
ok(options, 'expected `options`')
ok(options.depth, 'expected `options.depth`')
ok(typeof options.depth === 'number', 'expected `options.depth`')

/**
* Read directory result.
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/options.mts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ interface Options {

/**
* Maximum search depth (inclusive).
*
* > 👉 **Note**: A search depth less than `0` will produce an empty tree.
*/
depth?: number | null | undefined

Expand Down
2 changes: 1 addition & 1 deletion src/util.mts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function fromFileSystem(
if (
depth === null ||
depth === undefined ||
typeof depth === 'number' && depth > 0
typeof depth === 'number' && depth >= 0
) {
for (const dirent of fs.readdirSync(path, { withFileTypes: true })) {
/**
Expand Down

0 comments on commit e1e2279

Please sign in to comment.