Skip to content

Order

This module provides an implementation of the Order type class which is used to define a total ordering on some type A. An order is defined by a relation <=, which obeys the following laws:

- either x <= y or y <= x (totality) - if x <= y and y <= x, then x == y (antisymmetry) - if x <= y and y <= z, then x <= z (transitivity)

The truth table for compare is defined as follows:

| x <= y | x >= y | Ordering | | | -------- | -------- | -------- | --------------------- | | true | true | 0 | corresponds to x == y | | true | false | < 0 | corresponds to x < y | | false | true | > 0 | corresponds to x > y |

28 exports Added in v2.0.0 Source

Combinators

array

Added in v2.0.0 Source

This function creates and returns a new Order for an array of values based on a given Order for the elements of the array. The returned Order compares two arrays by applying the given Order to each element in the arrays. If all elements are equal, the arrays are then compared based on their length. It is useful when you need to compare two arrays of the same type and you have a specific way of comparing each element of the array.

Signature

declare function array<A>(O: Order<A>): Order<readonly Array<A>>

struct

Added in v2.0.0 Source

This function creates and returns a new Order for a struct of values based on the given Orders for each property in the struct.

Signature

declare function struct<
  R extends {
    [x: string]: Order<any>;
  },
>(fields: R): Order<{ [K in string | number | symbol]: [R[K]] extends [Order<A>] ? A : never }>;

tuple

Added in v2.0.0 Source

Similar to Promise.all but operates on Orders.

This function creates and returns a new Order for a tuple of values based on the given Orders for each element in the tuple. The returned Order compares two tuples of the same type by applying the corresponding Order to each element in the tuple. It is useful when you need to compare two tuples of the same type and you have a specific way of comparing each element of the tuple.

Signature

declare function tuple<T extends readonly Array<Order<any>>>(...elements: T): Order<Readonly<{ [I in string | number | symbol]: [T[I]] extends [Order<A>] ? A : never }>>

Combining

all

Added in v2.0.0 Source

Signature

declare function all<A>(collection: Iterable<Order<A>>): Order<readonly Array<A>>

combine

Added in v2.0.0 Source

Signature

declare const combine: {
  <A>(that: Order<A>): (self: Order<A>) => Order<A>;
  <A>(self: Order<A>, that: Order<A>): Order<A>;
};

combineAll

Added in v2.0.0 Source

Signature

declare function combineAll<A>(collection: Iterable<Order<A>>): Order<A>;

combineMany

Added in v2.0.0 Source

Signature

declare const combineMany: {
  <A>(collection: Iterable<Order<A>>): (self: Order<A>) => Order<A>;
  <A>(self: Order<A>, collection: Iterable<Order<A>>): Order<A>;
};

product

Added in v2.0.0 Source

Signature

declare const product: {
  <B>(that: Order<B>): <A>(self: Order<A>) => Order<readonly [A, B]>;
  <A, B>(self: Order<A>, that: Order<B>): Order<readonly [A, B]>;
};

productMany

Added in v2.0.0 Source

Signature

declare const productMany: {
  <A>(collection: Iterable<Order<A>>): (self: Order<A>) => Order<readonly [A, A]>;
  <A>(self: Order<A>, collection: Iterable<Order<A>>): Order<readonly [A, A]>;
};

Constructors

make

Added in v2.0.0 Source

Signature

declare function make<A>(compare: (self: A, that: A) => -1 | 0 | 1): Order<A>;

Instances

bigint

Added in v2.0.0 Source

Signature

declare const bigint: Order<bigint>;

boolean

Added in v2.0.0 Source

Signature

declare const boolean: Order<boolean>;

Date

Added in v2.0.0 Source

Signature

declare const Date: Order<Date>;

number

Added in v2.0.0 Source

Signature

declare const number: Order<number>;

string

Added in v2.0.0 Source

Signature

declare const string: Order<string>;

Mapping

mapInput

Added in v2.0.0 Source

Signature

declare const mapInput: {
  <B, A>(f: (b: B) => A): (self: Order<A>) => Order<B>;
  <A, B>(self: Order<A>, f: (b: B) => A): Order<B>;
};

Other

between

Added in v2.0.0 Source

Test whether a value is between a minimum and a maximum (inclusive).

Signature

declare function between<A>(O: Order<A>): {
  (options: { maximum: A; minimum: A }): (self: A) => boolean;
  (
    self: A,
    options: {
      maximum: A;
      minimum: A;
    },
  ): boolean;
};

clamp

Added in v2.0.0 Source

Clamp a value between a minimum and a maximum.

Signature

declare function clamp<A>(O: Order<A>): {
  (options: { maximum: A; minimum: A }): (self: A) => A;
  (
    self: A,
    options: {
      maximum: A;
      minimum: A;
    },
  ): A;
};

empty

Added in v2.0.0 Source

Signature

declare function empty<A>(): Order<A>;

greaterThan

Added in v2.0.0 Source

Test whether one value is _strictly greater than_ another.

Signature

declare function greaterThan<A>(O: Order<A>): {
  (that: A): (self: A) => boolean;
  (self: A, that: A): boolean;
};

Test whether one value is _non-strictly greater than_ another.

Signature

declare function greaterThanOrEqualTo<A>(O: Order<A>): {
  (that: A): (self: A) => boolean;
  (self: A, that: A): boolean;
};

lessThan

Added in v2.0.0 Source

Test whether one value is _strictly less than_ another.

Signature

declare function lessThan<A>(O: Order<A>): {
  (that: A): (self: A) => boolean;
  (self: A, that: A): boolean;
};

Test whether one value is _non-strictly less than_ another.

Signature

declare function lessThanOrEqualTo<A>(O: Order<A>): {
  (that: A): (self: A) => boolean;
  (self: A, that: A): boolean;
};

max

Added in v2.0.0 Source

Take the maximum of two values. If they are considered equal, the first argument is chosen.

Signature

declare function max<A>(O: Order<A>): {
  (that: A): (self: A) => A;
  (self: A, that: A): A;
};

min

Added in v2.0.0 Source

Take the minimum of two values. If they are considered equal, the first argument is chosen.

Signature

declare function min<A>(O: Order<A>): {
  (that: A): (self: A) => A;
  (self: A, that: A): A;
};

reverse

Added in v2.0.0 Source

Signature

declare function reverse<A>(O: Order<A>): Order<A>;

Type Class

Order interface

Added in v2.0.0 Source

Signature

interface Order<in A> {
  (self: A, that: A): -1 | 0 | 1;
}

Type Lambdas

OrderTypeLambda interface

Added in v2.0.0 Source

Signature

interface OrderTypeLambda extends TypeLambda {
  readonly type: Order<unknown>;
}