1
0

move types around

This commit is contained in:
2024-05-09 23:55:40 +02:00
parent fccebc61b6
commit 679a61f040
10 changed files with 58 additions and 60 deletions

View File

@@ -1,32 +1,11 @@
import { Accumulator, Action, BiConverter, Comparer, Converter, Enumerable, Equater, Predicate } from "./sync.js";
import { AsyncFunction, MaybeAsyncFunction, MaybePromise, asAsyncGenerator, combineAsyncComparers, defaultArrayComparer, identity, isAsyncIterable, operatorCompare, strictEquals } from "./utils.js";
import { Enumerable } from "./sync.js";
import { asAsyncGenerator, combineAsyncComparers, defaultArrayComparer, identity, isAsyncIterable, operatorCompare, strictEquals } from "./utils.js";
import { createQueue } from "./queue.js";
import { selectionSorter } from "./sorting.js";
import { createAsyncEqualitySet } from "./equality-set.js";
import { createAsyncEqualityMap } from "./equality-map.js";
import { Collector } from "./collector.js";
//#region types
export type MaybeAsyncIterable<T> = Iterable<T> | AsyncIterable<T>;
export type MaybeAsyncIterator<T> = Iterator<T> | AsyncIterator<T>;
export type AsyncPredicate<T> = AsyncFunction<Predicate<T>>;
export type MaybeAsyncPredicate<T> = MaybeAsyncFunction<Predicate<T>>;
export type AsyncConverter<TFrom, TTo> = AsyncFunction<Converter<TFrom, TTo>>;
export type MaybeAsyncConverter<TFrom, TTo> = MaybeAsyncFunction<Converter<TFrom, TTo>>;
export type AsyncBiConverter<TFromFirst, TFromSecond, TTo> = AsyncFunction<BiConverter<TFromFirst, TFromSecond, TTo>>;
export type MaybeAsyncBiConverter<TFromFirst, TFromSecond, TTo> = MaybeAsyncFunction<BiConverter<TFromFirst, TFromSecond, TTo>>;
export type AsyncAction<T> = AsyncFunction<Action<T>>;
export type MaybeAsyncAction<T> = MaybeAsyncFunction<Action<T>>;
export type AsyncAccumulator<TElement, TAccumulator> = AsyncFunction<Accumulator<TElement, TAccumulator>>;
export type MaybeAsyncAccumulator<T, U> = MaybeAsyncFunction<Accumulator<T, U>>;
export type AsyncComparer<T> = AsyncFunction<Comparer<T>>;
export type MaybeAsyncComparer<T> = MaybeAsyncFunction<Comparer<T>>;
export type AsyncEquater<T> = AsyncFunction<Equater<T>>;
export type MaybeAsyncEquater<T> = MaybeAsyncFunction<Equater<T>>;
//#endregion
import { MaybeAsyncPredicate, MaybeAsyncConverter, MaybeAsyncIterable, MaybeAsyncEquater, MaybeAsyncBiConverter, MaybeAsyncAccumulator, MaybeAsyncComparer, Predicate, MaybeAsyncAction, MaybePromise } from "./types.js";
//#region interfaces
@@ -1293,15 +1272,7 @@ abstract class BaseOrderedAsyncEnumerable<TElement> extends BaseAsyncEnumerable<
arr.push(obj);
}
//if (this.#sorter) {
await selectionSorter.sort(arr, this.#descending, this.#sorter);
//} else {
// arr.sort();
// if (this.#descending) {
// arr.reverse();
// }
//}
yield* arr;
}
@@ -1504,6 +1475,18 @@ class WrappedEnumerable<T> extends BaseAsyncEnumerable<T> {
return this.#enumerable.nonEnumeratedCount();
}
override async fastCount() {
return this.#enumerable.fastCount();
}
override async maxCount() {
return this.#enumerable.maxCount();
}
override async asArray() {
return this.#enumerable.asArray();
}
override async *iterator() {
yield* this.#enumerable;
}

View File

@@ -1,6 +1,6 @@
import { Collector } from "./collector.js";
import { Enumerable } from "./sync.js";
import { asArray, isDefined } from "./utils.js";
import { asArray } from "./utils.js";
export interface BitArray extends Iterable<boolean> {
readonly length: number;
@@ -251,7 +251,7 @@ class BitArrayImpl implements BitArray {
}
public equals(other: BitArray) {
return other === this || isDefined(other) && (other instanceof BitArrayImpl ? Enumerable.arrayLike(this.#bits).sequenceEquals(Enumerable.arrayLike(other.#bits)) : Enumerable.sequence(this).sequenceEquals(Enumerable.sequence(other)));
return other === this || other && (other instanceof BitArrayImpl ? Enumerable.arrayLike(this.#bits).sequenceEquals(Enumerable.arrayLike(other.#bits)) : Enumerable.sequence(this).sequenceEquals(Enumerable.sequence(other)));
}
public toString() {
@@ -493,7 +493,7 @@ class BitArraySlice implements BitArray {
}
public equals(other: BitArray) {
return other === this || isDefined(other) && Enumerable.sequence(this).sequenceEquals(Enumerable.sequence(other));
return other === this || other && Enumerable.sequence(this).sequenceEquals(Enumerable.sequence(other));
}
public toString() {

View File

@@ -1,4 +1,4 @@
import { Converter } from "./sync.js";
import { Converter } from "./types.js";
export interface Collector<TElement, TAccumulator, TResult> {
initialize(): TAccumulator;

View File

@@ -1,5 +1,4 @@
import { Equater } from "./sync.js";
import { MaybeAsyncEquater } from "./async.js";
import { Equater, MaybeAsyncEquater } from "./types.js";
import { asAsyncGenerator } from "./utils.js";
export interface EqualityMap<K, V> extends Iterable<[K, V]> {

View File

@@ -1,5 +1,4 @@
import { Equater } from "./sync.js";
import { MaybeAsyncEquater } from "./async.js";
import { Equater, MaybeAsyncEquater } from "./types.js";
import { asAsyncGenerator } from "./utils.js";
export interface EqualitySet<T> extends Iterable<T> {

View File

@@ -1,4 +1,4 @@
export * from "./sync.js";
export * from "./async.js";
export * from "./collector.js";
export * from "./random.js";
export { BaseEnumerable, Enumerable, GroupedEnumerable, OrderedEnumerable } from "./sync.js";
export { AsyncEnumerable, BaseAsyncEnumerable, GroupedAsyncEnumerable, OrderedAsyncEnumerable } from "./async.js";
export { Collector } from "./collector.js";
export { Random, RandomOptions } from "./random.js";

View File

@@ -1,4 +1,4 @@
import { MaybeAsyncComparer } from "./async.js";
import { MaybeAsyncComparer } from "./types.js";
import { reverseAsyncComparer } from "./utils.js";
export interface AsyncSorter {

View File

@@ -4,15 +4,7 @@ import { RandomGenerator, RandomOptions, Random } from "./random.js";
import { createQueue } from "./queue.js";
import { Collector } from "./collector.js";
import { combineComparers, defaultArrayComparer, identity, operatorCompare, reverseComparer, strictEquals, wrapAsIterable } from "./utils.js";
export type Predicate<T> = (obj: T) => boolean;
export type FilterPredicate<TElement, TFiltered extends TElement> = (obj: TElement) => obj is TFiltered;
export type Converter<TFrom, TTo> = (obj: TFrom) => TTo;
export type BiConverter<TFromFirst, TFromSecond, TTo> = (first: TFromFirst, second: TFromSecond) => TTo;
export type Action<T> = (obj: T) => void;
export type Accumulator<TElement, TAccumulator> = (acc: TAccumulator, obj: TElement) => TAccumulator;
export type Comparer<T> = (first: T, second: T) => number;
export type Equater<T> = (first: T, second: T) => boolean;
import { Predicate, Converter, FilterPredicate, Equater, BiConverter, Accumulator, Comparer, Action } from "./types.js";
export interface Enumerable<TElement> extends Iterable<TElement> {
iterator(): Iterator<TElement>;

30
src/types.ts Normal file
View File

@@ -0,0 +1,30 @@
export type Predicate<T> = (obj: T) => boolean;
export type FilterPredicate<TElement, TFiltered extends TElement> = (obj: TElement) => obj is TFiltered;
export type Converter<TFrom, TTo> = (obj: TFrom) => TTo;
export type BiConverter<TFromFirst, TFromSecond, TTo> = (first: TFromFirst, second: TFromSecond) => TTo;
export type Action<T> = (obj: T) => void;
export type Accumulator<TElement, TAccumulator> = (acc: TAccumulator, obj: TElement) => TAccumulator;
export type Comparer<T> = (first: T, second: T) => number;
export type Equater<T> = (first: T, second: T) => boolean;
export type MaybeAsyncIterable<T> = Iterable<T> | AsyncIterable<T>;
export type MaybeAsyncIterator<T> = Iterator<T> | AsyncIterator<T>;
export type AsyncFunction<TFunc extends (...args: any) => any> = TFunc extends (...args: infer P) => infer R ? (...args: P) => Promise<Awaited<R>> : never;
export type MaybePromise<T> = T | Promise<T>;
export type MaybeAsyncFunction<TFunc extends (...args: any) => any> = TFunc extends (...args: infer P) => infer R ? (...args: P) => MaybePromise<R> : never;
export type AsyncPredicate<T> = AsyncFunction<Predicate<T>>;
export type MaybeAsyncPredicate<T> = MaybeAsyncFunction<Predicate<T>>;
export type AsyncConverter<TFrom, TTo> = AsyncFunction<Converter<TFrom, TTo>>;
export type MaybeAsyncConverter<TFrom, TTo> = MaybeAsyncFunction<Converter<TFrom, TTo>>;
export type AsyncBiConverter<TFromFirst, TFromSecond, TTo> = AsyncFunction<BiConverter<TFromFirst, TFromSecond, TTo>>;
export type MaybeAsyncBiConverter<TFromFirst, TFromSecond, TTo> = MaybeAsyncFunction<BiConverter<TFromFirst, TFromSecond, TTo>>;
export type AsyncAction<T> = AsyncFunction<Action<T>>;
export type MaybeAsyncAction<T> = MaybeAsyncFunction<Action<T>>;
export type AsyncAccumulator<TElement, TAccumulator> = AsyncFunction<Accumulator<TElement, TAccumulator>>;
export type MaybeAsyncAccumulator<T, U> = MaybeAsyncFunction<Accumulator<T, U>>;
export type AsyncComparer<T> = AsyncFunction<Comparer<T>>;
export type MaybeAsyncComparer<T> = MaybeAsyncFunction<Comparer<T>>;
export type AsyncEquater<T> = AsyncFunction<Equater<T>>;
export type MaybeAsyncEquater<T> = MaybeAsyncFunction<Equater<T>>;

View File

@@ -1,9 +1,4 @@
import { AsyncComparer, MaybeAsyncComparer, MaybeAsyncIterator } from "./async.js";
import { Comparer } from "./sync.js";
export type AsyncFunction<TFunc extends (...args: any) => any> = TFunc extends (...args: infer P) => infer R ? (...args: P) => Promise<Awaited<R>> : never;
export type MaybePromise<T> = T | Promise<T>;
export type MaybeAsyncFunction<TFunc extends (...args: any) => any> = TFunc extends (...args: infer P) => infer R ? (...args: P) => MaybePromise<R> : never;
import { Comparer, MaybeAsyncComparer, AsyncComparer, MaybeAsyncIterator } from "./types.js";
export function isDefined<T = any>(obj: T): obj is NonNullable<T> {
return obj !== undefined && obj !== null;