Skip to content

Commit

Permalink
Added a maxSize property to some schemas. (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
iwoplaza authored Sep 15, 2024
1 parent 6a2c1ee commit 4cbe151
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 2 deletions.
12 changes: 12 additions & 0 deletions packages/typed-binary/src/structure/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ export class ArraySchema<TElement extends AnySchema> 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<TElement>[] | MaxValue,
measurer: IMeasurer = new Measurer(),
Expand Down
35 changes: 35 additions & 0 deletions packages/typed-binary/src/structure/baseTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import { MaxValue, Schema } from './types';
////

export class BoolSchema extends Schema<boolean> {
/**
* 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();
}
Expand Down Expand Up @@ -57,6 +64,13 @@ export class StringSchema extends Schema<string> {
////

export class ByteSchema extends Schema<number> {
/**
* 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();
}
Expand All @@ -78,6 +92,13 @@ export class ByteSchema extends Schema<number> {
////

export class Int32Schema extends Schema<number> {
/**
* 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();
}
Expand All @@ -99,6 +120,13 @@ export class Int32Schema extends Schema<number> {
////

export class Uint32Schema extends Schema<number> {
/**
* 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();
}
Expand All @@ -120,6 +148,13 @@ export class Uint32Schema extends Schema<number> {
////

export class Float32Schema extends Schema<number> {
/**
* 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();
}
Expand Down
12 changes: 12 additions & 0 deletions packages/typed-binary/src/structure/dynamicArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ export class DynamicArraySchema<TElement extends AnySchema> 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<TElement>[] | typeof MaxValue,
measurer: IMeasurer = new Measurer(),
Expand Down
14 changes: 13 additions & 1 deletion packages/typed-binary/src/structure/keyed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
type IKeyedSchema,
type IRefResolver,
type ISchema,
type MaxValue,
MaxValue,
type PropertyDescription,
Ref,
type Unwrap,
Expand Down Expand Up @@ -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<TInner> | typeof MaxValue,
measurer: IMeasurer = new Measurer(),
Expand Down
18 changes: 18 additions & 0 deletions packages/typed-binary/src/structure/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ export class ObjectSchema<TProps extends Record<string, AnySchema>>
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<TProps> | typeof MaxValue,
measurer: IMeasurer = new Measurer(),
Expand Down
14 changes: 13 additions & 1 deletion packages/typed-binary/src/structure/optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { ParseUnwrapped } from '../utilityTypes';
import {
type AnySchema,
type IRefResolver,
type MaxValue,
MaxValue,
Schema,
type Unwrap,
} from './types';
Expand Down Expand Up @@ -52,6 +52,18 @@ export class OptionalSchema<TInner extends AnySchema> 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<TInner> | MaxValue | undefined,
measurer: IMeasurer = new Measurer(),
Expand Down
12 changes: 12 additions & 0 deletions packages/typed-binary/src/structure/tuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<UnwrapArray<TSequence>> | MaxValue,
measurer: IMeasurer = new Measurer(),
Expand Down

0 comments on commit 4cbe151

Please sign in to comment.