iter-tools-es
Version:
The iterable toolbox
43 lines (34 loc) • 842 B
JavaScript
const {
asyncIterableCurry
} = require('../../internal/async-iterable.js');
const {
defaultCompareOrder
} = require('../../internal/compare.js');
const {
Heap
} = require('./internal/heap.js');
async function* __asyncTakeSorted(source, n = Infinity, compare = defaultCompareOrder) {
const heap = new Heap(compare);
for await (const value of source) {
heap.push(value);
if (heap.size > n) {
heap.pop();
}
}
while (heap.size) {
yield heap.pop();
}
}
exports.__asyncTakeSorted = __asyncTakeSorted;
const asyncTakeSorted = /*#__PURE__*/asyncIterableCurry(__asyncTakeSorted, {
minArgs: 0,
maxArgs: 2,
validateArgs(args) {
if (typeof args[1] === 'function') {
const temp = args[2];
args[2] = args[1];
args[1] = temp;
}
}
});
exports.asyncTakeSorted = asyncTakeSorted;