collectype

collectype v0.11.0


collectype / factory/objects/objectAttributes / objectAttributesFactory

Function: 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.

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

ObjectAttributesOper

The attribute operation to perform (see PredicType.object.attributes).

Returns

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

Type Parameters

K

K extends string | number | symbol

Parameters

field

K

target

string symbol

Returns

C

Example

// 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'

Remarks