Skip to content

Commit ccde25e

Browse files
committed
refactor: set async method as default method
1 parent 87ed886 commit ccde25e

File tree

4 files changed

+35
-46
lines changed

4 files changed

+35
-46
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ $ npm install --save fast-glob
3232
const fg = require('fast-glob');
3333

3434
fg(['src/**/*.js', '!src/**/*.spec.js']).then((entries) => console.log(entries));
35-
fg.async(['src/**/*.js', '!src/**/*.spec.js']).then((entries) => console.log(entries));
3635
```
3736

3837
#### Synchronous
@@ -61,7 +60,6 @@ stream.once('end', () => console.log(entries));
6160
## API
6261

6362
### fg(patterns, [options])
64-
### fg.async(patterns, [options])
6563

6664
Returns a `Promise` with an array of matching [entries](#entry).
6765

src/benchmark/suites/async/fast-glob-current.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const settings = new Settings({
1111

1212
const timeStart = utils.timeStart();
1313

14-
glob.async(process.env.BENCHMARK_PATTERN as string, settings)
14+
glob(process.env.BENCHMARK_PATTERN as string, settings)
1515
.then((matches) => {
1616
const memory = utils.getMemory();
1717
const time = utils.timeEnd(timeStart);

src/index.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('Package', () => {
5151
it('should throw an error when input values can not pass validation', async () => {
5252
try {
5353
/* tslint:disable-next-line no-any */
54-
await pkg.async(null as any);
54+
await pkg(null as any);
5555
throw new Error('An unexpected error was found.');
5656
} catch (error) {
5757
assert.strictEqual((error as Error).toString(), 'TypeError: Patterns must be a string or an array of strings');
@@ -69,7 +69,7 @@ describe('Package', () => {
6969
'fixtures/second/nested/file.md'
7070
];
7171

72-
const actual = await pkg.async(['fixtures/**/*.md']);
72+
const actual = await pkg(['fixtures/**/*.md']);
7373

7474
actual.sort();
7575

@@ -86,7 +86,7 @@ describe('Package', () => {
8686
'fixtures/second/nested/file.md'
8787
];
8888

89-
const actual = await pkg.async(['fixtures/first/**/*.md', 'fixtures/second/**/*.md']);
89+
const actual = await pkg(['fixtures/first/**/*.md', 'fixtures/second/**/*.md']);
9090

9191
actual.sort();
9292

src/index.ts

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,9 @@ type EntryObjectModePredicate = { [P in keyof Pick<Options, 'objectMode'>]-?: tr
1313
type EntryStatsPredicate = { [P in keyof Pick<Options, 'stats'>]-?: true };
1414
type EntryObjectPredicate = EntryObjectModePredicate | EntryStatsPredicate;
1515

16-
function sync(source: Pattern | Pattern[], options: Options & EntryObjectPredicate): Entry[];
17-
function sync(source: Pattern | Pattern[], options?: Options): string[];
18-
function sync(source: Pattern | Pattern[], options?: Options): EntryItem[] {
19-
assertPatternsInput(source);
20-
21-
const works = getWorks(source, ProviderSync, options);
22-
23-
return utils.array.flatten(works);
24-
}
25-
26-
function async(source: Pattern | Pattern[], options: Options & EntryObjectPredicate): Promise<Entry[]>;
27-
function async(source: Pattern | Pattern[], options?: Options): Promise<string[]>;
28-
function async(source: Pattern | Pattern[], options?: Options): Promise<EntryItem[]> {
16+
function FastGlob(source: Pattern | Pattern[], options: Options & EntryObjectPredicate): Promise<Entry[]>;
17+
function FastGlob(source: Pattern | Pattern[], options?: Options): Promise<string[]>;
18+
function FastGlob(source: Pattern | Pattern[], options?: Options): Promise<EntryItem[]> {
2919
try {
3020
assertPatternsInput(source);
3121
} catch (error) {
@@ -37,26 +27,38 @@ function async(source: Pattern | Pattern[], options?: Options): Promise<EntryIte
3727
return Promise.all(works).then(utils.array.flatten);
3828
}
3929

40-
function stream(source: Pattern | Pattern[], options?: Options): NodeJS.ReadableStream {
41-
assertPatternsInput(source);
30+
namespace FastGlob {
31+
export function sync(source: Pattern | Pattern[], options: Options & EntryObjectPredicate): Entry[];
32+
export function sync(source: Pattern | Pattern[], options?: Options): string[];
33+
export function sync(source: Pattern | Pattern[], options?: Options): EntryItem[] {
34+
assertPatternsInput(source);
4235

43-
const works = getWorks(source, ProviderStream, options);
36+
const works = getWorks(source, ProviderSync, options);
4437

45-
/**
46-
* The stream returned by the provider cannot work with an asynchronous iterator.
47-
* To support asynchronous iterators, regardless of the number of tasks, we always multiplex streams.
48-
* This affects performance (+25%). I don't see best solution right now.
49-
*/
50-
return utils.stream.merge(works);
51-
}
38+
return utils.array.flatten(works);
39+
}
40+
41+
export function stream(source: Pattern | Pattern[], options?: Options): NodeJS.ReadableStream {
42+
assertPatternsInput(source);
5243

53-
function generateTasks(source: Pattern | Pattern[], options?: Options): Task[] {
54-
assertPatternsInput(source);
44+
const works = getWorks(source, ProviderStream, options);
5545

56-
const patterns = ([] as Pattern[]).concat(source);
57-
const settings = new Settings(options);
46+
/**
47+
* The stream returned by the provider cannot work with an asynchronous iterator.
48+
* To support asynchronous iterators, regardless of the number of tasks, we always multiplex streams.
49+
* This affects performance (+25%). I don't see best solution right now.
50+
*/
51+
return utils.stream.merge(works);
52+
}
5853

59-
return taskManager.generate(patterns, settings);
54+
export function generateTasks(source: Pattern | Pattern[], options?: Options): Task[] {
55+
assertPatternsInput(source);
56+
57+
const patterns = ([] as Pattern[]).concat(source);
58+
const settings = new Settings(options);
59+
60+
return taskManager.generate(patterns, settings);
61+
}
6062
}
6163

6264
function getWorks<T>(source: Pattern | Pattern[], _Provider: new (settings: Settings) => Provider<T>, options?: Options): T[] {
@@ -82,15 +84,4 @@ function isString(source: unknown): source is string {
8284
return typeof source === 'string';
8385
}
8486

85-
export default async;
86-
export {
87-
async,
88-
sync,
89-
stream,
90-
generateTasks,
91-
92-
Options,
93-
Settings,
94-
Task,
95-
EntryItem
96-
};
87+
export = FastGlob;

0 commit comments

Comments
 (0)