change project structure to remove namespaces
This commit is contained in:
2406
src/sync/impl.ts
Normal file
2406
src/sync/impl.ts
Normal file
File diff suppressed because it is too large
Load Diff
147
src/sync/index.ts
Normal file
147
src/sync/index.ts
Normal file
@@ -0,0 +1,147 @@
|
||||
import { mathRandom } from "../random/index.js";
|
||||
import { RandomGenerator } from "../random/types.js";
|
||||
import { BigIntRangeEnumerable, ConcatEnumerable, EmptyEnumerable, EnumerableMarker, FunctionEnumerable, GeneratorEnumerable, RangeEnumerable, RepeatEnumerable, RepeatForeverEnumerable, WrappedArray, WrappedArrayLike, WrappedIterable, WrappedMap, WrappedObject, WrappedSet } from "./impl.js";
|
||||
import { Enumerable } from "./types.js";
|
||||
|
||||
export function wrap<T>(iterable: Iterable<T>): Enumerable<T> {
|
||||
if (isEnumerable<T>(iterable)) {
|
||||
return iterable;
|
||||
}
|
||||
|
||||
if (Array.isArray(iterable)) {
|
||||
return array(iterable);
|
||||
}
|
||||
|
||||
if (iterable instanceof Set) {
|
||||
return set<T>(iterable);
|
||||
}
|
||||
|
||||
if (iterable instanceof Map) {
|
||||
return map(iterable) as unknown as Enumerable<T>;
|
||||
}
|
||||
|
||||
return sequence(iterable);
|
||||
}
|
||||
|
||||
export function sequence<T>(iterable: Iterable<T>): Enumerable<T> {
|
||||
return new WrappedIterable(iterable);
|
||||
}
|
||||
|
||||
export function empty<T>(): Enumerable<T> {
|
||||
return EmptyEnumerable.INSTANCE;
|
||||
}
|
||||
|
||||
export function single<T>(obj: T): Enumerable<T> {
|
||||
return new WrappedObject(obj);
|
||||
}
|
||||
|
||||
export function array<T>(array: T[]): Enumerable<T> {
|
||||
return new WrappedArray(array);
|
||||
}
|
||||
|
||||
export function arrayLike<T>(arrayLike: ArrayLike<T>): Enumerable<T> {
|
||||
return new WrappedArrayLike(arrayLike);
|
||||
}
|
||||
|
||||
export function set<T>(set: Set<T>): Enumerable<T> {
|
||||
return new WrappedSet(set);
|
||||
}
|
||||
|
||||
export function map<K, V>(map: Map<K, V>): Enumerable<[K, V]> {
|
||||
return new WrappedMap(map);
|
||||
}
|
||||
|
||||
export function of<T>(...elements: T[]): Enumerable<T> {
|
||||
switch (elements.length) {
|
||||
case 0:
|
||||
return empty();
|
||||
case 1:
|
||||
return single(elements[0]);
|
||||
default:
|
||||
return array(elements);
|
||||
}
|
||||
}
|
||||
|
||||
export function ofPropertyKeys<T extends PropertyKey>(...elements: T[]): Enumerable<T> {
|
||||
return of(...elements);
|
||||
}
|
||||
|
||||
export function entries<T>(o: Record<string, T> | ArrayLike<T>): Enumerable<[string, T]> {
|
||||
return array(Object.entries(o));
|
||||
}
|
||||
|
||||
export function keys(o: object): Enumerable<string> {
|
||||
return array(Object.keys(o));
|
||||
}
|
||||
|
||||
export function func<T>(f: () => T): Enumerable<T> {
|
||||
return new FunctionEnumerable(f);
|
||||
}
|
||||
|
||||
export function generator<T>(generator: () => Iterable<T>): Enumerable<T> {
|
||||
return new GeneratorEnumerable(generator);
|
||||
}
|
||||
|
||||
export function range(max: number): Enumerable<number>
|
||||
export function range(min: number, max: number): Enumerable<number>
|
||||
export function range(min: number, max: number, step: number): Enumerable<number>
|
||||
export function range(a: number, b?: number, c?: number): Enumerable<number> {
|
||||
if (b === undefined) {
|
||||
b = a;
|
||||
a = 0;
|
||||
}
|
||||
|
||||
if (c === undefined) {
|
||||
c = 1;
|
||||
}
|
||||
|
||||
return new RangeEnumerable(a, b, c);
|
||||
}
|
||||
|
||||
export function bigintRange(max: bigint): Enumerable<bigint>
|
||||
export function bigintRange(min: bigint, max: bigint): Enumerable<bigint>
|
||||
export function bigintRange(min: bigint, max: bigint, step: bigint): Enumerable<bigint>
|
||||
export function bigintRange(a: bigint, b?: bigint, c?: bigint): Enumerable<bigint> {
|
||||
if (b === undefined) {
|
||||
b = a;
|
||||
a = 0n;
|
||||
}
|
||||
|
||||
if (c === undefined) {
|
||||
c = 1n;
|
||||
}
|
||||
|
||||
return new BigIntRangeEnumerable(a, b, c);
|
||||
}
|
||||
|
||||
export function repeat<T>(value: T, count?: number): Enumerable<T> {
|
||||
if (count == undefined) {
|
||||
return new RepeatForeverEnumerable(value);
|
||||
}
|
||||
|
||||
if (count < 0) {
|
||||
throw new RangeError();
|
||||
}
|
||||
|
||||
if (count === 0) {
|
||||
return empty();
|
||||
}
|
||||
|
||||
if (count === 1) {
|
||||
return new WrappedObject(value);
|
||||
}
|
||||
|
||||
return new RepeatEnumerable(value, count);
|
||||
}
|
||||
|
||||
export function randomSequence(random?: RandomGenerator): Enumerable<number> {
|
||||
return new FunctionEnumerable(random ?? mathRandom);
|
||||
}
|
||||
|
||||
export function concat<T>(...enumerables: Enumerable<T>[]): Enumerable<T> {
|
||||
return new ConcatEnumerable(enumerables);
|
||||
}
|
||||
|
||||
export function isEnumerable<T = any>(obj: any): obj is Enumerable<T> {
|
||||
return obj instanceof EnumerableMarker;
|
||||
}
|
||||
135
src/sync/types.ts
Normal file
135
src/sync/types.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
import { Collector } from "../collector/types.js";
|
||||
import { RandomOptions } from "../random/types.js";
|
||||
import { Predicate, Converter, FilterPredicate, Equater, BiConverter, Accumulator, Comparer, Action } from "../types.js";
|
||||
|
||||
export interface Enumerable<TElement> extends Iterable<TElement> {
|
||||
iterator(): Iterator<TElement>;
|
||||
|
||||
apply<TResult>(pipeline: (enumerable: Enumerable<TElement>) => TResult): TResult;
|
||||
|
||||
count(predicate?: Predicate<TElement>): number;
|
||||
nonEnumeratedCount(): number;
|
||||
fastCount(): number;
|
||||
maxCount(): number;
|
||||
|
||||
select<TResult>(selector: Converter<TElement, TResult>): Enumerable<TResult>;
|
||||
selectMany<TResult>(selector: Converter<TElement, Iterable<TResult>>): Enumerable<TResult>;
|
||||
|
||||
where<TFiltered extends TElement>(predicate: FilterPredicate<TElement, TFiltered>): Enumerable<TFiltered>;
|
||||
where(predicate: Predicate<TElement>): Enumerable<TElement>;
|
||||
|
||||
groupBy<TKey>(keySelector: Converter<TElement, TKey>, elementSelector?: undefined, keyComparer?: Equater<TKey>): Enumerable<GroupedEnumerable<TKey, TElement>>;
|
||||
groupBy<TKey, TResult>(keySelector: Converter<TElement, TKey>, elementSelector: Converter<TElement, TResult>, keyComparer?: Equater<TKey>): Enumerable<GroupedEnumerable<TKey, TResult>>;
|
||||
|
||||
join<TOther, TKey>(iterable: Iterable<TOther>, firstKeySelector: Converter<TElement, TKey>, secondKeySelector: Converter<TOther, TKey>, resultSelector?: undefined, keyComparer?: Equater<TKey>): Enumerable<[TElement, TOther]>;
|
||||
join<TOther, TKey, TResult>(iterable: Iterable<TOther>, firstKeySelector: Converter<TElement, TKey>, secondKeySelector: Converter<TOther, TKey>, resultSelector: BiConverter<TElement, TOther, TResult>, keyComparer?: Equater<TKey>): Enumerable<TResult>;
|
||||
|
||||
groupJoin<TOther, TKey>(iterable: Iterable<TOther>, firstKeySelector: Converter<TElement, TKey>, secondKeySelector: Converter<TOther, TKey>, resultSelector?: undefined, keyComparer?: Equater<TKey>): Enumerable<GroupedEnumerable<TElement, TOther>>;
|
||||
groupJoin<TOther, TKey, TResult>(iterable: Iterable<TOther>, firstKeySelector: Converter<TElement, TKey>, secondKeySelector: Converter<TOther, TKey>, resultSelector: BiConverter<TElement, Enumerable<TOther>, TResult>, keyComparer?: Equater<TKey>): Enumerable<TResult>;
|
||||
|
||||
contains(obj: TElement, equater?: Equater<TElement>): boolean;
|
||||
|
||||
sequenceEquals(iterable: Iterable<TElement>, equater?: Equater<TElement>): boolean;
|
||||
|
||||
append(obj: TElement): Enumerable<TElement>;
|
||||
|
||||
prepend(obj: TElement): Enumerable<TElement>;
|
||||
|
||||
remove(obj: TElement, all?: boolean, equater?: Equater<TElement>): Enumerable<TElement>;
|
||||
|
||||
concat(...iterables: Iterable<TElement>[]): Enumerable<TElement>;
|
||||
|
||||
first(predicate?: Predicate<TElement>): TElement;
|
||||
firstOrDefault(predicate?: Predicate<TElement>, def?: TElement): TElement | undefined;
|
||||
|
||||
last(predicate?: Predicate<TElement>): TElement;
|
||||
lastOrDefault(predicate?: Predicate<TElement>, def?: TElement): TElement | undefined;
|
||||
|
||||
single(predicate?: Predicate<TElement>): TElement;
|
||||
singleOrDefault(predicate?: Predicate<TElement>, def?: TElement): TElement | undefined;
|
||||
|
||||
elementAt(index: number): TElement;
|
||||
elementAtOrDefault(index: number, def?: TElement): TElement | undefined;
|
||||
|
||||
aggregate(accumulator: Accumulator<TElement, TElement>): TElement;
|
||||
aggregate<TAccumulator>(accumulator: Accumulator<TElement, TAccumulator>, seed?: TAccumulator): TAccumulator;
|
||||
aggregate<TAccumulator, TResult>(accumulator: Accumulator<TElement, TAccumulator>, seed?: TAccumulator, resultSelector?: Converter<TAccumulator, TResult>): TResult;
|
||||
|
||||
min(comparer?: Comparer<TElement>): TElement;
|
||||
minBy<TBy>(selector: Converter<TElement, TBy>, comparer?: Comparer<TBy>): TElement;
|
||||
|
||||
max(comparer?: Comparer<TElement>): TElement;
|
||||
maxBy<TBy>(selector: Converter<TElement, TBy>, comparer?: Comparer<TBy>): TElement;
|
||||
|
||||
order(comparer?: Comparer<TElement>): OrderedEnumerable<TElement>;
|
||||
orderBy<TBy>(selector: Converter<TElement, TBy>, comparer?: Comparer<TBy>): OrderedEnumerable<TElement>;
|
||||
|
||||
orderDescending(comparer?: Comparer<TElement>): OrderedEnumerable<TElement>;
|
||||
orderByDescending<TBy>(selector: Converter<TElement, TBy>, comparer?: Comparer<TBy>): OrderedEnumerable<TElement>;
|
||||
|
||||
distinct(equater?: Equater<TElement>): Enumerable<TElement>;
|
||||
distinctBy<TBy>(selector: Converter<TElement, TBy>, equater?: Equater<TBy>): Enumerable<TElement>;
|
||||
|
||||
union(iterable: Iterable<TElement>, equater?: Equater<TElement>): Enumerable<TElement>;
|
||||
unionBy<TBy>(iterable: Iterable<TElement>, selector: Converter<TElement, TBy>, equater?: Equater<TBy>): Enumerable<TElement>;
|
||||
|
||||
except(iterable: Iterable<TElement>, equater?: Equater<TElement>): Enumerable<TElement>;
|
||||
exceptBy<TBy>(iterable: Iterable<TElement>, selector: Converter<TElement, TBy>, equater?: Equater<TBy>): Enumerable<TElement>;
|
||||
|
||||
intersect(iterable: Iterable<TElement>, equater?: Equater<TElement>): Enumerable<TElement>;
|
||||
intersectBy<TBy>(iterable: Iterable<TElement>, selector: Converter<TElement, TBy>, equater?: Equater<TBy>): Enumerable<TElement>;
|
||||
|
||||
all(predicate: Predicate<TElement>): boolean;
|
||||
any(predicate: Predicate<TElement>): boolean;
|
||||
any(): boolean;
|
||||
none(predicate: Predicate<TElement>): boolean;
|
||||
none(): boolean;
|
||||
|
||||
skip(n: number): Enumerable<TElement>;
|
||||
skipLast(n: number): Enumerable<TElement>;
|
||||
skipWhile(condition: Predicate<TElement>): Enumerable<TElement>;
|
||||
|
||||
take(n: number): Enumerable<TElement>;
|
||||
takeLast(n: number): Enumerable<TElement>;
|
||||
takeWhile(condition: Predicate<TElement>): Enumerable<TElement>;
|
||||
|
||||
peek(action: Action<TElement>): Enumerable<TElement>;
|
||||
|
||||
forEach(action: Action<TElement>): void;
|
||||
|
||||
zip<TOther>(iterable: Iterable<TOther>): Enumerable<[TElement, TOther]>;
|
||||
|
||||
indexed(): Enumerable<[number, TElement]>;
|
||||
|
||||
reversed(): Enumerable<TElement>;
|
||||
|
||||
chunked(size: number): Enumerable<TElement[]>;
|
||||
|
||||
random(options?: RandomOptions<TElement>): TElement | undefined;
|
||||
|
||||
cached(): Enumerable<TElement>;
|
||||
|
||||
asArray(): TElement[];
|
||||
toArray(): TElement[];
|
||||
toMap<TKey, TValue>(keySelector: Converter<TElement, TKey>, valueSelector: Converter<TElement, TValue>): Map<TKey, TValue>;
|
||||
toSet(): Set<TElement>;
|
||||
toObject<TKey extends PropertyKey, TValue>(keySelector: Converter<TElement, TKey>, valueSelector: Converter<TElement, TValue>): Record<TKey, TValue>;
|
||||
|
||||
collect<TResult>(collector: Collector<TElement, any, TResult>): TResult;
|
||||
}
|
||||
|
||||
export interface GroupedEnumerable<TKey, TElement> extends Enumerable<TElement> {
|
||||
get key(): TKey;
|
||||
}
|
||||
|
||||
export interface OrderedEnumerable<TElement> extends Enumerable<TElement> {
|
||||
get comparer(): Comparer<TElement> | undefined;
|
||||
|
||||
thenSelf(comparer?: Comparer<TElement>): OrderedEnumerable<TElement>;
|
||||
|
||||
thenBy<TBy>(selector: Converter<TElement, TBy>, comparer?: Comparer<TBy>): OrderedEnumerable<TElement>;
|
||||
|
||||
thenSelfDescending(comparer?: Comparer<TElement>): OrderedEnumerable<TElement>;
|
||||
|
||||
thenByDescending<TBy>(selector: Converter<TElement, TBy>, comparer?: Comparer<TBy>): OrderedEnumerable<TElement>;
|
||||
}
|
||||
Reference in New Issue
Block a user