Skip to content

tyneq


tyneq / operator

Function: operator()

operator<TArgs>(name, category, validate?): <TClass>(target, _context) => TClass

Defined in: src/plugin/decorators/operator.ts:40

Class decorator that registers a TyneqEnumerator subclass as an operator on every sequence.

Validation runs eagerly at the call site, before the lazy enumerator is created.

Type Parameters

Type ParameterDefault type
TArgs extends unknown[]never

Parameters

ParameterTypeDescription
namestringMethod name to expose on every sequence.
category"streaming" | "buffer"Operator kind ("streaming"
validate?(...args) => voidOptional eager validation function for user-supplied arguments.

Returns

<TClass>(target, _context): TClass

Type Parameters

Type Parameter
TClass extends (...args) => any

Parameters

ParameterType
targetTClass
_contextClassDecoratorContext

Returns

TClass

Example

ts
import { operator, TyneqEnumerator } from "tyneq/plugin";
import type { Enumerator } from "tyneq";

@operator<[predicate: (item: unknown) => boolean]>("myFilter", "streaming", (predicate) => {
    if (typeof predicate !== "function") throw new Error("predicate must be a function");
})
class MyFilterEnumerator<T> extends TyneqEnumerator<T> {
    public constructor(source: Enumerator<T>, private readonly predicate: (item: T) => boolean) {
        super(source);
    }
    protected handleNext(): IteratorResult<T> {
        while (true) {
            const next = this.sourceEnumerator.next();
            if (next.done || this.predicate(next.value)) return next;
        }
    }
}