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: fileFilter not receiving all params #426

Merged
merged 1 commit into from
Nov 13, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/shared/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export type Batch = {
additionalInfo: ?string,
};

export type FileFilterMethod = (File | string) => boolean | Promise<boolean>;
export type FileFilterMethod = (File | string, number, FileLike[] | string[]) => boolean | Promise<boolean>;

export type UploadOptions = {|
//automatically upload files when they are added (default: true)
Expand Down
19 changes: 13 additions & 6 deletions packages/core/uploader/src/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,26 @@ let bCounter = 0;
const processFiles = (batchId, files: UploadInfo, isPending: boolean, fileFilter: ?FileFilterMethod): Promise<BatchItem[]> => {
const filterFn = fileFilter || DEFAULT_FILTER;

//we need a simple array of (file, url) to pass to filter fn if its provided (files can be recycled batch items)
const all: any[] = fileFilter ? Array.prototype
//$FlowExpectedError[method-unbinding] flow 0.153 !!!
.map.call(files, (f) => getIsBatchItem(f) ? (f.file || f.url) : f) :
//in case no filter fn, no need to map it
[];

return Promise.all(Array.prototype
//$FlowExpectedError[method-unbinding] flow 0.153 !!!
.map.call(files, (f) => {
const isBatchItem = getIsBatchItem(f);
const filterResult = filterFn(isBatchItem ? (f.file || f.url) : f);
.map.call(files, (f, index) => {
const filterResult = filterFn(all[index], index, all);

return isPromise(filterResult) ?
filterResult.then((result) => !!result && f) :
(!!filterResult && f);
}))
.then((filtered) => filtered
.filter(Boolean)
.map((f) => createBatchItem(f, batchId, isPending)));
.then((filtered) =>
filtered
.filter(Boolean)
.map((f) => createBatchItem(f, batchId, isPending)));
};

const createBatch = (files: UploadInfo | UploadInfo[], uploaderId: string, options: UploaderCreateOptions): Promise<Batch> => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/uploader/src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { devFreeze } from "@rpldy/shared";

export const DEFAULT_PARAM_NAME = "file";

export const DEFAULT_FILTER = (input: any): boolean => true;
export const DEFAULT_FILTER = (input: any, i: number, arr: any[]): boolean => true;

export const DEFAULT_OPTIONS: Object = devFreeze({
autoUpload: true,
Expand Down
18 changes: 16 additions & 2 deletions packages/core/uploader/src/tests/batch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,21 @@ describe("Batch tests", () => {

expect(batch.items).toHaveLength(2);

expect(fileFilter).toHaveBeenNthCalledWith(1, files[0].file);
expect(fileFilter).toHaveBeenNthCalledWith(2, files[1].url);
expect(fileFilter).toHaveBeenNthCalledWith(1, files[0].file, 0, [files[0].file, files[1].url]);
expect(fileFilter).toHaveBeenNthCalledWith(2, files[1].url, 1, [files[0].file, files[1].url]);
});

it("should filter to only the first two items", async () => {
const files = [ {name: "test"}, "123", "456"];

createBatchItem
.mockReturnValueOnce("item1")
.mockReturnValueOnce("item2");

const batch = await createBatch(files, "u1", {
fileFilter: (_, index) => index < 2
});

expect(batch.items).toHaveLength(2);
});
});