ByteSize
Index
Codec
__decode__
- interface SomeObject {foo: string}const someCodec: Codec<SomeObject> = ...;const input: unknown = ...;const decoded = Codec.decode(someCodec, input);
__encode__
Returns the encoded
inputinterface 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
const someCodec: Codec<unknown> = ...;const jsonSchema = Codec.schema(someCodec);
Formatting
asString
Converts the given value to a String.
Number.asString(123); // '123'
Numeric
-
Subtraction operator
type T = ...;const TNumeric: Numeric.Subtract<T> = ...;const result = Numeric['-'](left, right);// represents (left - right)
*
Multiplication operator
type T = ...;const TNumeric: Numeric.Multiply<T> = ...;const result = Numeric['*'](left, right);// represents (left * right)
/
Division operator
type T = ...;const TNumeric: Numeric.Divide<T> = ...;const result = Numeric['/'](left, right);// represents (left / right)
/%
Quotient/Modulo operator
type T = ...;const TNumeric: Numeric.Integral<T> = ...;const result = Numeric['/%'](left, right);// represents [ left / right, left % right ]
%
Remainder operator
type T = ...;const TNumeric: Numeric.Remainder<T> = ...;const result = Numeric['%'](left, right);// represents (left % right)
+
Addition operator
type T = ...;const TNumeric: Numeric.Add<T> = ...;const result = TNumeric['+'](left, right);// represents (left + right)
abs
Absolute value. It should satisfy
Numeric['*'](Numeric.abs(x), Numeric.sign(x)) == xtype T = ...;const TSigned: Numeric.Signed<T> = ...;const result = TSigned.abs(value);// absolute value of (value)
asInt
Converts the given value to a Int.
const SomeType: AsInt<T>;const value: T;SomeType.asInt(value); // Int(...)
isNegative
Returns true if the number is negative and false if the number is zero or positive.
Number.isPositive(-1);// trueNumber.isPositive(0);// falseNumber.isPositive(1);// false
isOne
Returns true if self is equal to the multiplicative identity.
Number.isOne(1); // trueNumber.isOne(0); // false
isPositive
Returns true if the number is positive and false if the number is zero or negative.
Number.isPositive(-1);// falseNumber.isPositive(0);// falseNumber.isPositive(1);// true
isZero
Returns true if self is equal to the additive identity.
Number.isZero(0); // trueNumber.isZero(1); // false
negate
Negates the given value.
Number.negate(5); // -5Number.negate(Number.negate(5)); // 5
one
Returns the additive identity element of
T, 0.Number.one(); // 1
sign
Sign of a number. It should satisfy
TSigned['*'](TSigned.abs(x), TSigned.sign(x)) == xNumber.sign(-2.5);// -1Number.sign(0);// 0Number.sign(2.5);// 1
zero
Returns the additive identity element of
T, 0.Number.zero(); // 0
Type
__inspect__
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
Type string representation
StringType.typeName // 'String'
Int.typeName // 'Int'
Person.typeName // 'Person'
asInstance
Try to convert anyValue to enum value or else returns
Option.Noneconst StringType: Type<string>;StringType.asInstance('foo'); // Option.Some('foo')StringType.asInstance(12); // Option.None
hasInstance
Return
trueif the given value is an instance of the class.const StringType: Type<string>;StringType.hasInstance('foo'); // trueStringType.hasInstance(42); // false
unwrap
Convert a tagged value to the underlying type
wrap
Convert an underlying type to a tagged type
Other
__call__
format
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
parse
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
Returns the decoded
input,Result.OkorResult.Error()