Skip to content

Commit e35dec2

Browse files
committed
fix: Fix filter option issue.
1 parent 2b81392 commit e35dec2

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/index.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
import path from 'path';
44
import recursiveReaddirFiles from './';
55

6-
it('Recursive Readdir Files', async () => {
6+
it('ignored test case', async () => {
77
const files = await recursiveReaddirFiles(process.cwd(), {
88
ignored: /\/(node_modules|coverage|\.git)/
99
});
1010
expect(files.length).toBe(10);
1111
expect(Object.keys(files[0])).toEqual(['name', 'path', 'size', 'ext']);
1212
});
1313

14-
it('Recursive Readdir Files', async () => {
14+
it('ignored/exclude/include test case', async () => {
1515
const files = await recursiveReaddirFiles(process.cwd(), {
1616
ignored: /\/(node_modules|coverage|\.git)/,
1717
exclude: /(\.json)$/,
@@ -42,6 +42,7 @@ it('Recursive Readdir Files. try catch', async () => {
4242

4343
it('filter options test case', async () => {
4444
const files = await recursiveReaddirFiles(process.cwd(), {
45+
ignored: /\/(node_modules|\.git|(coverage))/,
4546
filter: (item) => /(.md)/.test(item.path)
4647
});
4748
expect(files.length).toBe(1);

src/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,16 @@ async function getFiles(rootPath: string, options: RecursiveReaddirFilesOptions
5050
name: file,
5151
path: path.join(rootPath, file),
5252
})).filter(item => {
53-
if (filter && typeof filter === 'function') {
54-
return filter(item);
55-
}
5653
if (include && include.test(item.path)) {
5754
return true;
5855
}
5956
if (exclude && exclude.test(item.path)) {
6057
return false
6158
}
62-
if (!ignored || !ignored.test(item.path)) {
63-
return true;
59+
if (ignored) {
60+
return !ignored.test(item.path)
6461
}
65-
return false;
62+
return true;
6663
});
6764

6865
await Promise.all(
@@ -79,7 +76,12 @@ async function getFiles(rootPath: string, options: RecursiveReaddirFilesOptions
7976
}
8077
}),
8178
);
82-
return files;
79+
return files.filter((item) => {
80+
if (filter && typeof filter === 'function') {
81+
return filter(item);
82+
}
83+
return true
84+
});
8385
}
8486

8587

0 commit comments

Comments
 (0)