sync
This commit is contained in:
219
src/collector/v2/impl.ts
Normal file
219
src/collector/v2/impl.ts
Normal file
@@ -0,0 +1,219 @@
|
||||
// import { Converter } from "../../types.js";
|
||||
// import { CollectorFactory, CollectorInstance } from "./types.js";
|
||||
|
||||
// export class ToArrayCollectorFactory<TElement> implements CollectorFactory<TElement, TElement[]> {
|
||||
// initialize() {
|
||||
// return new ToArrayCollectorInstance<TElement>();
|
||||
// }
|
||||
// };
|
||||
|
||||
// class ToArrayCollectorInstance<TElement> implements CollectorInstance<TElement, TElement[]> {
|
||||
// readonly #accumulator: TElement[];
|
||||
|
||||
// constructor() {
|
||||
// this.#accumulator = [];
|
||||
// }
|
||||
|
||||
// accumulate(element: TElement) {
|
||||
// this.#accumulator.push(element);
|
||||
// }
|
||||
|
||||
// finalize() {
|
||||
// return this.#accumulator;
|
||||
// }
|
||||
// }
|
||||
|
||||
// export class ToObjectCollectorFactory<TElement, TKey extends PropertyKey, TValue> implements CollectorFactory<TElement, Record<TKey, TValue>> {
|
||||
// readonly #keySelector: Converter<TElement, TKey>;
|
||||
// readonly #valueSelector: Converter<TElement, TValue>;
|
||||
|
||||
// constructor(keySelector: Converter<TElement, TKey>, valueSelector: Converter<TElement, TValue>) {
|
||||
// this.#keySelector = keySelector;
|
||||
// this.#valueSelector = valueSelector;
|
||||
// }
|
||||
|
||||
// get keySelector() {
|
||||
// return this.#keySelector;
|
||||
// }
|
||||
|
||||
// get valueSelector() {
|
||||
// return this.#valueSelector;
|
||||
// }
|
||||
|
||||
// initialize() {
|
||||
// return new ToObjectCollectorInstance<TElement, TKey, TValue>(this);
|
||||
// }
|
||||
// }
|
||||
|
||||
// class ToObjectCollectorInstance<TElement, TKey extends PropertyKey, TValue> implements CollectorInstance<TElement, Record<TKey, TValue>> {
|
||||
// readonly #factory: ToObjectCollectorFactory<TElement, TKey, TValue>;
|
||||
// readonly #accumulator = <Record<TKey, TValue>>{};
|
||||
|
||||
// constructor(factory: ToObjectCollectorFactory<TElement, TKey, TValue>) {
|
||||
// this.#factory = factory;
|
||||
// }
|
||||
|
||||
// accumulate(element: TElement) {
|
||||
// const key = this.#factory.keySelector(element);
|
||||
// const value = this.#factory.valueSelector(element);
|
||||
|
||||
// this.#accumulator[key] = value;
|
||||
// }
|
||||
|
||||
// finalize() {
|
||||
// return this.#accumulator;
|
||||
// }
|
||||
// }
|
||||
|
||||
// export class ToMapCollectorFactory<TElement, TKey extends PropertyKey, TValue> implements CollectorFactory<TElement, Map<TKey, TValue>> {
|
||||
// readonly #keySelector: Converter<TElement, TKey>;
|
||||
// readonly #valueSelector: Converter<TElement, TValue>;
|
||||
|
||||
// constructor(keySelector: Converter<TElement, TKey>, valueSelector: Converter<TElement, TValue>) {
|
||||
// this.#keySelector = keySelector;
|
||||
// this.#valueSelector = valueSelector;
|
||||
// }
|
||||
|
||||
// get keySelector() {
|
||||
// return this.#keySelector;
|
||||
// }
|
||||
|
||||
// get valueSelector() {
|
||||
// return this.#valueSelector;
|
||||
// }
|
||||
|
||||
// initialize() {
|
||||
// return new ToMapCollectorInstance<TElement, TKey, TValue>(this);
|
||||
// }
|
||||
// }
|
||||
|
||||
// class ToMapCollectorInstance<TElement, TKey extends PropertyKey, TValue> implements CollectorInstance<TElement, Map<TKey, TValue>> {
|
||||
// readonly #factory: ToMapCollectorFactory<TElement, TKey, TValue>;
|
||||
// readonly #accumulator = new Map<TKey, TValue>();
|
||||
|
||||
// constructor(factory: ToMapCollectorFactory<TElement, TKey, TValue>) {
|
||||
// this.#factory = factory;
|
||||
// }
|
||||
|
||||
// accumulate(element: TElement) {
|
||||
// const key = this.#factory.keySelector(element);
|
||||
// const value = this.#factory.valueSelector(element);
|
||||
|
||||
// this.#accumulator.set(key, value);
|
||||
// }
|
||||
|
||||
// finalize() {
|
||||
// return this.#accumulator;
|
||||
// }
|
||||
// }
|
||||
|
||||
// export class ToSetCollectorFactory<TElement> implements CollectorFactory<TElement, Set<TElement>> {
|
||||
// initialize() {
|
||||
// return new ToSetCollectorInstance<TElement>();
|
||||
// }
|
||||
// }
|
||||
|
||||
// class ToSetCollectorInstance<TElement> implements CollectorInstance<TElement, Set<TElement>> {
|
||||
// readonly #accumulator = new Set<TElement>();
|
||||
|
||||
// accumulate(element: TElement) {
|
||||
// this.#accumulator.add(element);
|
||||
// }
|
||||
|
||||
// finalize() {
|
||||
// return this.#accumulator;
|
||||
// }
|
||||
// }
|
||||
|
||||
// export class JoinCollectorFactory implements CollectorFactory<any, string> {
|
||||
// readonly #delimiter: string;
|
||||
// readonly #prefix: string;
|
||||
// readonly #suffix: string;
|
||||
|
||||
// constructor(delimiter?: string, prefix?: string, suffix?: string) {
|
||||
// this.#delimiter = delimiter ?? "";
|
||||
// this.#prefix = prefix ?? "";
|
||||
// this.#suffix = suffix ?? "";
|
||||
// }
|
||||
|
||||
// accumulate(element: string) {
|
||||
// this.#accumulator.push(element);
|
||||
// }
|
||||
|
||||
// finalize() {
|
||||
// return this.#prefix + this.#accumulator.join(this.#delimiter) + this.#suffix;
|
||||
// }
|
||||
// }
|
||||
|
||||
// class JoinCollectorInstance implements CollectorInstance<any, string> {
|
||||
// readonly #delimiter: string;
|
||||
// readonly #prefix: string;
|
||||
// readonly #suffix: string;
|
||||
// readonly #accumulator: any[] = [];
|
||||
|
||||
// constructor(delimiter: string, prefix: string, suffix: string) {
|
||||
// this.#delimiter = delimiter ?? "";
|
||||
// this.#prefix = prefix ?? "";
|
||||
// this.#suffix = suffix ?? "";
|
||||
// }
|
||||
|
||||
// accumulate(element: string) {
|
||||
// this.#accumulator.push(element);
|
||||
// }
|
||||
|
||||
// finalize() {
|
||||
// return this.#prefix + this.#accumulator.join(this.#delimiter) + this.#suffix;
|
||||
// }
|
||||
// }
|
||||
|
||||
// export class SumCollectorInstance implements CollectorInstance<number, number> {
|
||||
// #accumulator = 0;
|
||||
|
||||
// accumulate(element: number) {
|
||||
// this.#accumulator += element;
|
||||
// }
|
||||
|
||||
// finalize() {
|
||||
// return this.#accumulator;
|
||||
// }
|
||||
// }
|
||||
|
||||
// export class BigIntSumCollectorInstance implements CollectorInstance<bigint, bigint> {
|
||||
// #accumulator = 0n;
|
||||
|
||||
// accumulate(element: bigint) {
|
||||
// this.#accumulator += element;
|
||||
// }
|
||||
|
||||
// finalize() {
|
||||
// return this.#accumulator;
|
||||
// }
|
||||
// }
|
||||
|
||||
// export class AverageCollectorInstance implements CollectorInstance<number, number> {
|
||||
// #count = 0;
|
||||
// #sum = 0;
|
||||
|
||||
// accumulate(element: number) {
|
||||
// this.#count++;
|
||||
// this.#sum += element;
|
||||
// }
|
||||
|
||||
// finalize() {
|
||||
// return this.#count === 0 ? 0 : this.#sum / this.#count;
|
||||
// }
|
||||
// }
|
||||
|
||||
// export class BigIntAverageCollectorInstance implements CollectorInstance<bigint, bigint> {
|
||||
// #count = 0n;
|
||||
// #sum = 0n;
|
||||
|
||||
// accumulate(element: bigint) {
|
||||
// this.#count++;
|
||||
// this.#sum += element;
|
||||
// }
|
||||
|
||||
// finalize() {
|
||||
// return this.#count === 0n ? 0n : this.#sum / this.#count;
|
||||
// }
|
||||
// }
|
||||
57
src/collector/v2/index.ts
Normal file
57
src/collector/v2/index.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
// import { Converter } from "../../types.js";
|
||||
// import { ToArrayCollectorInstance, ToObjectCollectorInstance, ToMapCollectorInstance, ToSetCollectorInstance, JoinCollectorInstance, SumCollectorInstance, BigIntSumCollectorInstance, AverageCollectorInstance, BigIntAverageCollectorInstance, ToArrayCollectorFactory } from "./impl.js";
|
||||
// import { CollectorFactory } from "./types.js";
|
||||
|
||||
// const toArrayCollectorFactory = new ToArrayCollectorFactory<any>();
|
||||
|
||||
// export function toArray<TElement>(): CollectorFactory<TElement, TElement[]> {
|
||||
// return toArrayCollectorFactory;
|
||||
// }
|
||||
|
||||
// export function toObject<TElement, TKey extends PropertyKey, TValue>(keySelector: Converter<TElement, TKey>, valueSelector: Converter<TElement, TValue>): CollectorFactory<TElement, Record<TKey, TValue>> {
|
||||
// return () => toObjectCollectorFactory<TElement, TKey, TValue>(keySelector, valueSelector);
|
||||
// }
|
||||
|
||||
// export function toMap<TElement, TKey, TValue>(keySelector: Converter<TElement, TKey>, valueSelector: Converter<TElement, TValue>): CollectorFactory<TElement, Map<TKey, TValue>> {
|
||||
// return () => new ToMapCollectorInstance<TElement, TKey, TValue>(keySelector, valueSelector);
|
||||
// }
|
||||
|
||||
// export function toSet<TElement>(): CollectorFactory<TElement, Set<TElement>> {
|
||||
// return () => new ToSetCollectorInstance<TElement>();
|
||||
// }
|
||||
|
||||
// export function join(delimiter?: string, prefix?: string, suffix?: string): CollectorFactory<any, string> {
|
||||
// return () => new JoinCollectorInstance(delimiter, prefix, suffix);
|
||||
// }
|
||||
|
||||
// function sumCollectorFactory() {
|
||||
// return new SumCollectorInstance();
|
||||
// }
|
||||
|
||||
// export function sum(): CollectorFactory<number, number> {
|
||||
// return sumCollectorFactory;
|
||||
// }
|
||||
|
||||
// function bigintSumCollectorFactory() {
|
||||
// return new BigIntSumCollectorInstance();
|
||||
// }
|
||||
|
||||
// export function bigintSum(): CollectorFactory<bigint | number, bigint> {
|
||||
// return bigintSumCollectorFactory;
|
||||
// }
|
||||
|
||||
// function averageCollectorFactory() {
|
||||
// return new AverageCollectorInstance();
|
||||
// }
|
||||
|
||||
// export function average(): CollectorFactory<number, number> {
|
||||
// return averageCollectorFactory;
|
||||
// }
|
||||
|
||||
// function bigintAverageCollectorFactory() {
|
||||
// return new BigIntAverageCollectorInstance();
|
||||
// }
|
||||
|
||||
// export function bigintAverage(): CollectorFactory<bigint, bigint> {
|
||||
// return bigintAverageCollectorFactory;
|
||||
// }
|
||||
8
src/collector/v2/types.ts
Normal file
8
src/collector/v2/types.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
// export interface CollectorFactory<TElement, TResult> {
|
||||
// initialize(): CollectorInstance<TElement, TResult>;
|
||||
// }
|
||||
|
||||
// export interface CollectorInstance<TElement, TResult> {
|
||||
// accumulate(element: TElement): void;
|
||||
// finalize(): TResult;
|
||||
// }
|
||||
Reference in New Issue
Block a user