Skip to content

Commit

Permalink
Add pagination.stackAllItems option (#1214)
Browse files Browse the repository at this point in the history
  • Loading branch information
PopGoesTheWza authored and sindresorhus committed May 2, 2020
1 parent c127f5b commit c1208d1
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,15 @@ The maximum amount of request that should be triggered. [Retries on failure](#re

For example, it can be helpful during development to avoid an infinite number of requests.

###### pagination.stackAllItems

Type: `boolean`\
Default: `true`

Defines how the parameter `allItems` in [pagination.paginate](#pagination.paginate), [pagination.filter](#pagination.filter) and [pagination.shouldContinue](#pagination.shouldContinue) is managed. When set to `false`, the parameter `allItems` is always an empty array.

This option can be helpful to save on memory usage when working with a large dataset.

##### localAddress

Type: `string`
Expand Down
1 change: 1 addition & 0 deletions source/as-promise/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export interface PaginationOptions<T, R> {
shouldContinue?: (item: T, allItems: T[], currentItems: T[]) => boolean;
countLimit?: number;
requestLimit?: number;
stackAllItems?: boolean;
};
}

Expand Down
8 changes: 6 additions & 2 deletions source/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ const create = (defaults: InstanceDefaults): Got => {
}

const all: T[] = [];
let {countLimit} = pagination;

let numberOfRequests = 0;
while (numberOfRequests < pagination.requestLimit) {
Expand All @@ -224,10 +225,13 @@ const create = (defaults: InstanceDefaults): Got => {

yield item;

all.push(item as T);
if (pagination.stackAllItems) {
all.push(item as T);
}

current.push(item as T);

if (all.length === pagination.countLimit) {
if (--countLimit <= 0) {
return;
}
}
Expand Down
3 changes: 2 additions & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ const defaults: InstanceDefaults = {
filter: () => true,
shouldContinue: () => true,
countLimit: Infinity,
requestLimit: 10000
requestLimit: 10000,
stackAllItems: true
}
},
handlers: [defaultHandler],
Expand Down
56 changes: 56 additions & 0 deletions test/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,59 @@ test('`requestLimit` works', withServer, async (t, server, got) => {

t.deepEqual(results, [1]);
});

test('`stackAllItems` set to true', withServer, async (t, server, got) => {
attachHandler(server, 3);

let itemCount = 0;
const result = await got.paginate.all<number>({
pagination: {
stackAllItems: true,
filter: (_item, allItems, _currentItems) => {
t.is(allItems.length, itemCount);

return true;
},
shouldContinue: (_item, allItems, _currentItems) => {
t.is(allItems.length, itemCount);

return true;
},
paginate: (response, allItems, currentItems) => {
itemCount += 1;
t.is(allItems.length, itemCount);

return got.defaults.options.pagination!.paginate(response, allItems, currentItems);
}
}
});

t.deepEqual(result, [1, 2, 3]);
});

test('`stackAllItems` set to false', withServer, async (t, server, got) => {
attachHandler(server, 3);

const result = await got.paginate.all<number>({
pagination: {
stackAllItems: false,
filter: (_item, allItems, _currentItems) => {
t.is(allItems.length, 0);

return true;
},
shouldContinue: (_item, allItems, _currentItems) => {
t.is(allItems.length, 0);

return true;
},
paginate: (response, allItems, currentItems) => {
t.is(allItems.length, 0);

return got.defaults.options.pagination!.paginate(response, allItems, currentItems);
}
}
});

t.deepEqual(result, [1, 2, 3]);
});

0 comments on commit c1208d1

Please sign in to comment.