Aller au contenu principal

Int

A collection of functions to manipulate integer values

Index

Bound

readonlymaxValue

maxValue: Int

Maximum value for this type

const anyNumber: number
anyNumber < Number.maxValue // true

readonlyminValue

minValue: Int

Minimum value for this type

const anyNumber: number
anyNumber > Number.minValue // true

Codec

__decode__

  • __decode__(this, input, context): Result<Int, CodecError>
  • Returns the decoded input, Result.Ok or Result.Error()

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

__encode__

  • __encode__(this, input): unknown
  • Returns the encoded input

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

__schema__

  • __schema__(this): JSONValue
  • Returns the JSONSchema corresponding to the decoded type

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

Comparator

!=

  • !=(this, left, right): boolean
  • "Not equal to" operator

    const TEqual: Equal<T>;
    TEqual['!='](value, otherValue); // true
    TEqual['!='](value, value); // false

  • (this, left, right): boolean
  • "Less than" operator

    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['<'](smallerT, smallerT); // false
    TCompare['<'](smallerT, greaterT); // true
    TCompare['<'](greaterT, smallerT); // false

<=

  • <=(this, left, right): boolean
  • "Less than or equal to" operator

    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['<='](smallerT, smallerT); // true
    TCompare['<='](smallerT, greaterT); // true
    TCompare['<='](greaterT, smallerT); // false

==

  • ==(this, left, right): boolean
  • "Equal to" operator

    type T = // ...
    const TEqual: Equal<T>;
    TEqual['=='](value, value); // true
    TEqual['=='](value, otherValue); // false

  • (this, left, right): boolean
  • "Greater than" operator

    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['>'](smallerT, smallerT); // false
    TCompare['>'](smallerT, greaterT); // false
    TCompare['>'](greaterT, smallerT); // true

>=

  • >=(this, left, right): boolean
  • "Greater than or equal to" operator

    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['>='](smallerT, smallerT); // true
    TCompare['>='](smallerT, greaterT); // false
    TCompare['>='](greaterT, smallerT); // true

clamp

  • clamp(this, value, minValue, maxValue): Int
  • Clamp value between minValue and maxValue

    type T;
    const TCompare: Comparable<T>;
    TCompare.clamp(value, min, max); // min if value < min, max if value > max, otherwise value itself

compare

  • compare(this, left, right): Ordering
  • Return an Ordering that represents comparison result

    type T;
    const TCompare: Comparable<T>;
    const sorted = [3, 1, 1].sort(TCompare.compare);

    See more at Order

equals

  • equals(this, left, right): boolean
  • Alias to '=='

    type T = // ...
    const TEqual: Equal<T>;
    TEqual.equals(value, value); // true
    TEqual.equals(value, otherValue); // false

max

  • max(this, left, right): Int
  • "maximum" operator

    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare.max(smallerT, greaterT); // greaterT

min

  • min(this, left, right): Int
  • "minimum" operator

    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare.min(smallerT, greaterT); // smallerT

Constructor

fromNumber

  • fromNumber(value): Option<Int>
  • Return a new integer from value

    const intValue = Int.fromNumber(0.5);// 0

Formatting

asString

  • asString(this, self): string
  • Converts the given value to a String.

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

Indexable

indexType

indexType: number

Index type

at

  • at(index): Option<Int>
  • Returns the value at the index

indexOf

  • indexOf(value): Option<Int>
  • Returns the integer index of a value

range

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

rangeSize

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

Numeric

-

  • -(left, right): Int
  • Subtraction operator

    type T = ...;
    const TNumeric: Numeric.Subtract<T> = ...;
    const result = Numeric['-'](left, right);// represents (left - right)

*

  • *(left, right): Int
  • Multiplication operator

    type T = ...;
    const TNumeric: Numeric.Multiply<T> = ...;
    const result = Numeric['*'](left, right);// represents (left * right)

/

  • /(base, divider): Int
  • Division operator

    type T = ...;
    const TNumeric: Numeric.Divide<T> = ...;
    const result = Numeric['/'](left, right);// represents (left / right)

/%

  • /%(this, base, divider): [quot: Int, mod: Int]
  • Quotient/Modulo operator

    type T = ...;
    const TNumeric: Numeric.Integral<T> = ...;
    const result = Numeric['/%'](left, right);// represents [ left / right, left % right ]

%

  • %(left, right): Int
  • Remainder operator

    type T = ...;
    const TNumeric: Numeric.Remainder<T> = ...;
    const result = Numeric['%'](left, right);// represents (left % right)

+

  • +(left, right): Int
  • Addition operator

    type T = ...;
    const TNumeric: Numeric.Add<T> = ...;
    const result = TNumeric['+'](left, right);// represents (left + right)

abs

  • abs(this, value): Int
  • Absolute value. It should satisfy Numeric['*'](Numeric.abs(x), Numeric.sign(x)) == x

    type T = ...;
    const TSigned: Numeric.Signed<T> = ...;
    const result = TSigned.abs(value);// absolute value of (value)

asInt

  • asInt(this, self): Int
  • Converts the given value to a Int.

    const SomeType: AsInt<T>;
    const value: T;
    SomeType.asInt(value); // Int(...)

isNegative

  • isNegative(this, self): boolean
  • Returns true if the number is negative and false if the number is zero or positive.

    Number.isPositive(-1);// true
    Number.isPositive(0);// false
    Number.isPositive(1);// false

isOne

  • isOne(this, self): boolean
  • Returns true if self is equal to the multiplicative identity.

    Number.isOne(1); // true
    Number.isOne(0); // false

isPositive

  • isPositive(this, self): boolean
  • Returns true if the number is positive and false if the number is zero or negative.

    Number.isPositive(-1);// false
    Number.isPositive(0);// false
    Number.isPositive(1);// true

isZero

  • isZero(this, self): boolean
  • Returns true if self is equal to the additive identity.

    Number.isZero(0); // true
    Number.isZero(1); // false

negate

  • negate(this, self): Int
  • Negates the given value.

    Number.negate(5); // -5
    Number.negate(Number.negate(5)); // 5

one

  • one(this): Int
  • Returns the additive identity element of T, 0.

    Number.one(); // 1

sign

  • sign(this, value): Int
  • Sign of a number. It should satisfy TSigned['*'](TSigned.abs(x), TSigned.sign(x)) == x

    Number.sign(-2.5);// -1
    Number.sign(0);// 0
    Number.sign(2.5);// 1

zero

  • zero(this): Int
  • Returns the additive identity element of T, 0.

    Number.zero(); // 0

Type

__inspect__

__inspect__: Option<(anyValue, depth, options, inspect) => string>

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

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

asInstance

  • asInstance(this, anyValue): Option<Int>
  • Try to convert anyValue to enum value or else returns Option.None

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

hasInstance

  • hasInstance(this, anyValue): anyValue is Int
  • Return true if the given value is an instance of the class.

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

unwrap

  • unwrap(value): number
  • Convert a tagged value to the underlying type

wrap

  • wrap(value): Int
  • Convert an underlying type to a tagged type

Other

typeName

Re-exports typeName

Module

Module<T>:

Type parameters

  • T: number

readonlyinheritedmaxValue

maxValue: T

Maximum value for this type

const anyNumber: number
anyNumber < Number.maxValue // true

readonlyinheritedminValue

minValue: T

Minimum value for this type

const anyNumber: number
anyNumber > Number.minValue // true

inherited-

  • -(left, right): T
  • Subtraction operator

    type T = ...;
    const TNumeric: Numeric.Subtract<T> = ...;
    const result = Numeric['-'](left, right);// represents (left - right)

inherited!=

  • !=(this, left, right): boolean
  • "Not equal to" operator

    const TEqual: Equal<T>;
    TEqual['!='](value, otherValue); // true
    TEqual['!='](value, value); // false

inherited*

  • *(left, right): T
  • Multiplication operator

    type T = ...;
    const TNumeric: Numeric.Multiply<T> = ...;
    const result = Numeric['*'](left, right);// represents (left * right)

inherited**

  • **(left, right): T
  • Power operator

    type T = ...;
    const TNumeric: Numeric.Power<T> = ...;
    const result = Numeric['**'](left, right);// represents (left ** right)

inherited/?

  • /?(left, right): Option<T>
  • Division operator

    type T = ...;
    const TNumeric: Numeric.CheckedDivide<T> = ...;
    const result = Numeric['/?'](left, right);// represents (left / right)

inherited%

  • %(left, right): T
  • Remainder operator

    type T = ...;
    const TNumeric: Numeric.Remainder<T> = ...;
    const result = Numeric['%'](left, right);// represents (left % right)

inherited+

  • +(left, right): T
  • Addition operator

    type T = ...;
    const TNumeric: Numeric.Add<T> = ...;
    const result = TNumeric['+'](left, right);// represents (left + right)

inherited

  • (this, left, right): boolean
  • "Less than" operator

    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['<'](smallerT, smallerT); // false
    TCompare['<'](smallerT, greaterT); // true
    TCompare['<'](greaterT, smallerT); // false

inherited<=

  • <=(this, left, right): boolean
  • "Less than or equal to" operator

    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['<='](smallerT, smallerT); // true
    TCompare['<='](smallerT, greaterT); // true
    TCompare['<='](greaterT, smallerT); // false

inherited==

  • ==(this, left, right): boolean
  • "Equal to" operator

    type T = // ...
    const TEqual: Equal<T>;
    TEqual['=='](value, value); // true
    TEqual['=='](value, otherValue); // false

inherited

  • (this, left, right): boolean
  • "Greater than" operator

    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['>'](smallerT, smallerT); // false
    TCompare['>'](smallerT, greaterT); // false
    TCompare['>'](greaterT, smallerT); // true

inherited>=

  • >=(this, left, right): boolean
  • "Greater than or equal to" operator

    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['>='](smallerT, smallerT); // true
    TCompare['>='](smallerT, greaterT); // false
    TCompare['>='](greaterT, smallerT); // true

inheritedabs

  • abs(this, value): T
  • Absolute value. It should satisfy Numeric['*'](Numeric.abs(x), Numeric.sign(x)) == x

    type T = ...;
    const TSigned: Numeric.Signed<T> = ...;
    const result = TSigned.abs(value);// absolute value of (value)

inheritedclamp

  • clamp(this, value, minValue, maxValue): T
  • Clamp value between minValue and maxValue

    type T;
    const TCompare: Comparable<T>;
    TCompare.clamp(value, min, max); // min if value < min, max if value > max, otherwise value itself

inheritedcompare

  • compare(this, left, right): Ordering
  • Return an Ordering that represents comparison result

    type T;
    const TCompare: Comparable<T>;
    const sorted = [3, 1, 1].sort(TCompare.compare);

    See more at Order

inheritedequals

  • equals(this, left, right): boolean
  • Alias to '=='

    type T = // ...
    const TEqual: Equal<T>;
    TEqual.equals(value, value); // true
    TEqual.equals(value, otherValue); // false

inheritedisNegative

  • isNegative(this, self): boolean
  • Returns true if the number is negative and false if the number is zero or positive.

    Number.isPositive(-1);// true
    Number.isPositive(0);// false
    Number.isPositive(1);// false

inheritedisPositive

  • isPositive(this, self): boolean
  • Returns true if the number is positive and false if the number is zero or negative.

    Number.isPositive(-1);// false
    Number.isPositive(0);// false
    Number.isPositive(1);// true

inheritedisZero

  • isZero(this, self): boolean
  • Returns true if self is equal to the additive identity.

    Number.isZero(0); // true
    Number.isZero(1); // false

inheritedmax

  • max(this, left, right): T
  • "maximum" operator

    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare.max(smallerT, greaterT); // greaterT

inheritedmin

  • min(this, left, right): T
  • "minimum" operator

    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare.min(smallerT, greaterT); // smallerT

inheritednegate

  • negate(this, self): T
  • Negates the given value.

    Number.negate(5); // -5
    Number.negate(Number.negate(5)); // 5

inheritedsign

  • sign(this, value): T
  • Sign of a number. It should satisfy TSigned['*'](TSigned.abs(x), TSigned.sign(x)) == x

    Number.sign(-2.5);// -1
    Number.sign(0);// 0
    Number.sign(2.5);// 1

inheritedzero

  • zero(this): T
  • Returns the additive identity element of T, 0.

    Number.zero(); // 0

__call__

  • __call__(value): Int

format

  • format(intValue, radix): string
  • Return string representation of integer

    Int.format(Int(1), 10);// '1'
    Int.format(Int(10), 16);// 'A'

fromInt

  • fromInt(this, value): Int

parse

  • parse(expression, radix): Option<Int>
  • Parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems). If the expression is not valid, it returns Option.None

    Int.parse('1');// Option.Some(1)
    Int.parse('invalid');// Option.None