Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with objectMode option #210

Merged
merged 3 commits into from Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 24 additions & 10 deletions index.js
Expand Up @@ -35,6 +35,24 @@ const checkCwdOption = options => {
};

const getPathString = fastGlobResult => fastGlobResult.path || fastGlobResult;
const unionFastGlobResults = (results, filter) => {
const seen = new Set();

return results.flat().filter(fastGlobResult => {
if (filter(fastGlobResult)) {
return false;
}

const value = getPathString(fastGlobResult);
if (seen.has(value)) {
return false;
}

seen.add(value);

return true;
});
};

export const generateGlobTasks = (patterns, taskOptions) => {
patterns = arrayUnion([patterns].flat());
Expand Down Expand Up @@ -150,9 +168,9 @@ export const globby = async (patterns, options = {}) => {
};

const [filter, tasks] = await Promise.all([getFilter(options), getTasks()]);
const paths = await Promise.all(tasks.map(task => fastGlob(task.pattern, task.options)));
const results = await Promise.all(tasks.map(task => fastGlob(task.pattern, task.options)));

return arrayUnion(...paths).filter(path_ => !filter(path_));
return unionFastGlobResults(results, filter);
};

export const globbySync = (patterns, options = {}) => {
Expand All @@ -165,13 +183,9 @@ export const globbySync = (patterns, options = {}) => {
}

const filter = getFilterSync(options);
const results = tasks.map(task => fastGlob.sync(task.pattern, task.options));

let matches = [];
for (const task of tasks) {
matches = arrayUnion(matches, fastGlob.sync(task.pattern, task.options));
}

return matches.filter(path_ => !filter(path_));
return unionFastGlobResults(results, filter);
};

export const globbyStream = (patterns, options = {}) => {
Expand All @@ -184,8 +198,8 @@ export const globbyStream = (patterns, options = {}) => {
}

const filter = getFilterSync(options);
const filterStream = new FilterStream(p => !filter(p));
const uniqueStream = new UniqueStream();
const filterStream = new FilterStream(fastGlobResult => !filter(fastGlobResult));
const uniqueStream = new UniqueStream(fastGlobResult => getPathString(fastGlobResult));

return merge2(tasks.map(task => fastGlob.stream(task.pattern, task.options)))
.pipe(filterStream)
Expand Down
10 changes: 7 additions & 3 deletions stream-utils.js
Expand Up @@ -24,15 +24,19 @@ export class FilterStream extends ObjectTransform {
}

export class UniqueStream extends ObjectTransform {
constructor() {
constructor(comparator) {
super();
this._comparator = comparator;
this._pushed = new Set();
}

_transform(data, encoding, callback) {
if (!this._pushed.has(data)) {
const {_comparator: comparator, _pushed: pushed} = this;
const value = comparator(data);

if (!pushed.has(value)) {
this.push(data);
this._pushed.add(data);
pushed.add(value);
}

callback();
Expand Down
20 changes: 20 additions & 0 deletions test.js
Expand Up @@ -425,3 +425,23 @@ test('don\'t throw when specifying a non-existing cwd directory - sync', t => {
t.is(actual.length, 0);
}
});

test('unique when using objectMode option', async t => {
const patterns = ['a.tmp', '*.tmp'];
const options = {cwd, objectMode: true};
const isUnique = result => [...new Set(result)].length === result.length;

const syncResult = globbySync(patterns, options).map(({path}) => path);
t.true(isUnique(syncResult));

const result = await globby(patterns, options);
t.deepEqual(result.map(({path}) => path), syncResult);

// TODO: Use `Array.fromAsync` when Node.js supports it
const streamResult = [];
for await (const {path} of globbyStream(patterns, options)) {
streamResult.push(path);
}

t.deepEqual(streamResult, syncResult);
});