From b6531e821f513c2112d0c969dfb38c3344362c1e Mon Sep 17 00:00:00 2001 From: Simon Chan Date: Sun, 10 Jan 2021 00:37:42 +0800 Subject: [PATCH] feat(struct): add missing `int8` and `int16` types --- packages/struct/src/struct.ts | 47 ++++++++++++++++++++++++++--- packages/struct/src/types/number.ts | 4 +++ packages/struct/src/types/utils.ts | 14 ++++----- 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/packages/struct/src/struct.ts b/packages/struct/src/struct.ts index c93dd136..aad7b602 100644 --- a/packages/struct/src/struct.ts +++ b/packages/struct/src/struct.ts @@ -215,18 +215,18 @@ export default class Struct< * Merges (flats) another `Struct`'s fields and extra fields into this one. */ public fields>( - struct: TOther + other: TOther ): Struct< TValue & TOther['valueType'], TInit & TOther['initType'], TExtra & TOther['extraType'], TPostDeserialized > { - for (const field of struct._fields) { + for (const field of other._fields) { this._fields.push(field); } - this._size += struct._size; - Object.assign(this._extra, struct._extra); + this._size += other._size; + Object.assign(this._extra, other._extra); return this as any; } @@ -245,6 +245,23 @@ export default class Struct< ); } + /** + * Appends an `int8` field to the `Struct` + */ + public int8< + TName extends PropertyKey, + TTypeScriptType = (typeof NumberFieldType)['Uint8']['valueType'] + >( + name: TName, + _typescriptType?: TTypeScriptType, + ) { + return this.number( + name, + NumberFieldType.Int8, + _typescriptType + ); + } + /** * Appends an `uint8` field to the `Struct` */ @@ -262,6 +279,23 @@ export default class Struct< ); } + /** + * Appends an `int16` field to the `Struct` + */ + public int16< + TName extends PropertyKey, + TTypeScriptType = (typeof NumberFieldType)['Uint16']['valueType'] + >( + name: TName, + _typescriptType?: TTypeScriptType, + ) { + return this.number( + name, + NumberFieldType.Int16, + _typescriptType + ); + } + /** * Appends an `uint16` field to the `Struct` */ @@ -409,11 +443,14 @@ export default class Struct< }; /** - * Adds some extra fields into every Struct instance. + * Adds some extra fields into every Struct value. * * Extra fields will not affect serialize or deserialize process. * * Multiple calls to `extra` will merge all values together. + * + * @param value + * An object containing anything you want to add to the result object. Accessors and methods are also allowed. */ public extra { + public static readonly Int8 = new NumberFieldType(1, 'getInt8', 'setInt8'); + public static readonly Uint8 = new NumberFieldType(1, 'getUint8', 'setUint8'); + public static readonly Int16 = new NumberFieldType(2, 'getInt16', 'setUint16'); + public static readonly Uint16 = new NumberFieldType(2, 'getUint16', 'setUint16'); public static readonly Int32 = new NumberFieldType(4, 'getInt32', 'setInt32'); diff --git a/packages/struct/src/types/utils.ts b/packages/struct/src/types/utils.ts index 8aa315d3..c7ec2f02 100644 --- a/packages/struct/src/types/utils.ts +++ b/packages/struct/src/types/utils.ts @@ -33,15 +33,15 @@ export type Overwrite = */ export type OmitNever = Pick; -/** - * Generates a type. Useful in generic type inference. - */ -export function placeholder(): T { - return undefined as unknown as T; -} - /** * Extract keys of fields in `T` that has type `TValue` */ export type KeysOfType = { [TKey in keyof T]: T[TKey] extends TValue ? TKey : never }[keyof T]; + +/** + * Returns a (fake) value of the given type. + */ +export function placeholder(): T { + return undefined as unknown as T; +}