Skip to main content

ByteSize

Index

Codec

__decode__

  • __decode__(this: void, input: unknown, context: Context<ByteSize>): Result<ByteSize, CodecError>
  • 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: ByteSize): 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__

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

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

Formatting

asString

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

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

Numeric

%

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

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

*

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

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

+

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

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

-

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

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

/

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

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

/%

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

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

abs

  • abs(this: void, value: ByteSize): ByteSize
  • 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)

asInt

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

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

isNegative

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

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

isOne

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

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

isPositive

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

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

isZero

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

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

negate

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

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

one

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

    @example
    Number.one(); // 1

sign

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

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

zero

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

    @example
    Number.zero(); // 0

Type

__inspect__

__inspect__: Option<(anyValue: ByteSize, 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(this: void, anyValue: unknown): Option<ByteSize>
  • 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(this: void, anyValue: unknown): anyValue is ByteSize
  • 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

unwrap

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

wrap

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

Other

__call__

  • __call__(value: number): ByteSize

format

  • format(self: ByteSize, options?: FormatOptions): string
  • Formats a ByteSize into a human-readable string.

    @example
    format(ByteSize(1234)); // '1,234.00 MB'
    format(ByteSize(1234), { standard: 'IEC' }); // '1,234.00 KiB'

fromInt

  • fromInt(this: void, value: Int): ByteSize

parse

  • parse(value: string): Option<ByteSize>
  • Parses a human-readable file size string into a ByteSize.

    @example
    parse('1,024 KB'); // Option.Some(ByteSize(1048576))
    parse('21 MB'); // Option.Some(ByteSize(22020096))
    parse('invalid'); // Option.None