Skip to main content Link Search Menu Expand Document (external link)

Order.ts overview

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

Since v2.0.0


Exports Grouped by Category


combinators

array

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 const array: <A>(O: Order<A>) => Order<ReadonlyArray<A>>

Source

Since v2.0.0

struct

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 const struct: <R extends { readonly [x: string]: Order<any> }>(
  fields: R
) => Order<{ [K in keyof R]: [R[K]] extends [Order<infer A>] ? A : never }>

Source

Since v2.0.0

tuple

Similar to Promise.all but operates on Orders.

[Order<A>, Order<B>, ...] -> Order<[A, B, ...]>

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 const tuple: <T extends ReadonlyArray<Order<any>>>(
  ...elements: T
) => Order<Readonly<{ [I in keyof T]: [T[I]] extends [Order<infer A>] ? A : never }>>

Source

Since v2.0.0

combining

all

Signature

declare const all: <A>(collection: Iterable<Order<A>>) => Order<ReadonlyArray<A>>

Source

Since v2.0.0

combine

Signature

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

Source

Since v2.0.0

combineAll

Signature

declare const combineAll: <A>(collection: Iterable<Order<A>>) => Order<A>

Source

Since v2.0.0

combineMany

Signature

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

Source

Since v2.0.0

product

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]>
}

Source

Since v2.0.0

productMany

Signature

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

Source

Since v2.0.0

constructors

make

Signature

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

Source

Since v2.0.0

instances

Date

Signature

declare const Date: Order<Date>

Source

Since v2.0.0

bigint

Signature

declare const bigint: Order<bigint>

Source

Since v2.0.0

boolean

Signature

declare const boolean: Order<boolean>

Source

Since v2.0.0

number

Signature

declare const number: Order<number>

Source

Since v2.0.0

string

Signature

declare const string: Order<string>

Source

Since v2.0.0

mapping

mapInput

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>
}

Source

Since v2.0.0

type class

Order (interface)

Signature

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

Source

Since v2.0.0

type lambdas

OrderTypeLambda (interface)

Signature

export interface OrderTypeLambda extends TypeLambda {
  readonly type: Order<this["Target"]>
}

Source

Since v2.0.0

utils

between

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

Signature

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

Source

Since v2.0.0

clamp

Clamp a value between a minimum and a maximum.

Example

import * as assert from "node:assert"
import { Order, Number } from "effect"

const clamp = Order.clamp(Number.Order)({ minimum: 1, maximum: 5 })

assert.equal(clamp(3), 3)
assert.equal(clamp(0), 1)
assert.equal(clamp(6), 5)

Signature

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

Source

Since v2.0.0

empty

Signature

declare const empty: <A>() => Order<A>

Source

Since v2.0.0

greaterThan

Test whether one value is strictly greater than another.

Signature

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

Source

Since v2.0.0

greaterThanOrEqualTo

Test whether one value is non-strictly greater than another.

Signature

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

Source

Since v2.0.0

lessThan

Test whether one value is strictly less than another.

Signature

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

Source

Since v2.0.0

lessThanOrEqualTo

Test whether one value is non-strictly less than another.

Signature

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

Source

Since v2.0.0

max

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

Signature

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

Source

Since v2.0.0

min

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

Signature

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

Source

Since v2.0.0

reverse

Signature

declare const reverse: <A>(O: Order<A>) => Order<A>

Source

Since v2.0.0