Skip to content

Commit f3642dc

Browse files
authored
Merge pull request #200 from mrmlnc/fix_some_issues
ISSUE-181: fix some issues
2 parents 3be96c9 + b32842d commit f3642dc

File tree

21 files changed

+86
-171
lines changed

21 files changed

+86
-171
lines changed

README.md

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ The current working directory in which to search.
138138

139139
#### deep
140140

141-
* Type: `number|boolean`
142-
* Default: `true`
141+
* Type: `number`
142+
* Default: `Infinity`
143143

144-
The deep option can be set to `true` to traverse the entire directory structure, or it can be set to a *number* to only traverse that many levels deep. The countdown begins with `1`.
144+
The deep option can be set to `Infinity` to traverse the entire directory structure, or it can be set to a *number* to only traverse that many levels deep. The countdown begins with `1`.
145145

146146
For example, you have the following tree:
147147

@@ -180,6 +180,13 @@ An array of glob patterns to exclude matches.
180180

181181
Allow patterns to match filenames starting with a period (files & directories), even if the pattern does not explicitly have a period in that spot.
182182

183+
#### objectMode
184+
185+
* Type: `boolean`
186+
* Default: `false`
187+
188+
Return an [`Entry`](#entry) object instead of filepath.
189+
183190
#### stats
184191

185192
* Type: `boolean`
@@ -291,38 +298,6 @@ Allow glob patterns without slashes to match a file path based on its basename.
291298
Suppress any errors from reader. Works only with Node.js 10.10+.
292299
Can be useful when the directory has entries with a special level of access.
293300

294-
#### transform
295-
296-
* Type: `Function`
297-
* Default: `null`
298-
299-
Allows you to transform a path or `fs.Stats` object before sending to the array.
300-
301-
```js
302-
const fg = require('fast-glob');
303-
304-
const entries1 = fg.sync(['**/*.scss']);
305-
const entries2 = fg.sync(['**/*.scss'], { transform: (entry) => '_' + entry });
306-
307-
console.log(entries1); // ['a.scss', 'b.scss']
308-
console.log(entries2); // ['_a.scss', '_b.scss']
309-
```
310-
311-
If you are using **TypeScript**, you probably want to specify your own type of the returned array.
312-
313-
```ts
314-
import * as fg from 'fast-glob';
315-
316-
interface IMyOwnEntry {
317-
path: string;
318-
}
319-
320-
const entries: IMyOwnEntry[] = fg.sync<IMyOwnEntry>(['*.md'], {
321-
transform: (entry) => typeof entry === 'string' ? { path: entry } : { path: entry.path }
322-
// Will throw compilation error for non-IMyOwnEntry types (boolean, for example)
323-
});
324-
```
325-
326301
#### fs
327302

328303
* Type: `FileSystemAdapter`

src/index.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,50 @@ import ProviderAsync from './providers/async';
33
import Provider from './providers/provider';
44
import ProviderStream from './providers/stream';
55
import ProviderSync from './providers/sync';
6-
import Settings, { Options, TransformFunction } from './settings';
7-
import { EntryItem, Pattern } from './types/index';
6+
import Settings, { Options } from './settings';
7+
import { Entry, EntryItem, Pattern } from './types/index';
88
import * as utils from './utils/index';
99

1010
type Task = taskManager.Task;
1111

12+
type EntryObjectModePredicate = { [P in keyof Pick<Options, 'objectMode'>]-?: true };
13+
type EntryStatsPredicate = { [P in keyof Pick<Options, 'stats'>]-?: true };
14+
type EntryObjectPredicate = EntryObjectModePredicate | EntryStatsPredicate;
15+
16+
function sync(source: Pattern | Pattern[], options: Options & EntryObjectPredicate): Entry[];
17+
function sync(source: Pattern | Pattern[], options?: Options): string[];
1218
function sync(source: Pattern | Pattern[], options?: Options): EntryItem[] {
1319
assertPatternsInput(source);
1420

15-
const works = getWorks<EntryItem[]>(source, ProviderSync, options);
21+
const works = getWorks(source, ProviderSync, options);
1622

1723
return utils.array.flatten(works);
1824
}
1925

26+
function async(source: Pattern | Pattern[], options: Options & EntryObjectPredicate): Promise<Entry[]>;
27+
function async(source: Pattern | Pattern[], options?: Options): Promise<string[]>;
2028
function async(source: Pattern | Pattern[], options?: Options): Promise<EntryItem[]> {
2129
try {
2230
assertPatternsInput(source);
2331
} catch (error) {
2432
return Promise.reject(error);
2533
}
2634

27-
const works = getWorks<Promise<EntryItem[]>>(source, ProviderAsync, options);
35+
const works = getWorks(source, ProviderAsync, options);
2836

2937
return Promise.all(works).then(utils.array.flatten);
3038
}
3139

3240
function stream(source: Pattern | Pattern[], options?: Options): NodeJS.ReadableStream {
3341
assertPatternsInput(source);
3442

35-
const works = getWorks<NodeJS.ReadableStream>(source, ProviderStream, options);
43+
const works = getWorks(source, ProviderStream, options);
3644

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+
*/
3750
return utils.stream.merge(works);
3851
}
3952

@@ -78,7 +91,6 @@ export {
7891

7992
Options,
8093
Settings,
81-
TransformFunction,
8294
Task,
8395
EntryItem
8496
};

src/providers/async.spec.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,6 @@ describe('Providers → ProviderAsync', () => {
7474
assert.deepStrictEqual(actual, expected);
7575
});
7676

77-
it('should call the transform function when it is presented', async () => {
78-
const transform = sinon.stub();
79-
const provider = getProvider({ transform });
80-
const task = tests.task.builder().base('.').positive('*').build();
81-
const entry = tests.entry.builder().path('root/file.txt').file().build();
82-
83-
await getEntries(provider, task, entry);
84-
85-
assert.strictEqual(transform.callCount, 1);
86-
});
87-
8877
it('should throw error', async () => {
8978
const provider = getProvider();
9079
const task = tests.task.builder().base('.').positive('*').build();

src/providers/filters/deep.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ describe('Providers → Filters → Deep', () => {
2828

2929
describe('.getFilter', () => {
3030
describe('options.deep', () => {
31-
it('should return `false` when option is disabled', () => {
32-
const filter = getFilter('.', ['**/*'], [], { deep: false });
31+
it('should return `false` when option has 0 as value', () => {
32+
const filter = getFilter('.', ['**/*'], [], { deep: 0 });
3333
const entry = tests.entry.builder().path('root/directory').directory().build();
3434

3535
const actual = filter(entry);

src/providers/filters/deep.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default class DeepFilter {
5656
}
5757

5858
private _isSkippedByDeep(entryDepth: number): boolean {
59-
return !this._settings.deep || (typeof this._settings.deep === 'number' && entryDepth >= this._settings.deep);
59+
return entryDepth >= this._settings.deep;
6060
}
6161

6262
private _isSkippedByMaxPatternDepth(entryDepth: number, maxPatternDepth: number): boolean {

src/providers/provider.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ describe('Providers → Provider', () => {
103103
nobrace: false,
104104
nocase: false,
105105
noext: false,
106-
noglobstar: false
106+
noglobstar: false,
107+
posix: true
107108
};
108109

109110
const actual = provider.getMicromatchOptions();

src/providers/provider.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ export default abstract class Provider<T> {
4242
protected _getMicromatchOptions(): MicromatchOptions {
4343
return {
4444
dot: this._settings.dot,
45+
matchBase: this._settings.matchBase,
4546
nobrace: !this._settings.braceExpansion,
46-
noglobstar: !this._settings.globstar,
47-
noext: !this._settings.extglob,
4847
nocase: !this._settings.caseSensitiveMatch,
49-
matchBase: this._settings.matchBase
48+
noext: !this._settings.extglob,
49+
noglobstar: !this._settings.globstar,
50+
posix: true
5051
};
5152
}
5253
}

src/providers/stream.spec.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,6 @@ describe('Providers → ProviderStream', () => {
8282
assert.deepStrictEqual(actual, expected);
8383
});
8484

85-
it('should call the transform function when it is presented', async () => {
86-
const transform = sinon.stub();
87-
const provider = getProvider({ transform });
88-
const task = tests.task.builder().base('.').positive('*').build();
89-
const entry = tests.entry.builder().path('root/file.txt').file().build();
90-
91-
await getEntries(provider, task, entry);
92-
93-
assert.strictEqual(transform.callCount, 1);
94-
});
95-
9685
it('should emit error to the transform stream', (done) => {
9786
const provider = getProvider();
9887
const task = tests.task.builder().base('.').positive('*').build();

src/providers/sync.spec.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,5 @@ describe('Providers → ProviderSync', () => {
6262
assert.strictEqual(provider.reader.static.callCount, 1);
6363
assert.deepStrictEqual(actual, expected);
6464
});
65-
66-
it('should call the transform function when it is presented', () => {
67-
const transform = sinon.stub();
68-
const provider = getProvider({ transform });
69-
const task = tests.task.builder().base('.').positive('*').build();
70-
const entry = tests.entry.builder().path('root/file.txt').file().build();
71-
72-
provider.reader.dynamic.returns([entry]);
73-
74-
provider.read(task);
75-
76-
assert.strictEqual(transform.callCount, 1);
77-
});
7865
});
7966
});

src/providers/sync.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default class ProviderSync extends Provider<EntryItem[]> {
1212

1313
const entries = this.api(root, task, options);
1414

15-
return entries.map<EntryItem>(options.transform);
15+
return entries.map(options.transform);
1616
}
1717

1818
public api(root: string, task: Task, options: ReaderOptions): Entry[] {

0 commit comments

Comments
 (0)