Aller au contenu principal

ByteSize

Index

Codec

__decode__

  • __decode__(this, input, context): Result<ByteSize, 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);

Formatting

asString

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

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

Numeric

-

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

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

*

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

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

/

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

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

/%

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

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

%

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

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

+

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

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

abs

  • abs(this, value): ByteSize
  • 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): ByteSize
  • Negates the given value.

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

one

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

    Number.one(); // 1

sign

  • sign(this, value): ByteSize
  • 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): ByteSize
  • 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 }'

typeName

typeName: string

Type string representation

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

asInstance

  • asInstance(this, anyValue): Option<ByteSize>
  • 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 ByteSize
  • 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): ByteSize
  • Convert an underlying type to a tagged type

Other

__call__

  • __call__(value): ByteSize

format

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

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

fromInt

  • fromInt(this, value): ByteSize

parse

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

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