Interface ComparatorWithPredicateFactory<T>

A factory that holds a list of comparators and attached predicates. Use the orIf method to push additional comparators with predicates, thus broadening the range of accepted values and types.

orElse and orElseThrow will produce the final Comparator, that will only accept values which match one of the added predicates. Predicates will be checked in the same order as they were added.

On each comparator call both arguments must match the same predicate. If no such predicate exists, the comparator will either throw an error or use a fallback comparator, depending on whether you used orElseThrow or orElse respectively.

Use type guards as predicates to ensure the best type safety.

Example

// inferred type: Comparator<string | number>
const unionComparator = comparatorWithPredicate(isString, ignoreCase)
.orIf(isNumber, reversedOrder)
.orElseThrow();

// set type explicitly when not using a proper type guard; otherwise any will be inferred
// inferred type: Comparator<string | number | boolean | Person>
const anotherUnionComparator = comparatorWithPredicate(isPerson, personComparator)
.orIf<string | number | boolean>(isPrimitive, naturalOrder)
.orElseThrow();

// not provided by this library
declare const isString = (value: any) => value is string;
declare const isNumber = (value: any) => value is number;
declare const isPerson = (value: any) => value is Person;
declare const isPrimitive = (value: any) => boolean;

Type Parameters

  • T

Hierarchy

  • ComparatorWithPredicateFactory

Methods

  • Add another predicate and comparator.

    See

    ComparatorWithPredicateFactory

    Deprecated

    use the identical orIf method instead. add will be removed in a future release.

    Type Parameters

    Type Parameters

    • U

    Parameters

    • predicate: ((value: any) => value is U) | ((value: U) => boolean)

      a predicate that indicates if the following comparator can handle the value

    • comparator: Comparator<U>

      a comparator for the incoming value

    Returns ComparatorWithPredicateFactory<T | U>

  • Creates a single comparator that will only accept values, which both match one of the attached predicates.

    See

    ComparatorWithPredicateFactory

    Type Parameters

    Type Parameters

    • U

    Parameters

    Returns Comparator<T | U>

  • Creates a single comparator that will only accept values, which both match one of the attached predicates.

    See

    ComparatorWithPredicateFactory

    Returns Comparator<T>

  • Add another predicate and comparator.

    See

    ComparatorWithPredicateFactory

    Type Parameters

    Type Parameters

    • U

    Parameters

    • predicate: ((value: any) => value is U) | ((value: U) => boolean)

      a predicate that indicates if the following comparator can handle the value

    • comparator: Comparator<U>

      a comparator for the incoming value

    Returns ComparatorWithPredicateFactory<T | U>

  • Creates a single comparator that will only accept values, which both match one of the attached predicates.

    See

    ComparatorWithPredicateFactory

    Deprecated

    use the identical orElseThrow method instead. toComparator will be removed in a future release.

    Returns Comparator<T>

Generated using TypeDoc