Skip to main content

Ordering

A collection of functions to manipulate ordering values

Index

Codec

__decode__

  • Returns the decoded input, Result.Ok or Result.Error()

    @example
    interface SomeObject {
    foo: string
    }
    const someCodec: Codec<SomeObject> = ...;
    const input: unknown = ...;
    const decoded = Codec.decode(someCodec, input);

__encode__

  • __encode__(this: void, input: -1 | 0 | 1): unknown
  • Returns the encoded input

    @example
    interface SomeObject {
    foo: string
    }
    const someCodec: Codec<SomeObject> = ...;
    const someObject: SomeObject = { foo: "bar" }
    const encoded = Codec.decode(someCodec, someObject);

__schema__

  • Returns the JSONSchema corresponding to the decoded type

    @example
    const someCodec: Codec<unknown> = ...;
    const jsonSchema = Codec.schema(someCodec);

Formatting

asString

  • asString(this: void, self: -1 | 0 | 1): string
  • Converts the given value to a String.

    @example
    Number.asString(123); // '123'

Indexable

indexType

indexType: number

Index type

at

  • at(index: number): Option<-1 | 0 | 1>
  • Returns the value at the index

indexOf

  • indexOf(value: -1 | 0 | 1): Option<number>
  • Returns the integer index of a value

range

  • range(start: -1 | 0 | 1, end: -1 | 0 | 1): Range<-1 | 0 | 1>
  • Returns an Iterable starting from start to end. If start or end is not in range then returns an empty iterable.

rangeSize

  • rangeSize(start: -1 | 0 | 1, end: -1 | 0 | 1): number
  • Returns the size of a range. If start or end is not in range then returns 0.

Type

__inspect__

__inspect__: Option<(anyValue: -1 | 0 | 1, depth: number, options: InspectOptions, inspect: InspectFunction) => string>

When defined, returns a custom string representation. To be useful, it should be bound to a prototype (ex: Struct)

@example
import { inspect } from 'node:util';

interface Foo {
foo: boolean;
}
const Foo = Struct.define<Foo>({
typeName: 'Foo',
__inspect__: (self) => `Foo { ${String(self.foo)} }`,
});
const myStruct = Struct.create(Foo, { foo: true });// 'Foo { true }'
inspect(myStruct);// 'Foo { true }'
@param

typeName

typeName: Ordering = 'Ordering'

Type string representation

@example
StringType.typeName // 'String'
Int.typeName // 'Int'
Person.typeName // 'Person'

asInstance

  • asInstance(anyValue: unknown): Option<-1 | 0 | 1>
  • Try to convert anyValue to enum value or else returns Option.None

    @example
    const StringType: Type<string>;
    StringType.asInstance('foo'); // Option.Some('foo')
    StringType.asInstance(12); // Option.None

hasInstance

  • hasInstance(anyValue: unknown): anyValue is -1 | 0 | 1
  • Return true if the given value is an instance of the class.

    @example
    const StringType: Type<string>;
    StringType.hasInstance('foo'); // true
    StringType.hasInstance(42); // false

Other

Equal

Equal: 0 = 0

An ordering where a compared value is equal to another.

Greater

Greater: 1 = 1

An ordering where a compared value is greater than another.

Less

Less: -1 = -1

An ordering where a compared value is less than another.

readonly[enumKeys]

[enumKeys]: readonly (Less | Equal | Greater)[]

An array of all keys

reverse

  • Reverses the Ordering.

    • Less becomes Greater.
    • Greater becomes Less.
    • Equal becomes Equal.
    @example
    reverse(Ordering.Less); // == Ordering.Greater
    reverse(Ordering.Greater); // == Ordering.Less
    reverse(Ordering.Equal); // == Ordering.Equal