Skip to content

Commit

Permalink
Fix bug with objectMode option (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 17, 2022
1 parent 2c9cc27 commit 1852fc5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
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);
});

0 comments on commit 1852fc5

Please sign in to comment.