1
0
Files
sequence-js/src/collector/v2/impl.ts
Herve BECHER 0fd0e3bada sync
2025-05-24 12:11:44 +02:00

220 lines
6.0 KiB
TypeScript

// 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;
// }
// }