@luolapeikko/ts-common
    Preparing search index...

    Class RecordPredicate

    The RecordPredicate class provides utility functions for creating predicate functions for record properties.

    v1.1.0

    Index

    Methods

    • Creates a predicate function that checks if a specific property of a record is equal to a given value.

      Type Parameters

      • T extends Record<PropertyKey, any>

        The type of the record (strict overload).

      • K extends string | number | symbol

        The key of the property to check.

      Parameters

      • key: K

        The key of the property to check.

      • value: T[K]

        The value or iterable values to compare against.

      Returns (target: T) => boolean

      A function that takes a record and returns true if the property is equal to the value.

      // Strict object structure
      const isAdmin = RecordPredicate.propEq<User, 'role'>('role', 'admin');
      const admins = users.filter(isAdmin);
      // Loosely typed object
      const isPublished = RecordPredicate.propEq('status', 'published');
      const publishedPosts = posts.filter(isPublished);

      v1.1.0

    • Creates a predicate function that checks if a specific property of a record is equal to a given value.

      Type Parameters

      • V

        The value type of the property (loose overload).

      • K extends PropertyKey

        The key of the property to check.

      Parameters

      • key: K

        The key of the property to check.

      • value: V

        The value or iterable values to compare against.

      Returns (obj: Partial<Record<K, V>>) => boolean

      A function that takes a record and returns true if the property is equal to the value.

      // Strict object structure
      const isAdmin = RecordPredicate.propEq<User, 'role'>('role', 'admin');
      const admins = users.filter(isAdmin);
      // Loosely typed object
      const isPublished = RecordPredicate.propEq('status', 'published');
      const publishedPosts = posts.filter(isPublished);

      v1.1.0

    • Creates a predicate function that checks if a specific property of a record is not equal to a given value.

      Type Parameters

      • T extends Record<PropertyKey, any>

        The type of the record (strict overload).

      • K extends string | number | symbol

        The key of the property to check.

      Parameters

      • key: K

        The key of the property to check.

      • value: T[K]

        The value to compare against.

      Returns (target: T) => boolean

      A function that takes a record and returns true if the property is not equal to the value.

      // Strict object structure
      const isNotAdmin = RecordPredicate.propNotEq<User, 'role'>('role', 'admin');
      const nonAdmins = users.filter(isNotAdmin);
      // Loosely typed object
      const isNotPublished = RecordPredicate.propNotEq('status', 'published');
      const draftsOrArchived = posts.filter(isNotPublished);

      v1.1.0

    • Creates a predicate function that checks if a specific property of a record is not equal to a given value.

      Type Parameters

      • V

        The value type of the property (loose overload).

      • K extends PropertyKey

        The key of the property to check.

      Parameters

      • key: K

        The key of the property to check.

      • value: V

        The value to compare against.

      Returns (obj: Partial<Record<K, V>>) => boolean

      A function that takes a record and returns true if the property is not equal to the value.

      // Strict object structure
      const isNotAdmin = RecordPredicate.propNotEq<User, 'role'>('role', 'admin');
      const nonAdmins = users.filter(isNotAdmin);
      // Loosely typed object
      const isNotPublished = RecordPredicate.propNotEq('status', 'published');
      const draftsOrArchived = posts.filter(isNotPublished);

      v1.1.0