-
Notifications
You must be signed in to change notification settings - Fork 1
/
uniqByAsync.js
49 lines (41 loc) · 1.1 KB
/
uniqByAsync.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
40
41
42
43
44
45
46
47
48
49
const mapAsync = require('./mapAsync').default
const { unCurry } = require('./internals/unCurry')
const {
resolveCollectionAndValues,
} = require('./internals/resolveCollectionAndValues')
const uniqFn = ([collection, values]) => {
const { length } = collection
const result = []
const seen = new Set()
for (let index = 0; index < length; ++index) {
const value = values[index]
if (seen.has(value)) {
continue
}
seen.add(value)
result.push(collection[index])
}
return result
}
const uniqByAsync = callback => collection => Promise
.all([
resolveCollectionAndValues(collection),
mapAsync(callback, collection),
])
.then(uniqFn)
/**
* @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 uniqByAsync
* @param {iteratee} callback
* @param {*[]|Promise<*[]>} collection
* @return {Promise<*[]>}
*/
module.exports.default = unCurry(uniqByAsync, 2)