collectype

collectype v0.11.0


collectype / factory/objects/objectInstanceType / objectInstanceTypeFactory

Function: objectInstanceTypeFactory()

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

Defined in: factory/objects/objectInstanceType.ts:40

Creates a predicate filter for object instance types using PredicType.object.instanceType.

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

ObjectInstanceTypeOper

The instance type operation to perform (see PredicType.object.instanceType).

Returns

Returns a function that takes a field (of type object on T) and applies the object instance type predicate to filter the context.

<K>(field): C

Type Parameters

K

K extends string | number | symbol

Parameters

field

K

Returns

C

Example

// Example: Composing an object instance type filter as a property, homogeneous model
import { BaseFunctions } from 'collectype';
import { objectInstanceTypeFactory } from 'collectype';

type Person = { name: string; meta: object };

class PersonFunctions extends BaseFunctions<Person> {
  objectIsType = objectInstanceTypeFactory<Person, this>(this, 'Date');
}

// Usage:
const people: Person[] = [
  { name: 'Alice', meta: new Date() },
  { name: 'Bob', meta: {} },
  { name: 'Eve', meta: [] }
];
const fn = new PersonFunctions(people);
const filtered = fn.objectIsType('meta');
// filtered contains the items where 'meta' is of type Date

Remarks