Skip to content

Commit

Permalink
feat: filter out .dotFiles by default
Browse files Browse the repository at this point in the history
* chore: move test files in their correct folder

* feat: filter out .dotFiles by default

* chore: add utility maybeFilter

* test: add a .dotFile to add more tests
  • Loading branch information
lpatiny authored Oct 27, 2022
1 parent d1a8945 commit 3f4622f
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 6 deletions.
Empty file added src/__tests__/data/.dotFile
Empty file.
24 changes: 24 additions & 0 deletions src/__tests__/fileCollectionFromPath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,30 @@ describe('fileCollectionFromPath', () => {
]);
});

it('simple data without ignore dotFiles', async () => {
const fileCollection = await fileCollectionFromPath(
join(__dirname, 'data'),
{ ignoreDotFiles: false },
);

expect(
Array.from(
fileCollection.files.map((a) => `${a.relativePath} - ${a.name}`),
),
).toStrictEqual([
'data/.dotFile - .dotFile',
'data/dir1/a.txt - a.txt',
'data/dir1/b.txt - b.txt',
'data/dir1/dir3/e.txt - e.txt',
'data/dir1/dir3/f.txt - f.txt',
'data/dir2/c.txt - c.txt',
'data/dir2/d.txt - d.txt',
'data/dir3/a.MpT - a.MpT',
'data/dir3/a.mpr - a.mpr',
'data/dir3/a.mps - a.mps',
]);
});

it('data with zip', async () => {
const fileCollection = await fileCollectionFromPath(
join(__dirname, 'dataUnzip'),
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/fileCollectionFromWebservice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const server = setupServer(
const data = await readFile(pathname);
return res(ctx.body(data));
} else {
throw new Error(`uhknown path: ${pathname}`);
throw new Error(`unknown path: ${pathname}`);
}
}),
);
Expand All @@ -38,7 +38,7 @@ afterAll(() => {

test('displays the list of recent posts', async () => {
const url = 'http://localhost/data';
const fileCollection = (await fileCollectionFromWebservice(url)).files;
const fileCollection = (await fileCollectionFromWebservice(url, {})).files;
expect(fileCollection).toHaveLength(15);
const first = await fileCollection[0].text();
expect(first).toBe('c');
Expand Down
4 changes: 3 additions & 1 deletion src/fileCollectionFromFileList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ExpandOptions } from './ExpandOptions';
import { FileCollection } from './FileCollection';
import { FileCollectionItem } from './FileCollectionItem';
import { maybeExpand } from './utilities/maybeExpand';
import { FilterOptions, maybeFilter } from './utilities/maybeFilter';

/**
* Generate a FileCollection from a FileList
Expand All @@ -15,7 +16,7 @@ import { maybeExpand } from './utilities/maybeExpand';
*/
export async function fileCollectionFromFileList(
fileList: FileList,
options: ExpandOptions = {},
options: ExpandOptions & FilterOptions = {},
): Promise<FileCollection> {
let fileCollectionItems: FileCollectionItem[] = [];

Expand All @@ -32,6 +33,7 @@ export async function fileCollectionFromFileList(
}

fileCollectionItems = await maybeExpand(fileCollectionItems, options);
fileCollectionItems = await maybeFilter(fileCollectionItems, options);

return new FileCollection(fileCollectionItems);
}
4 changes: 3 additions & 1 deletion src/fileCollectionFromFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ExpandOptions } from './ExpandOptions';
import { FileCollection } from './FileCollection';
import { FileCollectionItem } from './FileCollectionItem';
import { maybeExpand } from './utilities/maybeExpand';
import { FilterOptions, maybeFilter } from './utilities/maybeFilter';

/**
* Generate a FileCollection from an array of File
Expand All @@ -13,7 +14,7 @@ import { maybeExpand } from './utilities/maybeExpand';
*/
export async function fileCollectionFromFiles(
files: File[],
options: ExpandOptions = {},
options: ExpandOptions & FilterOptions = {},
): Promise<FileCollection> {
let fileCollectionItems: FileCollectionItem[] = [];

Expand All @@ -31,6 +32,7 @@ export async function fileCollectionFromFiles(
}

fileCollectionItems = await maybeExpand(fileCollectionItems, options);
fileCollectionItems = await maybeFilter(fileCollectionItems, options);

return new FileCollection(fileCollectionItems);
}
4 changes: 3 additions & 1 deletion src/fileCollectionFromPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ExpandOptions } from './ExpandOptions';
import { FileCollection } from './FileCollection';
import { FileCollectionItem } from './FileCollectionItem';
import { maybeExpand } from './utilities/maybeExpand';
import { FilterOptions, maybeFilter } from './utilities/maybeFilter';

/**
* Generate a FileCollection from a directory path
Expand All @@ -16,13 +17,14 @@ import { maybeExpand } from './utilities/maybeExpand';
*/
export async function fileCollectionFromPath(
path: string,
options: ExpandOptions = {},
options: ExpandOptions & FilterOptions = {},
): Promise<FileCollection> {
path = resolve(path);
const base = basename(path);
let fileCollectionItems: FileCollectionItem[] = [];
await appendFiles(fileCollectionItems, path, base);
fileCollectionItems = await maybeExpand(fileCollectionItems, options);
fileCollectionItems = await maybeFilter(fileCollectionItems, options);
return new FileCollection(fileCollectionItems);
}

Expand Down
4 changes: 3 additions & 1 deletion src/fileCollectionFromWebservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ExpandOptions } from './ExpandOptions';
import { FileCollection } from './FileCollection';
import { FileCollectionItem } from './FileCollectionItem';
import { maybeExpand } from './utilities/maybeExpand';
import { FilterOptions, maybeFilter } from './utilities/maybeFilter';

/**
* Creates a FileCollection from a webservice. This webservice should return an array of objects containing the properties:
Expand All @@ -18,7 +19,7 @@ import { maybeExpand } from './utilities/maybeExpand';
*/
export async function fileCollectionFromWebservice(
url: string | URL,
options: ExpandOptions = {},
options: ExpandOptions & FilterOptions = {},
): Promise<FileCollection> {
const response = await fetch(url.toString());
const baseURL = url;
Expand Down Expand Up @@ -57,5 +58,6 @@ export async function fileCollectionFromWebservice(
});
}
fileCollectionItems = await maybeExpand(fileCollectionItems, options);
fileCollectionItems = await maybeFilter(fileCollectionItems, options);
return new FileCollection(fileCollectionItems);
}
Binary file added src/utilities/__tests__/data/hidden/data.zip
Binary file not shown.
17 changes: 17 additions & 0 deletions src/utilities/__tests__/maybeFilter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { join } from 'path';

import { fileCollectionFromPath } from '../../fileCollectionFromPath';
import { maybeFilter } from '../maybeFilter';

test('maybeFilter', async () => {
const files = (
await fileCollectionFromPath(join(__dirname, 'data/hidden'), {
ignoreDotFiles: false,
})
).files;
const filtered = await maybeFilter(files);
expect(filtered).toHaveLength(4);

const notFiltered = await maybeFilter(files, { ignoreDotFiles: false });
expect(notFiltered).toHaveLength(9);
});
28 changes: 28 additions & 0 deletions src/utilities/maybeFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { FileCollectionItem } from '../FileCollectionItem';

export type FilterOptions = {
/**
* Should we ignored files starting with dot
* @default true
*/
ignoreDotFiles?: boolean;
};

/**
* Utility function that allows to expand gzip and zip files without really expanding them
* @param fileCollection
* @param options
* @returns
*/
export async function maybeFilter(
fileCollectionItems: FileCollectionItem[],
options: FilterOptions = {},
): Promise<FileCollectionItem[]> {
const { ignoreDotFiles = true } = options;
if (ignoreDotFiles) {
fileCollectionItems = fileCollectionItems.filter(
(item) => !item.name.startsWith('.'),
);
}
return fileCollectionItems;
}

0 comments on commit 3f4622f

Please sign in to comment.