Refactor outbound delivery state, take 2

This reverts commit ad217c808d.
This commit is contained in:
Evan Hahn 2021-07-19 17:44:49 -05:00 committed by GitHub
parent aade43bfa3
commit c4a09b7507
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 2303 additions and 502 deletions

View file

@ -119,6 +119,9 @@ export function groupBy<T>(
return result;
}
export const isEmpty = (iterable: Iterable<unknown>): boolean =>
Boolean(iterable[Symbol.iterator]().next().done);
export function map<T, ResultT>(
iterable: Iterable<T>,
fn: (value: T) => ResultT
@ -167,6 +170,33 @@ export function reduce<T, TResult>(
return result;
}
export function repeat<T>(value: T): Iterable<T> {
return new RepeatIterable(value);
}
class RepeatIterable<T> implements Iterable<T> {
constructor(private readonly value: T) {}
[Symbol.iterator](): Iterator<T> {
return new RepeatIterator(this.value);
}
}
class RepeatIterator<T> implements Iterator<T> {
private readonly iteratorResult: IteratorResult<T>;
constructor(value: Readonly<T>) {
this.iteratorResult = {
done: false,
value,
};
}
next(): IteratorResult<T> {
return this.iteratorResult;
}
}
export function take<T>(iterable: Iterable<T>, amount: number): Iterable<T> {
return new TakeIterable(iterable, amount);
}
@ -194,3 +224,29 @@ class TakeIterator<T> implements Iterator<T> {
return nextIteration;
}
}
// In the future, this could support number and symbol property names.
export function zipObject<ValueT>(
props: Iterable<string>,
values: Iterable<ValueT>
): Record<string, ValueT> {
const result: Record<string, ValueT> = {};
const propsIterator = props[Symbol.iterator]();
const valuesIterator = values[Symbol.iterator]();
// eslint-disable-next-line no-constant-condition
while (true) {
const propIteration = propsIterator.next();
if (propIteration.done) {
break;
}
const valueIteration = valuesIterator.next();
if (valueIteration.done) {
break;
}
result[propIteration.value] = valueIteration.value;
}
return result;
}