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

settings: Add uniqueBy function #374

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/providers/filters/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Entry, EntryFilterFunction, MicromatchOptions, Pattern, PatternRe } fro
import * as utils from '../../utils';

export default class EntryFilter {
public readonly index: Map<string, undefined> = new Map();
public readonly index: Map<unknown, undefined> = new Map();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also curious if we could have a shared/cached index in case we want to create multiple separate fast-glob instances (such as would be needed to support ordered glob negation)


constructor(private readonly _settings: Settings, private readonly _micromatchOptions: MicromatchOptions) {}

Expand Down Expand Up @@ -40,7 +40,7 @@ export default class EntryFilter {
}

private _isDuplicateEntry(entry: Entry): boolean {
return this.index.has(entry.path);
return this.index.has(this._settings.uniqueBy(entry));
}

private _createIndexRecord(entry: Entry): void {
Expand Down
9 changes: 8 additions & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as os from 'os';

import { FileSystemAdapter, Pattern } from './types';
import { Entry, FileSystemAdapter, Pattern } from './types';

/**
* The `os.cpus` method can return zero. We expect the number of cores to be greater than zero.
Expand Down Expand Up @@ -152,6 +152,12 @@ export type Options = {
* @default true
*/
unique?: boolean;
/**
* Overrides the uniqueness value when `unique` is set to `true`.
*
* @default (entry) => entry.path
*/
uniqueBy?: (entry: Entry) => unknown;
};

export default class Settings {
Expand All @@ -176,6 +182,7 @@ export default class Settings {
public readonly suppressErrors: boolean = this._getValue(this._options.suppressErrors, false);
public readonly throwErrorOnBrokenSymbolicLink: boolean = this._getValue(this._options.throwErrorOnBrokenSymbolicLink, false);
public readonly unique: boolean = this._getValue(this._options.unique, true);
public readonly uniqueBy: (entry: Entry) => unknown = this._getValue(this._options.uniqueBy, (entry: Entry) => entry.path);

constructor(private readonly _options: Options = {}) {
if (this.onlyDirectories) {
Expand Down