collectype

collectype v0.11.0


collectype / factory/arrays/arrayComparison / arrayComparisonFactory

Function: arrayComparisonFactory()

arrayComparisonFactory<T, C>(ctx, oper): <K>(field, target) => C

Defined in: factory/arrays/arrayComparison.ts:40

Creates a predicate filter for array comparison using PredicType.array.comparison.

Type Parameters

T

T

The item type in the collection.

C

C extends Wherable<T, C>

The Wherable context type (must extend Wherable<T, C>).

Parameters

ctx

C

The context (usually a collection) supporting the where method.

oper

ArrayComparisonOper

The array comparison operation (e.g., ‘includes’, ‘some’, ‘every’, etc.).

Returns

Returns a function that takes a field (of type array on T) and a target value, and applies the array comparison predicate to filter the context.

<K>(field, target): C

Type Parameters

K

K extends string | number | symbol

Parameters

field

K

target

unknown[]

Returns

C

Example

// Example: Composing an array comparison filter as a property, homogeneous model
import { BaseFunctions } from 'collectype';
import { arrayComparisonFactory } from 'collectype';

type Person = { name: string; ids: number[] };

class PersonFunctions extends BaseFunctions<Person> {
  arrayIncludes = arrayComparisonFactory<Person, this>(this, 'includes');
}

// Usage:
const people: Person[] = [
  { name: 'Alice', ids: [1, 2, 3] },
  { name: 'Bob', ids: [3, 4] }
];
const fn = new PersonFunctions(people);
const filtered = fn.arrayIncludes('ids', 3);
// filtered contains the items where 'ids' includes 3

Remarks