-
Notifications
You must be signed in to change notification settings - Fork 1
/
filterAsync.js
33 lines (29 loc) · 906 Bytes
/
filterAsync.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const mapAsync = require('./mapAsync').default
const { unCurry } = require('./internals/unCurry')
const {
resolveCollectionAndValues,
} = require('./internals/resolveCollectionAndValues')
const filterFn = ([collection, filterResult]) => collection
.filter((_, i) => filterResult[i])
const filterAsync = callback => collection => Promise
.all([
resolveCollectionAndValues(collection),
mapAsync(callback, collection),
])
.then(filterFn)
/**
* @callback iteratee
* @async
* @param {*} element - The current element in the collection.
* @param {number} [index] - The index of the current element in the collection.
* @param {*[]} [collection] - The collection.
* @return {Promise<boolean>}
*/
/**
* @async
* @function filterAsync
* @param {iteratee} callback
* @param {*[]|Promise<*[]>} collection
* @return {Promise<*[]>}
*/
module.exports.default = unCurry(filterAsync, 2)