Skip to content

Commit

Permalink
Merge pull request #417 from mrmlnc/ISSUE-408_readonly_array_as_input
Browse files Browse the repository at this point in the history
ISSUE-408: Allow to pass readonly array into public methods
  • Loading branch information
mrmlnc committed Aug 4, 2023
2 parents 1bbcfa9 + a8b0ea8 commit ddae0bf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
20 changes: 11 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { ReaderAsync, ReaderStream, ReaderSync } from './readers';
import type { Options as OptionsInternal } from './settings';
import type { Entry as EntryInternal, EntryItem, FileSystemAdapter as FileSystemAdapterInternal, Pattern as PatternInternal } from './types';

type InputPattern = PatternInternal | readonly PatternInternal[];

type EntryObjectModePredicate = { [TKey in keyof Pick<OptionsInternal, 'objectMode'>]-?: true };
type EntryStatsPredicate = { [TKey in keyof Pick<OptionsInternal, 'stats'>]-?: true };
type EntryObjectPredicate = EntryObjectModePredicate | EntryStatsPredicate;

function FastGlob(source: PatternInternal | PatternInternal[], options: EntryObjectPredicate & OptionsInternal): Promise<EntryInternal[]>;
function FastGlob(source: PatternInternal | PatternInternal[], options?: OptionsInternal): Promise<string[]>;
async function FastGlob(source: PatternInternal | PatternInternal[], options?: OptionsInternal): Promise<EntryItem[]> {
function FastGlob(source: InputPattern, options: EntryObjectPredicate & OptionsInternal): Promise<EntryInternal[]>;
function FastGlob(source: InputPattern, options?: OptionsInternal): Promise<string[]>;
async function FastGlob(source: InputPattern, options?: OptionsInternal): Promise<EntryItem[]> {
assertPatternsInput(source);

const settings = new Settings(options);
Expand Down Expand Up @@ -41,9 +43,9 @@ namespace FastGlob {

export const async = FastGlob;

export function sync(source: PatternInternal | PatternInternal[], options: EntryObjectPredicate & OptionsInternal): EntryInternal[];
export function sync(source: PatternInternal | PatternInternal[], options?: OptionsInternal): string[];
export function sync(source: PatternInternal | PatternInternal[], options?: OptionsInternal): EntryItem[] {
export function sync(source: InputPattern, options: EntryObjectPredicate & OptionsInternal): EntryInternal[];
export function sync(source: InputPattern, options?: OptionsInternal): string[];
export function sync(source: InputPattern, options?: OptionsInternal): EntryItem[] {
assertPatternsInput(source);

const settings = new Settings(options);
Expand All @@ -56,7 +58,7 @@ namespace FastGlob {
return utils.array.flatFirstLevel(entries);
}

export function stream(source: PatternInternal | PatternInternal[], options?: OptionsInternal): NodeJS.ReadableStream {
export function stream(source: InputPattern, options?: OptionsInternal): NodeJS.ReadableStream {
assertPatternsInput(source);

const settings = new Settings(options);
Expand All @@ -74,7 +76,7 @@ namespace FastGlob {
return utils.stream.merge(streams);
}

export function generateTasks(source: PatternInternal | PatternInternal[], options?: OptionsInternal): Task[] {
export function generateTasks(source: InputPattern, options?: OptionsInternal): Task[] {
assertPatternsInput(source);

const patterns = ([] as PatternInternal[]).concat(source);
Expand Down Expand Up @@ -132,7 +134,7 @@ namespace FastGlob {
}
}

function getTasks(source: PatternInternal | PatternInternal[], settings: Settings): taskManager.Task[] {
function getTasks(source: InputPattern, settings: Settings): taskManager.Task[] {
const patterns = ([] as PatternInternal[]).concat(source);

return taskManager.generate(patterns, settings);
Expand Down
6 changes: 3 additions & 3 deletions src/managers/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export interface Task {
negative: Pattern[];
}

export function generate(input: Pattern[], settings: Settings): Task[] {
const patterns = processPatterns(input, settings);
const ignore = processPatterns(settings.ignore, settings);
export function generate(input: readonly Pattern[], settings: Settings): Task[] {
const patterns = processPatterns([...input], settings);
const ignore = processPatterns([...settings.ignore], settings);

const positivePatterns = getPositivePatterns(patterns);
const negativePatterns = getNegativePatternsAsPositive(patterns, ignore);
Expand Down
4 changes: 2 additions & 2 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export interface Options {
*
* @default []
*/
ignore?: Pattern[];
ignore?: readonly Pattern[];
/**
* Mark the directory path with the final slash.
*
Expand Down Expand Up @@ -161,7 +161,7 @@ export default class Settings {
public readonly followSymbolicLinks: boolean;
public readonly fs: FileSystemAdapter;
public readonly globstar: boolean;
public readonly ignore: Pattern[];
public readonly ignore: readonly Pattern[];
public readonly markDirectories: boolean;
public readonly objectMode: boolean;
public readonly onlyDirectories: boolean;
Expand Down

0 comments on commit ddae0bf

Please sign in to comment.