Skip to content

Commit d76231b

Browse files
authored
Make .map() method accept an iterable, not just array (#98)
1 parent 9da5934 commit d76231b

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

index.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ export type LimitFunction = {
2424
clearQueue: () => void;
2525

2626
/**
27-
Process an array of inputs with limited concurrency.
27+
Process an iterable of inputs with limited concurrency.
2828
2929
The mapper function receives the item value and its index.
3030
31-
@param array - An array containing an argument for the given function.
31+
@param iterable - An iterable containing an argument for the given function.
3232
@param mapperFunction - Promise-returning/async function.
3333
@returns A Promise that returns an array of results.
3434
*/
3535
map: <Input, ReturnType> (
36-
array: readonly Input[],
36+
iterable: Iterable<Input>,
3737
mapperFunction: (input: Input, index: number) => PromiseLike<ReturnType> | ReturnType
3838
) => Promise<ReturnType[]>;
3939

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ export default function pLimit(concurrency) {
8282
},
8383
},
8484
map: {
85-
async value(array, function_) {
86-
const promises = array.map((value, index) => this(function_, value, index));
85+
async value(iterable, function_) {
86+
const promises = Array.from(iterable, (value, index) => this(function_, value, index));
8787
return Promise.all(promises);
8888
},
8989
},

index.test-d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ const lf = limitFunction(async (_a: string) => 'ok', {concurrency: 1});
2424
expectType<Promise<string>>(lf('input'));
2525

2626
expectError(limitFunction((_a: string) => 'x', {concurrency: 1}));
27+
28+
// LimitFunction.map accepts iterables
29+
expectType<Promise<string[]>>(limit.map(new Set(['a', 'b', 'c']), async x => x + x));
30+
expectType<Promise<number[]>>(limit.map([1, 2, 3].values(), async x => x * 2));

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ Any arguments to pass through to `fn`.
5757

5858
Support for passing arguments on to the `fn` is provided in order to be able to avoid creating unnecessary closures. You probably don't need this optimization unless you're pushing a *lot* of functions.
5959

60-
### limit.map(array, mapperFunction)
60+
### limit.map(iterable, mapperFunction)
6161

62-
Process an array of inputs with limited concurrency.
62+
Process an iterable of inputs with limited concurrency.
6363

6464
The mapper function receives the item value and its index.
6565

66-
Returns a promise equivalent to `Promise.all(array.map((item, index) => limit(mapperFunction, item, index)))`.
66+
Returns a promise equivalent to `Promise.all(Array.from(iterable, (item, index) => limit(mapperFunction, item, index)))`.
6767

6868
This is a convenience function for processing inputs that arrive in batches. For more complex use cases, see [p-map](https://github.com/sindresorhus/p-map).
6969

test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,24 @@ test('map passes index and preserves order with concurrency', async t => {
190190
t.deepEqual(results, [10, 11, 12, 13, 14]);
191191
});
192192

193+
test('map accepts an iterable (set)', async t => {
194+
const limit = pLimit(2);
195+
const inputs = new Set([1, 2, 3, 4]);
196+
197+
const results = await limit.map(inputs, input => input * 2); // eslint-disable-line unicorn/no-array-method-this-argument
198+
199+
t.deepEqual(results, [2, 4, 6, 8]);
200+
});
201+
202+
test('map accepts an iterable (array iterator)', async t => {
203+
const limit = pLimit(2);
204+
const inputs = [1, 2, 3, 4].values();
205+
206+
const results = await limit.map(inputs, input => input * 2); // eslint-disable-line unicorn/no-array-method-this-argument
207+
208+
t.deepEqual(results, [2, 4, 6, 8]);
209+
});
210+
193211
test('throws on invalid concurrency argument', t => {
194212
t.throws(() => {
195213
pLimit(0);

0 commit comments

Comments
 (0)