iter-tools-es
Version:
The iterable toolbox
67 lines (54 loc) • 1.44 kB
JavaScript
const {
asyncIterableCurry
} = require('../../internal/async-iterable.js');
const {
AsyncBisector
} = require('../../internal/async-bisector.js');
const {
__asyncToArray
} = require('../$to-array/async-to-array.js');
const {
__asyncPeekerate
} = require('../$peekerate/async-peekerate.js');
async function* asyncIndexStrategy(split, {
at
}, source) {
const _source = at < 0 ? await __asyncToArray(source) : source;
const idx = at < 0 ? _source.length + at : at;
const peekr = await __asyncPeekerate(_source);
try {
while (!peekr.done) {
if (peekr.index === idx) yield split;
yield peekr.value;
await peekr.advance();
}
} finally {
peekr.return();
}
}
exports.asyncIndexStrategy = asyncIndexStrategy;
async function* asyncConditionStrategy(split, {
at: predicate
}, source) {
let i = 0;
let splat = false;
for await (const value of source) {
if (!splat && (await predicate(value, i++))) {
yield split;
splat = true;
}
yield value;
}
}
exports.asyncConditionStrategy = asyncConditionStrategy;
function __asyncBisect(source, at) {
const strategy = typeof at === 'number' ? asyncIndexStrategy : asyncConditionStrategy;
return new AsyncBisector(source, strategy, {
at
});
}
exports.__asyncBisect = __asyncBisect;
const asyncBisect = /*#__PURE__*/asyncIterableCurry(__asyncBisect, {
forceSync: true
});
exports.asyncBisect = asyncBisect;