Compare
sourceFunctions for comparing equality and ordering of values of SDL-supported types.
Constructors
new Compare(): Compare
Properties
collator: 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
equalityOfAny<T>(lhs: T, rhs: T): booleansource
Compares two values using the === operator.
equalityOfArrays<T>(compareElements: EqCompareFn): Functionsource
Compares two Arrays using the given element comparison function.
equalityOfDates(lhs: PlainDate, rhs: PlainDate): booleansource
Compares two Temporal.PlainDates.
equalityOfDecimals(lhs: Decimal, rhs: Decimal): booleansource
Compares two Decimals.
equalityOfMaps<K, V>(compareValues: EqCompareFn): Functionsource
Compares two Maps using the given value comparison function.
equalityOfOptionals<T>(compare: EqCompareFn): Functionsource
Wraps a comparison function, avoiding invoking it if either value is missing.
equalityOfSets<T>(compareElements: EqCompareFn): Functionsource
Compares two Sets using the given element comparison function.
equalityOfStrings(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.
equalityOfTimestamps(lhs: Instant, rhs: Instant): booleansource
Compares two Temporal.Instants.
equalityOfTuples<Ts extends any[]>(compare: EqCompareTuple<Ts>): Functionsource
Compares two Arrays of fixed size using the given set of comparison functions.
equalityOfUnions<T extends TypeMap>(compareMap: EqCompareMap): Functionsource
Compares two values of the same sum type.
equalityOfYearMonths(lhs: PlainYearMonth, rhs: PlainYearMonth): booleansource
Compares two Temporal.PlainYearMonths.
equalityUsingMetadata<T>(metadata: ReflectionMetadata): Functionsource
Returns a function that compares two instances of a type using the given reflection metadata.
equalityWithOptionalRightHandSide<T>(compare: EqCompareFn): Functionsource
Wraps a comparison function, avoiding invoking it if the right-hand value is missing.
orderOfArrays<T>(compare: OrdCompareFn): Functionsource
Orders two Arrays using the given element ordering function.
orderOfBooleans(lhs: boolean, rhs: boolean): Orderingsource
Orders two booleans. false is ordered before true;
orderOfDecimals(lhs: Decimal, rhs: Decimal): Orderingsource
Orders two Decimalss.
orderOfEnums<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.
orderOfMaps(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.
orderOfNumbers(lhs: number, rhs: number): Orderingsource
Orders two numbers.
orderOfObjects(_lhs: object, _rhs: object): Orderingsource
Orders two arbitrarily-shaped objects. This just returns equal ordering for any input, since no proper comparison is possible.
orderOfOptionals<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.
orderOfSets<T>(compare: OrdCompareFn): Functionsource
Orders two Sets using the given element ordering function.
orderOfStrings(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.
orderOfTuples<Ts extends any[]>(compare: OrdCompareTuple<Ts>): OrdCompareFnsource
Orders two Arrays of fixed size using the given set of ordering functions.
orderOfUnions<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.
orderUsingMetadata<T>(metadata: ReflectionMetadata): OrdCompareFnsource
Returns a function that orders two instances of a type using the given reflection metadata.