diff --git a/packages/typed-binary/src/structure/array.ts b/packages/typed-binary/src/structure/array.ts index a41cf72..1bc9702 100644 --- a/packages/typed-binary/src/structure/array.ts +++ b/packages/typed-binary/src/structure/array.ts @@ -56,6 +56,18 @@ export class ArraySchema extends Schema< return array; } + /** + * Returns the maximum number of bytes this schema can take up. + * + * Returns `NaN` if the schema is unbounded. If you would like to know + * how many bytes a particular value encoding will take up, use `.measure(value)`. + * + * Alias for `.measure(MaxValue).size` + */ + get maxSize(): number { + return this.elementSchema.measure(MaxValue).size * this.length; + } + measure( values: ParseUnwrapped[] | MaxValue, measurer: IMeasurer = new Measurer(), diff --git a/packages/typed-binary/src/structure/baseTypes.ts b/packages/typed-binary/src/structure/baseTypes.ts index 4148803..7ec8f88 100644 --- a/packages/typed-binary/src/structure/baseTypes.ts +++ b/packages/typed-binary/src/structure/baseTypes.ts @@ -11,6 +11,13 @@ import { MaxValue, Schema } from './types'; //// export class BoolSchema extends Schema { + /** + * The maximum number of bytes this schema can take up. + * + * Alias for `.measure(MaxValue).size` + */ + readonly maxSize = 1; + read(input: ISerialInput): boolean { return input.readBool(); } @@ -57,6 +64,13 @@ export class StringSchema extends Schema { //// export class ByteSchema extends Schema { + /** + * The maximum number of bytes this schema can take up. + * + * Alias for `.measure(MaxValue).size` + */ + readonly maxSize = 1; + read(input: ISerialInput): number { return input.readByte(); } @@ -78,6 +92,13 @@ export class ByteSchema extends Schema { //// export class Int32Schema extends Schema { + /** + * The maximum number of bytes this schema can take up. + * + * Alias for `.measure(MaxValue).size` + */ + readonly maxSize = 4; + read(input: ISerialInput): number { return input.readInt32(); } @@ -99,6 +120,13 @@ export class Int32Schema extends Schema { //// export class Uint32Schema extends Schema { + /** + * The maximum number of bytes this schema can take up. + * + * Alias for `.measure(MaxValue).size` + */ + readonly maxSize = 4; + read(input: ISerialInput): number { return input.readUint32(); } @@ -120,6 +148,13 @@ export class Uint32Schema extends Schema { //// export class Float32Schema extends Schema { + /** + * The maximum number of bytes this schema can take up. + * + * Alias for `.measure(MaxValue).size` + */ + readonly maxSize = 4; + read(input: ISerialInput): number { return input.readFloat32(); } diff --git a/packages/typed-binary/src/structure/dynamicArray.ts b/packages/typed-binary/src/structure/dynamicArray.ts index 13d226b..9dc434b 100644 --- a/packages/typed-binary/src/structure/dynamicArray.ts +++ b/packages/typed-binary/src/structure/dynamicArray.ts @@ -51,6 +51,18 @@ export class DynamicArraySchema extends Schema< return array; } + /** + * The maximum number of bytes this schema can take up. + * + * Is `NaN` if the schema is unbounded. If you would like to know + * how many bytes a particular value encoding will take up, use `.measure(value)`. + * + * Alias for `.measure(MaxValue).size` + */ + get maxSize(): number { + return this.measure(MaxValue).size; + } + measure( values: ParseUnwrapped[] | typeof MaxValue, measurer: IMeasurer = new Measurer(), diff --git a/packages/typed-binary/src/structure/keyed.ts b/packages/typed-binary/src/structure/keyed.ts index 7ed1b71..bfd03d8 100644 --- a/packages/typed-binary/src/structure/keyed.ts +++ b/packages/typed-binary/src/structure/keyed.ts @@ -11,7 +11,7 @@ import { type IKeyedSchema, type IRefResolver, type ISchema, - type MaxValue, + MaxValue, type PropertyDescription, Ref, type Unwrap, @@ -122,6 +122,18 @@ export class KeyedSchema< this.innerType.write(output, value); } + /** + * The maximum number of bytes this schema can take up. + * + * Is `NaN` if the schema is unbounded. If you would like to know + * how many bytes a particular value encoding will take up, use `.measure(value)`. + * + * Alias for `.measure(MaxValue).size` + */ + get maxSize(): number { + return this.measure(MaxValue).size; + } + measure( value: ParseUnwrapped | typeof MaxValue, measurer: IMeasurer = new Measurer(), diff --git a/packages/typed-binary/src/structure/object.ts b/packages/typed-binary/src/structure/object.ts index 66a27e3..17f0695 100644 --- a/packages/typed-binary/src/structure/object.ts +++ b/packages/typed-binary/src/structure/object.ts @@ -80,6 +80,24 @@ export class ObjectSchema> return result; } + /** + * The maximum number of bytes this schema can take up. + * + * Is `NaN` if the schema is unbounded. If you would like to know + * how many bytes a particular value encoding will take up, use `.measure(value)`. + * + * Alias for `.measure(MaxValue).size` + */ + get maxSize(): number { + const measurer = new Measurer(); + + for (const property of Object.values(this.properties)) { + property.measure(MaxValue, measurer); + } + + return measurer.size; + } + measure( value: ParseUnwrappedRecord | typeof MaxValue, measurer: IMeasurer = new Measurer(), diff --git a/packages/typed-binary/src/structure/optional.ts b/packages/typed-binary/src/structure/optional.ts index 99fe436..d6e0a21 100644 --- a/packages/typed-binary/src/structure/optional.ts +++ b/packages/typed-binary/src/structure/optional.ts @@ -8,7 +8,7 @@ import type { ParseUnwrapped } from '../utilityTypes'; import { type AnySchema, type IRefResolver, - type MaxValue, + MaxValue, Schema, type Unwrap, } from './types'; @@ -52,6 +52,18 @@ export class OptionalSchema extends Schema< return undefined; } + /** + * The maximum number of bytes this schema can take up. + * + * Is `NaN` if the schema is unbounded. If you would like to know + * how many bytes a particular value encoding will take up, use `.measure(value)`. + * + * Alias for `.measure(MaxValue).size` + */ + get maxSize(): number { + return this.measure(MaxValue).size; + } + measure( value: ParseUnwrapped | MaxValue | undefined, measurer: IMeasurer = new Measurer(), diff --git a/packages/typed-binary/src/structure/tuple.ts b/packages/typed-binary/src/structure/tuple.ts index d5fc5e8..ba5d701 100644 --- a/packages/typed-binary/src/structure/tuple.ts +++ b/packages/typed-binary/src/structure/tuple.ts @@ -62,6 +62,18 @@ export class TupleSchema< return array; } + /** + * The maximum number of bytes this schema can take up. + * + * Is `NaN` if the schema is unbounded. If you would like to know + * how many bytes a particular value encoding will take up, use `.measure(value)`. + * + * Alias for `.measure(MaxValue).size` + */ + get maxSize(): number { + return this.measure(MaxValue).size; + } + measure( values: Parsed> | MaxValue, measurer: IMeasurer = new Measurer(),