collectype

collectype v0.11.0


collectype / factory/objects/objectPrototypeState / objectPrototypeStateFactory

Function: objectPrototypeStateFactory()

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

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

Creates a predicate filter for object prototype state using PredicType.object.prototypeState.

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

ObjectPrototypeStateOper

The prototype state operation to perform (see PredicType.object.prototypeState).

Returns

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

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

class PersonFunctions extends BaseFunctions<Person> {
  objectHasNullPrototype = objectPrototypeStateFactory<Person, this>(this, 'isNull');
}

// Usage:
const people: Person[] = [
  { name: 'Alice', meta: Object.create(null) },
  { name: 'Bob', meta: {} },
  { name: 'Eve', meta: [] }
];
const fn = new PersonFunctions(people);
const filtered = fn.objectHasNullPrototype('meta');
// filtered contains the items where 'meta' has a null prototype

Remarks