@idealic/poker-engine
Version:
Professional poker game engine and hand evaluator with built-in iterator utilities
50 lines • 1.61 kB
JavaScript
/**
* Functions for filtering elements of an iterable that match a predicate
*/
import { pubsub } from './lib/pubsub';
async function* _filter(input, test) {
for await (const value of input) {
const result = await test(value);
if (result === false || result === null)
continue;
if (result === true)
yield value;
else
yield result;
}
}
/**
* Process an iterable with limited concurrency
*
* @param input Source iterable to filter
* @param test Function to test each item
* @param concurrency Maximum number of filter operations
*/
function _filterConcurrently(iterator, test, concurrency = 1) {
const { output, input } = pubsub(concurrency);
return output({
onStart: () => {
return input(iterator, async (value) => {
const result = await test(value);
if (result === false || result === null)
return;
if (result === true)
return value;
else
return result;
});
},
});
}
export function filter(input, test, concurrency) {
// If the first argument is a function, assume it's the test function and return a curried function
if (typeof input === 'function') {
return (_input) => filter(_input, input, test);
}
// Otherwise, first argument is the input Series and second is the test function
return _filterConcurrently(input, test, concurrency);
}
export function identity(item) {
return item;
}
//# sourceMappingURL=filter.js.map