Compare

source

Functions for comparing equality and ordering of values of SDL-supported types.

Constructors

new Compare(): Compare

Properties

readonlystaticcollator: Collatorsource

The collator used for comparing equality and order of strings.

Note: This is fixed to an en-US locale. To be technically correct, we should sort using the user's chosen (client-side) locale. This would add significant complexity: the locale would need to be sent from the frontend to the backend, and the locale would need to form part of the hash of filter/sort predicates, since two views filtered or sorted by the same field would produce different results in different locales.

For now, we'll just use en-US comparison and ordering since that should be good enough for most of our purposes.

Methods

Static methods

staticequalityOfAny<T>(lhs: T, rhs: T): booleansource

Compares two values using the === operator.

staticequalityOfArrays<T>(compareElements: EqCompareFn): Functionsource

Compares two Arrays using the given element comparison function.

staticequalityOfDates(lhs: PlainDate, rhs: PlainDate): booleansource

Compares two Temporal.PlainDates.

staticequalityOfDecimals(lhs: Decimal, rhs: Decimal): booleansource

Compares two Decimals.

staticequalityOfMaps<K, V>(compareValues: EqCompareFn): Functionsource

Compares two Maps using the given value comparison function.

staticequalityOfOptionals<T>(compare: EqCompareFn): Functionsource

Wraps a comparison function, avoiding invoking it if either value is missing.

staticequalityOfSets<T>(compareElements: EqCompareFn): Functionsource

Compares two Sets using the given element comparison function.

staticequalityOfStrings(lhs: string, rhs: string): booleansource

Compares two strings using the system (en-US) locale.

Note: See notes on collator for more details on the technical compromise here.

staticequalityOfTimestamps(lhs: Instant, rhs: Instant): booleansource

Compares two Temporal.Instants.

staticequalityOfTuples<Ts extends any[]>(compare: EqCompareTuple<Ts>): Functionsource

Compares two Arrays of fixed size using the given set of comparison functions.

staticequalityOfUnions<T extends TypeMap>(compareMap: EqCompareMap): Functionsource

Compares two values of the same sum type.

staticequalityOfYearMonths(lhs: PlainYearMonth, rhs: PlainYearMonth): booleansource

Compares two Temporal.PlainYearMonths.

staticequalityUsingMetadata<T>(metadata: ReflectionMetadata): Functionsource

Returns a function that compares two instances of a type using the given reflection metadata.

staticequalityWithOptionalRightHandSide<T>(compare: EqCompareFn): Functionsource

Wraps a comparison function, avoiding invoking it if the right-hand value is missing.

staticorderOfArrays<T>(compare: OrdCompareFn): Functionsource

Orders two Arrays using the given element ordering function.

staticorderOfBooleans(lhs: boolean, rhs: boolean): Orderingsource

Orders two booleans. false is ordered before true;

staticorderOfDecimals(lhs: Decimal, rhs: Decimal): Orderingsource

Orders two Decimalss.

staticorderOfEnums<T extends EnumDomain>(domain: T): OrdCompareFnsource

Orders two enums, using the lexical ordering of the enumeration keys.

Note: To be technically correct here, we should sort based on the translated values of the enum, which means that the ordering would depend on the language chosen by the user. This adds significant complexity: see the notes on collator for more thoughts.

For now, we'll just order by the canonical key (as it's defined in the model), with the understanding that the order presented in the UI might look nonsensical to an end-user.

staticorderOfMaps(lhs: Map<any, any>, rhs: Map<any, any>): Orderingsource

Orders two Maps.

Note: No comparison is attempted on the values of the map. The map with fewer elements is sorted before the one with more entries.

staticorderOfNumbers(lhs: number, rhs: number): Orderingsource

Orders two numbers.

staticorderOfObjects(_lhs: object, _rhs: object): Orderingsource

Orders two arbitrarily-shaped objects. This just returns equal ordering for any input, since no proper comparison is possible.

staticorderOfOptionals<T>(compare: OrdCompareFn): OrdCompareFnsource

Wraps an ordering function, avoiding invoking it if either value is missing. The ordering produced is:

  • If both LHS and RHS are missing, the values are equal;
  • If one side is missing, the empty value is ordered last;
  • If both sides are present, the ordering function is invoked.

staticorderOfSets<T>(compare: OrdCompareFn): Functionsource

Orders two Sets using the given element ordering function.

staticorderOfStrings(lhs: string, rhs: string): Orderingsource

Orders two strings using the system (en-US) locale.

Note: See notes on collator for more details on the technical compromise here.

staticorderOfTuples<Ts extends any[]>(compare: OrdCompareTuple<Ts>): OrdCompareFnsource

Orders two Arrays of fixed size using the given set of ordering functions.

staticorderOfUnions<T extends TypeMap>(orderMap: OrdCompareMap): OrdCompareFnsource

Orders two values of the same sum type, using the given map of tag to comparison function.

There's no objectively correct way to do this, since no ordering relation exists across all possible types. If the two values are of the same type, we'll use the ordering function defined for that type; if not, we'll fall back to lexical ordering based on the result of 'typeof' for each values.

staticorderUsingMetadata<T>(metadata: ReflectionMetadata): OrdCompareFnsource

Returns a function that orders two instances of a type using the given reflection metadata.