refactor
This commit is contained in:
@@ -7,7 +7,7 @@ import { AsyncRandomOptions } from "../random/types.js";
|
||||
import { selectionSorter } from "../sorting.js";
|
||||
import { Sequence } from "../sync/types.js";
|
||||
import { MaybeAsyncConverter, MaybeAsyncPredicate, MaybeAsyncEquater, MaybeAsyncBiConverter, MaybeAsyncAccumulator, MaybeAsyncComparer, MaybeAsyncAction, MaybePromiseLike, MaybeAsyncGenerator, MaybeAsyncSequence } from "../types.js";
|
||||
import { strictEquals, identity, operatorCompare, asAsyncGenerator, defaultArrayComparer, combineAsyncComparers } from "../utils.js";
|
||||
import { strictEquals, identity, operatorCompare, defaultArrayComparer, combineAsyncComparers, asAsyncIterable } from "../utils.js";
|
||||
import { array, empty, wrap } from "./index.js";
|
||||
import { AsyncSequence, GroupedAsyncSequence, OrderedAsyncSequence } from "./types.js";
|
||||
|
||||
@@ -1467,7 +1467,7 @@ class SkipWhileAsyncSequence<T> extends BaseAsyncSequence<T> {
|
||||
break;
|
||||
}
|
||||
|
||||
yield* asAsyncGenerator(iterator);
|
||||
yield* asAsyncIterable(iterator);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1504,7 +1504,7 @@ class SkipLastAsyncSequence<T> extends BaseAsyncSequence<T> {
|
||||
|
||||
i = 0;
|
||||
|
||||
for await (const obj of asAsyncGenerator(iterator)) {
|
||||
for await (const obj of asAsyncIterable(iterator)) {
|
||||
yield buffer[i];
|
||||
buffer[i] = obj;
|
||||
i = (i + 1) % this.#n;
|
||||
@@ -1540,7 +1540,7 @@ class SkipAsyncSequence<T> extends BaseAsyncSequence<T> {
|
||||
i++;
|
||||
} while (i < this.#n);
|
||||
|
||||
yield* asAsyncGenerator(iterator);
|
||||
yield* asAsyncIterable(iterator);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1705,23 +1705,17 @@ class PrependAsyncSequence<T> extends BaseAsyncSequence<T> {
|
||||
}
|
||||
}
|
||||
|
||||
class PeekAsyncSequence<T> extends BaseAsyncSequence<T> {
|
||||
readonly #sequence: AsyncSequence<T>;
|
||||
class PeekAsyncSequence<T> extends DelegatedAsyncSequence<T> {
|
||||
readonly #action: MaybeAsyncAction<T>;
|
||||
|
||||
constructor(sequence: AsyncSequence<T>, action: MaybeAsyncAction<T>) {
|
||||
super();
|
||||
super(sequence);
|
||||
|
||||
this.#sequence = sequence;
|
||||
this.#action = action;
|
||||
}
|
||||
|
||||
override async nonEnumeratedCount() {
|
||||
return await this.#sequence.nonEnumeratedCount();
|
||||
}
|
||||
|
||||
override async *iterator() {
|
||||
for await (const obj of this.#sequence) {
|
||||
for await (const obj of this.sequence) {
|
||||
await this.#action(obj);
|
||||
yield obj;
|
||||
}
|
||||
@@ -1995,13 +1989,15 @@ class GroupByAsyncSequence<TElement, TKey, TResult> extends BaseAsyncSequence<Gr
|
||||
|
||||
for await (const obj of this.#sequence) {
|
||||
const key = await this.#keySelector(obj);
|
||||
let grouping = await groupings.get(key);
|
||||
const value = await this.#elementSelector(obj);
|
||||
|
||||
if (!grouping) {
|
||||
await groupings.set(key, grouping = []);
|
||||
const grouping = await groupings.get(key);
|
||||
|
||||
if (grouping) {
|
||||
grouping.push(value);
|
||||
} else {
|
||||
await groupings.set(key, [value]);
|
||||
}
|
||||
|
||||
grouping.push(await this.#elementSelector(obj));
|
||||
}
|
||||
|
||||
for (const entry of groupings) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { join } from "../collector/index.js";
|
||||
import { arrayLike, sequence } from "../sync/index.js";
|
||||
import { emptyIterableIterator } from "../utils.js";
|
||||
import { BitArray } from "./types.js";
|
||||
|
||||
const BYTE_SIZE = Uint8Array.BYTES_PER_ELEMENT * 8;
|
||||
@@ -234,9 +235,7 @@ export class BitArrayImpl implements BitArray {
|
||||
|
||||
export class EmptyBitArray implements BitArray {
|
||||
[Symbol.iterator](): Iterator<boolean, any, undefined> {
|
||||
return {
|
||||
next: () => ({ done: true, value: undefined })
|
||||
};
|
||||
return emptyIterableIterator<boolean>();
|
||||
}
|
||||
|
||||
get length() {
|
||||
|
||||
@@ -139,7 +139,7 @@ class CustomEqualityMap<K, V> implements EqualityMap<K, V> {
|
||||
|
||||
*[Symbol.iterator]() {
|
||||
for (const entry of this.#list) {
|
||||
yield [entry[0], entry[1]] as [K, V]; // no entry mutation allowed!
|
||||
yield entry.slice() as [K, V]; // no entry mutation allowed!
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -287,7 +287,7 @@ class CustomAsyncEqualityMap<K, V> implements AsyncEqualityMap<K, V> {
|
||||
|
||||
*[Symbol.iterator]() {
|
||||
for (const entry of this.#list) {
|
||||
yield [entry[0], entry[1]] as [K, V]; // no entry mutation allowed!
|
||||
yield entry.slice() as [K, V]; // no entry mutation allowed!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import { createQueue } from "../queue.js";
|
||||
import { getRandomElement } from "../random/index.js";
|
||||
import { RandomOptions } from "../random/types.js";
|
||||
import { Converter, FilterPredicate, Equater, BiConverter, Predicate, Accumulator, Comparer, Action } from "../types.js";
|
||||
import { strictEquals, identity, operatorCompare, reverseComparer, wrapAsIterable, defaultArrayComparer, combineComparers } from "../utils.js";
|
||||
import { strictEquals, identity, operatorCompare, reverseComparer, asIterable, defaultArrayComparer, combineComparers } from "../utils.js";
|
||||
import { array, empty } from "./index.js";
|
||||
import { Sequence, GroupedSequence, OrderedSequence } from "./types.js";
|
||||
|
||||
@@ -1645,7 +1645,7 @@ class SkipWhileSequence<T> extends BaseSequence<T> {
|
||||
}
|
||||
|
||||
override *iterator() {
|
||||
const e = wrapAsIterable(this.#sequence.iterator());
|
||||
const e = asIterable(this.#sequence.iterator());
|
||||
|
||||
for (const obj of e) {
|
||||
if (!this.#predicate(obj)) {
|
||||
@@ -1695,7 +1695,7 @@ class SkipLastSequence<T> extends BaseSequence<T> {
|
||||
|
||||
i = 0;
|
||||
|
||||
for (const obj of wrapAsIterable(iterator)) {
|
||||
for (const obj of asIterable(iterator)) {
|
||||
yield buffer[i];
|
||||
buffer[i] = obj;
|
||||
i = (i + 1) % this.#n;
|
||||
@@ -1735,7 +1735,7 @@ class SkipSequence<T> extends BaseSequence<T> {
|
||||
i++;
|
||||
} while (i < this.#n);
|
||||
|
||||
yield* wrapAsIterable(iterator);
|
||||
yield* asIterable(iterator);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1930,7 +1930,7 @@ class PeekSequence<T> extends DelegatedSequence<T> {
|
||||
}
|
||||
|
||||
override *iterator() {
|
||||
for (const obj of wrapAsIterable(super.iterator())) {
|
||||
for (const obj of this.sequence) {
|
||||
this.#action(obj);
|
||||
yield obj;
|
||||
}
|
||||
@@ -2230,13 +2230,15 @@ class GroupBySequence<TElement, TKey, TResult> extends BaseSequence<GroupedSeque
|
||||
|
||||
for (const obj of this.#sequence) {
|
||||
const key = this.#keySelector(obj);
|
||||
let grouping = groupings.get(key);
|
||||
const value = this.#elementSelector(obj);
|
||||
|
||||
if (!grouping) {
|
||||
groupings.set(key, grouping = []);
|
||||
const grouping = groupings.get(key);
|
||||
|
||||
if (grouping) {
|
||||
grouping.push(value);
|
||||
} else {
|
||||
groupings.set(key, [value]);
|
||||
}
|
||||
|
||||
grouping.push(this.#elementSelector(obj));
|
||||
}
|
||||
|
||||
for (const entry of groupings) {
|
||||
|
||||
64
src/utils.ts
64
src/utils.ts
@@ -70,30 +70,6 @@ export function reverseAsyncComparer<T>(comparer: MaybeAsyncComparer<T>): AsyncC
|
||||
return async (a, b) => await comparer(b, a);
|
||||
}
|
||||
|
||||
export function* asGenerator<T>(iterator: Iterator<T>) {
|
||||
while (true) {
|
||||
const next = iterator.next();
|
||||
|
||||
if (next.done) {
|
||||
return next.value;
|
||||
}
|
||||
|
||||
yield next.value;
|
||||
}
|
||||
}
|
||||
|
||||
export async function* asAsyncGenerator<T>(iterator: MaybeAsyncIterator<T>) {
|
||||
while (true) {
|
||||
const next = await iterator.next();
|
||||
|
||||
if (next.done) {
|
||||
return next.value;
|
||||
}
|
||||
|
||||
yield next.value;
|
||||
}
|
||||
}
|
||||
|
||||
export function asArray<T>(iterable: Iterable<T>) {
|
||||
return Array.isArray(iterable) ? <T[]>iterable : Array.from(iterable);
|
||||
}
|
||||
@@ -110,6 +86,44 @@ class WrappedIterator<T> implements Iterable<T> {
|
||||
}
|
||||
}
|
||||
|
||||
export function wrapAsIterable<T>(iterator: Iterator<T>): Iterable<T> {
|
||||
export function asIterable<T>(iterator: Iterator<T>): Iterable<T> {
|
||||
return isIterable<T>(iterator) ? iterator : new WrappedIterator(iterator);
|
||||
}
|
||||
|
||||
class WrappedAsyncIterator<T> implements AsyncIterable<T> {
|
||||
readonly #iterator: AsyncIterator<T>;
|
||||
|
||||
constructor(iterator: AsyncIterator<T>) {
|
||||
this.#iterator = iterator;
|
||||
}
|
||||
|
||||
[Symbol.asyncIterator]() {
|
||||
return this.#iterator;
|
||||
}
|
||||
}
|
||||
|
||||
export function asAsyncIterable<T>(iterator: AsyncIterator<T>): AsyncIterable<T> {
|
||||
return isAsyncIterable<T>(iterator) ? iterator : new WrappedAsyncIterator(iterator);
|
||||
}
|
||||
|
||||
const _emptyIterableIterator = new class EmptyIterableIterator implements IterableIterator<any> {
|
||||
[Symbol.iterator]() {
|
||||
return this;
|
||||
}
|
||||
|
||||
next(): IteratorResult<any, any> {
|
||||
return { done: true, value: undefined };
|
||||
}
|
||||
|
||||
return(value?: any): IteratorResult<any, any> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
throw(e?: any): IteratorResult<any, any> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
}
|
||||
|
||||
export function emptyIterableIterator<T>(): IterableIterator<T> {
|
||||
return _emptyIterableIterator;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user