collectype / factory/objects/objectAttributes / objectAttributesFactory
objectAttributesFactory<
T,C>(ctx,oper): <K>(field,target) =>C
Defined in: factory/objects/objectAttributes.ts:40
Creates a predicate filter for object attribute checks using PredicType.object.attributes.
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.
ObjectAttributesOper
The attribute operation to perform (see PredicType.object.attributes).
Returns a function that takes a field (of type object on T) and a target value, and applies the object attribute predicate to filter the context.
<
K>(field,target):C
K extends string | number | symbol
K
string |
symbol |
C
// Example: Composing an object attribute filter as a property, homogeneous model
import { BaseFunctions } from 'collectype';
import { objectAttributesFactory } from 'collectype';
type Person = { name: string; meta: { a?: number; b?: number; city?: string } };
class PersonFunctions extends BaseFunctions<Person> {
objectHasAttribute = objectAttributesFactory<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.objectHasAttribute('meta', 'a');
// filtered contains the items where 'meta' has the key 'a'
PredicType.object.attributes for attribute checks on object fields.