Returns the first iterator value that matches the condition.
npm i iter-get
or
yarn add iter-get
import { iterGet } from 'iter-get';
iterGet(iterator, options?);
By default it returns the first defined value received from the iterator:
const source = {
findHere: undefined,
findThere: undefined,
foundIt: 'gotcha!',
unreachable: 'in da house',
};
function* gen() {
yield source.findHere;
yield source.findThere;
yield source.foundIt;
yield source.unreachable;
}
const iterator = gen();
iterGet(iterator); // 'gotcha!'
default:
undefined
The default value returned after the iterator has done:
function* gen() {
yield;
yield;
yield;
}
const iterator = gen();
iterGet(iterator, { dflt: 'default' }); // 'default'
default:
(value) => value === undefined
A callback that determines whether the current iterator value should be skipped:
function* gen() {
yield '';
yield 0;
yield null;
yield 'result';
yield 'the second';
}
const iterator1 = gen();
const iterator2 = gen();
iterGet(iterator1); // ''
iterGet(iterator2, { skip: (value) => !value }); // 'result'
default:
undefined
If passed, returns the value for which the callback returns true:
function* gen() {
yield { id: 1, x: 0 };
yield { id: 2, x: 3 };
yield { id: 3, x: 7 };
yield { id: 4, x: 2 };
}
const iterator = gen();
iterGet(iterator, { find: (value) => value.x > 5 }); // { id: 3, x: 7 }
Licensed under the MIT license. Copyright (c) 2022 Vladislav Alexeev