-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
36 lines (27 loc) · 775 Bytes
/
Copy pathindex.js
File metadata and controls
36 lines (27 loc) · 775 Bytes
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
// this algorithm assumes that `.toHexString()` is the expensive operation.
// i don't know if it actually is.
module.exports = function (docs, oids) {
// we precalculate the array lengths
// because we expect to use them all
var docids = docs.map(toID)
var indexes = docs.map(toIndex)
var out = []
var index
for (var i = 0; i < oids.length; i++) {
index = docids.indexOf(oids[i].toHexString())
if (!~index) continue
out.push(docs[indexes[index]])
// never lookup this doc again
docids.splice(index, 1)
indexes.splice(index, 1)
// return early if all docs are accounted for
if (!docids.length) return out
}
return out
}
function toID(x) {
return (x._id || x).toHexString()
}
function toIndex(a, i) {
return i
}