collectype / factory/arrays/arrayIndexComparison / arrayIndexComparisonFactory
arrayIndexComparisonFactory<
T,C>(ctx,oper): <K>(field,index,target) =>C
Defined in: factory/arrays/arrayIndexComparison.ts:40
Factory for creating array index comparison predicates for use with a Wherable context.
T
The item type in the collection.
C extends Wherable<T, C>
The Wherable context type (must extend Wherable<T, C>).
C
The context (usually a collection) supporting the where method.
ArrayIndexComparisonOper
The array index comparison operation (e.g., ‘equals’, ‘greaterThan’, etc.).
Returns a function that takes a field (of type array on T), an index, and a target value, and applies the array index comparison predicate to filter the context.
<
K>(field,index,target):C
K extends string | number | symbol
K
number
unknown
C
// Example: Composing an array index comparison filter as a property, following README Example 5
import { BaseFunctions } from 'collectype';
import { arrayIndexComparisonFactory } from 'collectype';
type Person = { name: string; scores: number[] };
class PersonFunctions extends BaseFunctions<Person> {
arrayIndexEquals = arrayIndexComparisonFactory<Person, this>(this, 'equals');
}
// Usage:
const people: Person[] = [
{ name: 'Alice', scores: [10, 20, 30] },
{ name: 'Bob', scores: [30, 40] }
];
const fn = new PersonFunctions(people);
const filtered = fn.arrayIndexEquals('scores', 0, 10);
// filtered contient les éléments dont scores[0] === 10