Skip to content

Commit 3fec1e4

Browse files
committed
Renamed filter*
1 parent 73f94dd commit 3fec1e4

File tree

8 files changed

+104
-200
lines changed

8 files changed

+104
-200
lines changed

modern-async.d.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,9 @@ declare module "filterGenerator" {
8181
function filterGenerator<V>(iterable: Iterable<V> | AsyncIterable<V>, iteratee: (value: V, index: number, iterable: Iterable<V> | AsyncIterable<V>) => Promise<boolean> | boolean, queueOrConcurrency?: Queue | number, ordered?: boolean): AsyncIterable<V>;
8282
import Queue from "Queue";
8383
}
84-
declare module "filterLimit" {
85-
export default filterLimit;
86-
function filterLimit<V>(iterable: Iterable<V> | AsyncIterable<V>, iteratee: (value: V, index: number, iterable: Iterable<V> | AsyncIterable<V>) => Promise<boolean> | boolean, queueOrConcurrency: Queue | number): Promise<V[]>;
87-
import Queue from "Queue";
88-
}
8984
declare module "filter" {
9085
export default filter;
91-
function filter<V>(iterable: Iterable<V> | AsyncIterable<V>, iteratee: (value: V, index: number, iterable: Iterable<V> | AsyncIterable<V>) => Promise<boolean> | boolean): Promise<V[]>;
92-
}
93-
declare module "filterSeries" {
94-
export default filterSeries;
95-
function filterSeries<V>(iterable: Iterable<V> | AsyncIterable<V>, iteratee: (value: V, index: number, iterable: Iterable<V> | AsyncIterable<V>) => Promise<boolean> | boolean): Promise<V[]>;
86+
function filter<V>(iterable: Iterable<V> | AsyncIterable<V>, iteratee: (value: V, index: number, iterable: Iterable<V> | AsyncIterable<V>) => Promise<boolean> | boolean, queueOrConcurrency: Queue | number): Promise<V[]>;
9687
}
9788
declare module "findLimit" {
9889
export default findLimit;
@@ -227,8 +218,6 @@ declare module "modern-async" {
227218
export { default as every } from "every";
228219
export { default as filter } from "filter";
229220
export { default as filterGenerator } from "filterGenerator";
230-
export { default as filterLimit } from "filterLimit";
231-
export { default as filterSeries } from "filterSeries";
232221
export { default as find } from "find";
233222
export { default as findIndex } from "findIndex";
234223
export { default as findIndexLimit } from "findIndexLimit";

src/filter.mjs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11

2-
import filterLimit from './filterLimit.mjs'
2+
import Queue from './Queue.mjs'
3+
import toArray from './toArray.mjs'
4+
import filterGenerator from './filterGenerator.mjs'
35

46
/**
57
* Returns an array of all the values in `iterable` which pass an asynchronous truth test.
68
*
7-
* The calls to `iteratee` will perform in parallel. The results will be in the same order than in `iterable`.
9+
* The calls to `iteratee` will be performed in a queue to limit the concurrency of these calls.
10+
* The results will be in the same order than in `iterable`.
811
*
9-
* If any of the calls to `iteratee` throws an exception the returned promise will be rejected.
12+
* If any of the calls to `iteratee` throws an exception the returned promise will be rejected and the remaining
13+
* pending tasks will be cancelled.
1014
*
1115
* @param {Iterable | AsyncIterable} iterable An iterable or async iterable object.
1216
* @param {Function} iteratee A function that will be called with each member of `iterable`. It will receive
1317
* three arguments:
1418
* * `value`: The current value to process
1519
* * `index`: The index in the iterable. Will start from 0.
1620
* * `iterable`: The iterable on which the operation is being performed.
21+
* @param {Queue | number} queueOrConcurrency If a queue is specified it will be used to schedule the calls to
22+
* `iteratee`. If a number is specified it will be used as the concurrency of a Queue that will be created
23+
* implicitly for the same purpose. Defaults to `1`.
1724
* @returns {Promise<any[]>} A promise that will be resolved with an array containing all the values that passed
1825
* the truth test. This promise will be rejected if any of the `iteratee` calls throws an exception.
1926
* @example
2027
* import { filter, sleep } from 'modern-async'
2128
*
2229
* const array = [1, 2, 3]
2330
* const result = await filter(array, async (v) => {
24-
* // these calls will be performed in parallel
31+
* // these calls will be performed in parallel with a maximum of 2
32+
* // concurrent calls
2533
* await sleep(10) // waits 10ms
2634
* return v % 2 === 1
27-
* })
35+
* }, 2)
2836
* console.log(result) // prints [1, 3]
29-
* // total processing time should be ~ 10ms
30-
* })
37+
* // total processing time should be ~ 20ms
3138
*/
32-
async function filter (iterable, iteratee) {
33-
return filterLimit(iterable, iteratee, Number.POSITIVE_INFINITY)
39+
async function filter (iterable, iteratee, queueOrConcurrency = 1) {
40+
return await toArray(filterGenerator(iterable, iteratee, queueOrConcurrency))
3441
}
3542

3643
export default filter

src/filter.test.mjs

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import { range } from 'itertools'
66

77
test('filter base', async () => {
88
const arr = [...range(6)]
9-
const res = await filter(arr, async (x) => x % 2 === 0)
9+
const res = await filter(arr, async (x) => x % 2 === 0, 2)
1010
expect(res).toEqual([0, 2, 4])
1111
})
1212

1313
test('filter no async', async () => {
1414
const arr = [...range(6)]
15-
const res = await filter(arr, (x) => x % 2 === 0)
15+
const res = await filter(arr, (x) => x % 2 === 0, 2)
1616
expect(res).toEqual([0, 2, 4])
1717
})
1818

@@ -27,7 +27,49 @@ test('filter concurrency', async () => {
2727
ds[x].resolve()
2828
await d.promise
2929
return x % 2 === 0
30-
})
30+
}, 2)
31+
await ds[1].promise
32+
expect(called[0]).toBe(1)
33+
expect(called[1]).toBe(1)
34+
expect(called[2]).toBe(0)
35+
expect(called[3]).toBe(0)
36+
expect(called[4]).toBe(0)
37+
expect(called[5]).toBe(0)
38+
d.resolve()
39+
const res = await p
40+
expect(res).toEqual([0, 2, 4])
41+
expect(called[0]).toBe(1)
42+
expect(called[1]).toBe(1)
43+
expect(called[2]).toBe(1)
44+
expect(called[3]).toBe(1)
45+
expect(called[4]).toBe(1)
46+
expect(called[5]).toBe(1)
47+
})
48+
49+
test('filter infinite concurrency base', async () => {
50+
const arr = [...range(6)]
51+
const res = await filter(arr, async (x) => x % 2 === 0, Number.POSITIVE_INFINITY)
52+
expect(res).toEqual([0, 2, 4])
53+
})
54+
55+
test('filter infinite concurrency no async', async () => {
56+
const arr = [...range(6)]
57+
const res = await filter(arr, (x) => x % 2 === 0, Number.POSITIVE_INFINITY)
58+
expect(res).toEqual([0, 2, 4])
59+
})
60+
61+
test('filter infinite concurrency concurrency', async () => {
62+
const arr = [...range(6)]
63+
const called = {}
64+
arr.forEach((v) => { called[v] = 0 })
65+
const d = new Deferred()
66+
const ds = arr.map(() => new Deferred())
67+
const p = filter(arr, async (x) => {
68+
called[x] += 1
69+
ds[x].resolve()
70+
await d.promise
71+
return x % 2 === 0
72+
}, Number.POSITIVE_INFINITY)
3173
await ds[5].promise
3274
expect(called[0]).toBe(1)
3375
expect(called[1]).toBe(1)
@@ -45,3 +87,45 @@ test('filter concurrency', async () => {
4587
expect(called[4]).toBe(1)
4688
expect(called[5]).toBe(1)
4789
})
90+
91+
test('filter concurrency 1 base', async () => {
92+
const arr = [...range(6)]
93+
const res = await filter(arr, async (x) => x % 2 === 0)
94+
expect(res).toEqual([0, 2, 4])
95+
})
96+
97+
test('filter concurrency 1 no async', async () => {
98+
const arr = [...range(6)]
99+
const res = await filter(arr, (x) => x % 2 === 0)
100+
expect(res).toEqual([0, 2, 4])
101+
})
102+
103+
test('filter concurrency 1 concurrency', async () => {
104+
const arr = [...range(6)]
105+
const called = {}
106+
arr.forEach((v) => { called[v] = 0 })
107+
const d = new Deferred()
108+
const ds = arr.map(() => new Deferred())
109+
const p = filter(arr, async (x) => {
110+
called[x] += 1
111+
ds[x].resolve()
112+
await d.promise
113+
return x % 2 === 0
114+
})
115+
await ds[0].promise
116+
expect(called[0]).toBe(1)
117+
expect(called[1]).toBe(0)
118+
expect(called[2]).toBe(0)
119+
expect(called[3]).toBe(0)
120+
expect(called[4]).toBe(0)
121+
expect(called[5]).toBe(0)
122+
d.resolve()
123+
const res = await p
124+
expect(res).toEqual([0, 2, 4])
125+
expect(called[0]).toBe(1)
126+
expect(called[1]).toBe(1)
127+
expect(called[2]).toBe(1)
128+
expect(called[3]).toBe(1)
129+
expect(called[4]).toBe(1)
130+
expect(called[5]).toBe(1)
131+
})

src/filterLimit.mjs

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/filterLimit.test.mjs

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/filterSeries.mjs

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/filterSeries.test.mjs

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/modern-async.mjs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ export { default as Delayer } from './Delayer.mjs'
1010
export { default as every } from './every.mjs'
1111
export { default as filter } from './filter.mjs'
1212
export { default as filterGenerator } from './filterGenerator.mjs'
13-
export { default as filterLimit } from './filterLimit.mjs'
14-
export { default as filterSeries } from './filterSeries.mjs'
1513
export { default as find } from './find.mjs'
1614
export { default as findIndex } from './findIndex.mjs'
1715
export { default as findIndexLimit } from './findIndexLimit.mjs'

0 commit comments

Comments
 (0)