collectype

collectype v0.11.0


collectype / factory/arrays/arrayRelation / arrayRelationFactory

Function: arrayRelationFactory()

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

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

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

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

ArrayRelationOper

The array relation operation (e.g., ‘subsetOf’, ‘supersetOf’, etc.).

Returns

Returns a function that takes a field (of type array on T) and a target value, and applies the array relation 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 relation filter as a property, homogeneous model
import { BaseFunctions } from 'collectype';
import { arrayRelationFactory } from 'collectype';

type Person = { name: string; tags: string[] };

class PersonFunctions extends BaseFunctions<Person> {
  arraySubsetOf = arrayRelationFactory<Person, this>(this, 'subsetOf');
}

// Usage:
const people: Person[] = [
  { name: 'Alice', tags: ['a', 'b'] },
  { name: 'Bob', tags: ['a', 'b', 'c'] }
];
const fn = new PersonFunctions(people);
const filtered = fn.arraySubsetOf('tags', ['a', 'b']);
// filtered contains the items where 'tags' is a subset of ['a', 'b']

Remarks