Skip to main content

BigInt

A collection of functions to manipulate bigint

@example
import { BigInt } from '@w5s/core';

const total = [1n, 2n, 3n].reduce(BigInt['+'], 0n);// 6n
BigInt['=='](total, 6n);// true

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: bigint): 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);

Comparator

!=

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

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

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

    @example
    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: void, left: bigint, right: bigint): boolean
  • "Less than or equal to" operator

    @example
    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: void, left: bigint, right: bigint): boolean
  • "Equal to" operator

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

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

    @example
    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: void, left: bigint, right: bigint): boolean
  • "Greater than or equal to" operator

    @example
    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: void, value: bigint, minValue: bigint, maxValue: bigint): bigint
  • Clamp value between minValue and maxValue

    @example
    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: void, left: bigint, right: bigint): Ordering
  • Return an Ordering that represents comparison result

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

equals

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

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

max

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

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

min

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

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

Formatting

asString

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

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

Indexable

indexType

indexType: bigint

Index type

at

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

indexOf

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

range

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

rangeSize

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

Numeric

*

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

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

+

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

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

-

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

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

abs

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

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

isNegative

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

isPositive

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

isZero

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

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

negate

  • negate(self: bigint): bigint
  • Negates the given value.

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

sign

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

zero

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

    @example
    Number.zero(); // 0

Type

__inspect__

__inspect__: Option<(anyValue: bigint, 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: string

Type string representation

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

asInstance

  • asInstance(anyValue: unknown): Option<bigint>
  • 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 bigint
  • 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

format

  • format(self: bigint, radix?: number): string
  • Return string representation of bigint using radix

    @example
    BigInt.format(1024n, 10);// '1024'
    BigInt.format(1024n, 16);// '400'

fromInt

  • fromInt(value: Int): bigint
  • Convert an integer to a bigint

    @example
    BigInt.fromNumber(Int(1));// 1n
    BigInt.fromNumber(Int(-1));// -1n

fromNumber

  • fromNumber(value: number): undefined | bigint
  • Convert a number to a bigint

    @example
    BigInt.fromNumber(1);// Option.Some(1n)
    BigInt.fromNumber(-1);// Option.Some(-1n)
    BigInt.fromNumber(1.1);// Option.None
    BigInt.fromNumber(Number.MAX_SAFE_INTEGER + 1);// Option.None
    BigInt.fromNumber(Number.MIN_SAFE_INTEGER - 1);// Option.None

parse

  • parse(expression: string): Option<bigint>
  • Parse the expression and returns a bigint

    @example
    BigInt.parse('0b10101');// Option.Some(21n)
    BigInt.parse('1024');// Option.Some(1024n)
    BigInt.parse('0x123');// Option.Some(291n)
    BigInt.parse('0x123');// Option.Some(291n)
    BigInt.parse('invalid');// Option.None