collectype / factory/objects/objectProperty / objectPropertyFactory
objectPropertyFactory<
T,C>(ctx,oper): <K>(field,key) =>C
Defined in: factory/objects/objectProperty.ts:40
Creates a predicate filter for object property presence using PredicType.object.property.
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.
ObjectPropertyOper
The property operation to perform (see PredicType.object.property).
Returns a function that takes a field (of type object on T) and a property key, and applies the object property predicate to filter the context.
<
K>(field,key):C
K extends string | number | symbol
K
string |
symbol |
C
// Example: Composing an object property filter as a property, homogeneous model
import { BaseFunctions } from 'collectype';
import { objectPropertyFactory } from 'collectype';
type Person = { name: string; meta: { a?: number; b?: number; city?: string } };
class PersonFunctions extends BaseFunctions<Person> {
objectHasProperty = objectPropertyFactory<Person, this>(this, 'has');
}
// Usage:
const people: Person[] = [
{ name: 'Alice', meta: { a: 1, b: 2 } },
{ name: 'Bob', meta: { a: 1 } },
{ name: 'Eve', meta: { b: 2 } }
];
const fn = new PersonFunctions(people);
const filtered = fn.objectHasProperty('meta', 'a');
// filtered contains the items where 'meta' has the property 'a'
PredicType.object.property for property checks on object fields.