-
Notifications
You must be signed in to change notification settings - Fork 1
/
maxByAsync.js
39 lines (33 loc) · 911 Bytes
/
maxByAsync.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
34
35
36
37
38
39
const mapAsync = require('./mapAsync').default
const { unCurry } = require('./internals/unCurry')
const maxByFn = ([collection, values]) => {
let maxIndex = 0
for (let index = 1; index < collection.length; ++index) {
if (values[index] > values[maxIndex]) {
maxIndex = index
}
}
return collection[maxIndex]
}
const maxByAsync = callback => collection => Promise
.all([
collection,
mapAsync(callback, collection),
])
.then(maxByFn)
/**
* @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<*>}
*/
/**
* @async
* @function maxByAsync
* @param {iteratee} callback
* @param {*[]|Promise<*[]>} collection
* @return {Promise<*>}
*/
module.exports.default = unCurry(maxByAsync, 2)